View Javadoc

1   /*
2    * $Id: PropertiesSettings.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.config;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URL;
27  import java.util.Iterator;
28  
29  import org.apache.struts2.StrutsException;
30  import org.apache.struts2.util.ClassLoaderUtils;
31  
32  import com.opensymphony.xwork2.util.location.LocatableProperties;
33  import com.opensymphony.xwork2.util.location.Location;
34  import com.opensymphony.xwork2.util.location.LocationImpl;
35  import com.opensymphony.xwork2.util.logging.Logger;
36  import com.opensymphony.xwork2.util.logging.LoggerFactory;
37  
38  
39  /***
40   * A class to handle settings via a properties file.
41   */
42  class PropertiesSettings extends Settings {
43  
44      LocatableProperties settings;
45      static Logger LOG = LoggerFactory.getLogger(PropertiesSettings.class);
46  
47  
48      /***
49       * Creates a new properties config given the name of a properties file. The name is expected to NOT have
50       * the ".properties" file extension.  So when <tt>new PropertiesSettings("foo")</tt> is called
51       * this class will look in the classpath for the <tt>foo.properties</tt> file.
52       *
53       * @param name the name of the properties file, excluding the ".properties" extension.
54       */
55      public PropertiesSettings(String name) {
56          
57          URL settingsUrl = ClassLoaderUtils.getResource(name + ".properties", getClass());
58          
59          if (settingsUrl == null) {
60              LOG.debug(name + ".properties missing");
61              settings = new LocatableProperties();
62              return;
63          }
64          
65          settings = new LocatableProperties(new LocationImpl(null, settingsUrl.toString()));
66  
67          // Load settings
68          InputStream in = null;
69          try {
70              in = settingsUrl.openStream();
71              settings.load(in);
72          } catch (IOException e) {
73              throw new StrutsException("Could not load " + name + ".properties:" + e, e);
74          } finally {
75              if(in != null) {
76                  try {
77                      in.close();
78                  } catch(IOException io) {
79                      LOG.warn("Unable to close input stream", io);
80                  }
81              }
82          }
83      }
84  
85  
86      /***
87       * Sets a property in the properties file.
88       *
89       * @see #set(String, String)
90       */
91      public void setImpl(String aName, String aValue) {
92          settings.setProperty(aName, aValue);
93      }
94  
95      /***
96       * Gets a property from the properties file.
97       *
98       * @see #get(String)
99       */
100     public String getImpl(String aName) throws IllegalArgumentException {
101         String setting = settings.getProperty(aName);
102 
103         if (setting == null) {
104             throw new IllegalArgumentException("No such setting:" + aName);
105         }
106 
107         return setting;
108     }
109     
110     /***
111      * Gets the location of a property from the properties file.
112      *
113      * @see #getLocation(String)
114      */
115     public Location getLocationImpl(String aName) throws IllegalArgumentException {
116         Location loc = settings.getPropertyLocation(aName);
117 
118         if (loc == null) {
119             if (!settings.containsKey(aName)) {
120                 throw new IllegalArgumentException("No such setting:" + aName);
121             } 
122         }
123 
124         return loc;
125     }
126 
127     /***
128      * Tests to see if a property exists in the properties file.
129      *
130      * @see #isSet(String)
131      */
132     public boolean isSetImpl(String aName) {
133         if (settings.get(aName) != null) {
134             return true;
135         } else {
136             return false;
137         }
138     }
139 
140     /***
141      * Lists all keys in the properties file.
142      *
143      * @see #list()
144      */
145     public Iterator listImpl() {
146         return settings.keySet().iterator();
147     }
148 }