1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.config;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URL;
26 import java.util.Iterator;
27 import java.util.Properties;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts2.StrutsException;
32
33 import com.opensymphony.xwork2.util.location.LocatableProperties;
34 import com.opensymphony.xwork2.util.location.Location;
35 import com.opensymphony.xwork2.util.location.LocationImpl;
36
37
38 /***
39 * A class to handle settings via a properties file.
40 */
41 class PropertiesSettings extends Settings {
42
43 LocatableProperties settings;
44 static Log LOG = LogFactory.getLog(PropertiesSettings.class);
45
46
47 /***
48 * Creates a new properties config given the name of a properties file. The name is expected to NOT have
49 * the ".properties" file extension. So when <tt>new PropertiesSettings("foo")</tt> is called
50 * this class will look in the classpath for the <tt>foo.properties</tt> file.
51 *
52 * @param name the name of the properties file, excluding the ".properties" extension.
53 */
54 public PropertiesSettings(String name) {
55
56 URL settingsUrl = Thread.currentThread().getContextClassLoader().getResource(name + ".properties");
57
58 if (settingsUrl == null) {
59 LOG.debug(name + ".properties missing");
60 settings = new LocatableProperties();
61 return;
62 }
63
64 settings = new LocatableProperties(new LocationImpl(null, settingsUrl.toString()));
65
66
67 InputStream in = null;
68 try {
69 in = settingsUrl.openStream();
70 settings.load(in);
71 } catch (IOException e) {
72 throw new StrutsException("Could not load " + name + ".properties:" + e, e);
73 } finally {
74 if(in != null) {
75 try {
76 in.close();
77 } catch(IOException io) {
78 LOG.warn("Unable to close input stream", io);
79 }
80 }
81 }
82 }
83
84
85 /***
86 * Sets a property in the properties file.
87 *
88 * @see #set(String, String)
89 */
90 public void setImpl(String aName, String aValue) {
91 settings.setProperty(aName, aValue);
92 }
93
94 /***
95 * Gets a property from the properties file.
96 *
97 * @see #get(String)
98 */
99 public String getImpl(String aName) throws IllegalArgumentException {
100 String setting = settings.getProperty(aName);
101
102 if (setting == null) {
103 throw new IllegalArgumentException("No such setting:" + aName);
104 }
105
106 return setting;
107 }
108
109 /***
110 * Gets the location of a property from the properties file.
111 *
112 * @see #getLocation(String)
113 */
114 public Location getLocationImpl(String aName) throws IllegalArgumentException {
115 Location loc = settings.getPropertyLocation(aName);
116
117 if (loc == null) {
118 if (!settings.containsKey(aName)) {
119 throw new IllegalArgumentException("No such setting:" + aName);
120 }
121 }
122
123 return loc;
124 }
125
126 /***
127 * Tests to see if a property exists in the properties file.
128 *
129 * @see #isSet(String)
130 */
131 public boolean isSetImpl(String aName) {
132 if (settings.get(aName) != null) {
133 return true;
134 } else {
135 return false;
136 }
137 }
138
139 /***
140 * Lists all keys in the properties file.
141 *
142 * @see #list()
143 */
144 public Iterator listImpl() {
145 return settings.keySet().iterator();
146 }
147 }