1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }