1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package com.opensymphony.oscache.base; |
6 |
| |
7 |
| import org.apache.commons.logging.Log; |
8 |
| import org.apache.commons.logging.LogFactory; |
9 |
| |
10 |
| import java.io.IOException; |
11 |
| import java.io.InputStream; |
12 |
| import java.net.URL; |
13 |
| |
14 |
| import java.util.Properties; |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class Config implements java.io.Serializable { |
25 |
| |
26 |
| private static final transient Log log = LogFactory.getLog(Config.class); |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| private final static String PROPERTIES_FILENAME = "/oscache.properties"; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| private Properties properties = null; |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
0
| public Config() {
|
45 |
0
| this(null);
|
46 |
| } |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
285
| public Config(Properties p) {
|
60 |
285
| if (log.isDebugEnabled()) {
|
61 |
0
| log.debug("OSCache: Config called");
|
62 |
| } |
63 |
| |
64 |
285
| if (p == null) {
|
65 |
145
| this.properties = loadProperties(PROPERTIES_FILENAME, "the default configuration");
|
66 |
| } else { |
67 |
140
| this.properties = p;
|
68 |
| } |
69 |
| } |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
2427
| public String getProperty(String key) {
|
82 |
2427
| if (key == null) {
|
83 |
5
| throw new IllegalArgumentException("key is null");
|
84 |
| } |
85 |
| |
86 |
2422
| if (properties == null) {
|
87 |
0
| return null;
|
88 |
| } |
89 |
| |
90 |
2422
| return properties.getProperty(key);
|
91 |
| } |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
0
| public Properties getProperties() {
|
100 |
0
| return properties;
|
101 |
| } |
102 |
| |
103 |
206
| public Object get(Object key) {
|
104 |
206
| return properties.get(key);
|
105 |
| } |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
0
| public void set(Object key, Object value) {
|
116 |
0
| if (key == null) {
|
117 |
0
| throw new IllegalArgumentException("key is null");
|
118 |
| } |
119 |
| |
120 |
0
| if (value == null) {
|
121 |
0
| return;
|
122 |
| } |
123 |
| |
124 |
0
| if (properties == null) {
|
125 |
0
| properties = new Properties();
|
126 |
| } |
127 |
| |
128 |
0
| properties.put(key, value);
|
129 |
| } |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
145
| public static Properties loadProperties(URL url, String info) {
|
139 |
145
| log.info("OSCache: Getting properties from URL " + url + " for " + info);
|
140 |
| |
141 |
145
| Properties properties = new Properties();
|
142 |
145
| InputStream in = null;
|
143 |
| |
144 |
145
| try {
|
145 |
145
| in = url.openStream();
|
146 |
145
| properties.load(in);
|
147 |
145
| log.info("OSCache: Properties read " + properties);
|
148 |
| } catch (Exception e) { |
149 |
0
| log.error("OSCache: Error reading from " + url, e);
|
150 |
0
| log.error("OSCache: Ensure the properties information in " + url+ " is readable and in your classpath.");
|
151 |
| } finally { |
152 |
145
| try {
|
153 |
145
| in.close();
|
154 |
| } catch (IOException e) { |
155 |
0
| log.warn("OSCache: IOException while closing InputStream: " + e.getMessage());
|
156 |
| } |
157 |
| } |
158 |
| |
159 |
145
| return properties;
|
160 |
| } |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
145
| public static Properties loadProperties(String filename, String info) {
|
172 |
145
| URL url = null;
|
173 |
| |
174 |
145
| ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
|
175 |
145
| if (threadContextClassLoader != null) {
|
176 |
145
| url = threadContextClassLoader.getResource(filename);
|
177 |
| } |
178 |
145
| if (url == null) {
|
179 |
145
| url = Config.class.getResource(filename);
|
180 |
145
| if (url == null) {
|
181 |
0
| log.warn("OSCache: No properties file found in the classpath by filename " + filename);
|
182 |
0
| return new Properties();
|
183 |
| } |
184 |
| } |
185 |
| |
186 |
145
| return loadProperties(url, info);
|
187 |
| } |
188 |
| |
189 |
| } |