KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > tbutils > res > Resources


1 package com.tonbeller.tbutils.res;
2
3 import java.io.File JavaDoc;
4 import java.text.MessageFormat JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.MissingResourceException JavaDoc;
9
10 /**
11  * Facade to access key/value pairs and application home directory.
12  *
13  * @author av
14  */

15 public class Resources {
16   private ReplacingResourceProvider provider;
17   private Locale JavaDoc locale;
18   private File JavaDoc home;
19   private PersistentResourceProvider persistentProvider;
20   private CompositeResourceProvider compositeProvider;
21   
22   static final String JavaDoc PERSISTENT_PROPERTIES = "persistent.properties";
23
24   /**
25    * returns a <code>Resources</code> instance in the default locale
26    * of the VM. This is NOT the locale of the users browser etc.
27    */

28   public static Resources instance() {
29     return ResourcesFactory.instance().getResources(null, null);
30   }
31
32   /**
33    * returns a <code>Resources</code> instance in the configured locale, or if
34    * no locale was configured, in the browsers locale.
35    */

36   public static Resources instance(Locale JavaDoc browserLocale) {
37     return ResourcesFactory.instance().getResources(browserLocale, null);
38   }
39
40   /**
41    * returns a <code>Resources</code> instance in the configured locale, or if
42    * no locale was configured, in the browsers locale.
43    * <p>
44    * If the package of <code>clazz</code>
45    * contains a ResourceBundle named
46    * <code>resources.properties</code>
47    * that bundle will be added to the returned <code>Resources</code>.
48    */

49   public static Resources instance(Locale JavaDoc browserLocale, Class JavaDoc clazz) {
50     return ResourcesFactory.instance().getResources(browserLocale, clazz.getPackage().getName() + ".resources");
51   }
52
53   /**
54    * returns a <code>Resources</code> instance in the default locale.
55    * <p>
56    * If the package of <code>clazz</code>
57    * contains a ResourceBundle named
58    * <code>resources.properties</code>
59    * that bundle will be added to the returned <code>Resources</code>.
60    */

61   public static Resources instance(Class JavaDoc clazz) {
62     return ResourcesFactory.instance().getResources(Locale.getDefault(), clazz.getPackage().getName() + ".resources");
63   }
64   
65   /**
66    * returns a <code>Resources</code> instance in the configured locale, or if
67    * no locale was configured, in the browsers locale.
68    * <p>
69    * The named <code>ResourceBundle</code> will be added to the returned <code>Resources</code>.
70    */

71   public static Resources instance(Locale JavaDoc browserLocale, String JavaDoc bundleName) {
72     return ResourcesFactory.instance().getResources(browserLocale, bundleName);
73   }
74   
75   Resources(CompositeResourceProvider compositeProvider, Locale JavaDoc locale, File JavaDoc home) {
76     this.locale = locale;
77     this.home = home;
78     this.compositeProvider = compositeProvider;
79     File JavaDoc persistentProperties = new File JavaDoc(home, PERSISTENT_PROPERTIES);
80     this.persistentProvider = new FilePersistentResourceProvider(persistentProperties);
81     // make persistentProvider the first one to look into
82
compositeProvider.add(0, persistentProvider);
83     this.provider = new ReplacingResourceProvider(compositeProvider);
84   }
85
86   /**
87    * looksup a string value.
88    * @param key key in resourcebundle
89    * @param defaultValue returnvalue if key was not found
90    * @return resource value or defaultValue
91    */

92   public String JavaDoc getOptionalString(String JavaDoc key, String JavaDoc defaultValue) {
93     String JavaDoc s = provider.getString(key);
94     if (s == null)
95       return defaultValue;
96     return s.trim();
97   }
98   
99   /**
100    * looks up a string value.
101    * @throws MissingResourceException if key was not found
102    */

103   public String JavaDoc getString(String JavaDoc key) throws MissingResourceException JavaDoc {
104     String JavaDoc s = provider.getString(key);
105     if (s == null)
106       throw new MissingResourceException JavaDoc("missing resource for " + key, this.getClass().getName(), key);
107     return s.trim();
108   }
109
110   /**
111    * looks up a string value and formats via MessageFormat
112    */

113   public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg) throws MissingResourceException JavaDoc {
114     String JavaDoc fmt = getString(key);
115     return MessageFormat.format(fmt, new Object JavaDoc[]{arg});
116   }
117
118   /**
119    * looks up a string value and formats via MessageFormat
120    */

121   public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg0, Object JavaDoc arg1) throws MissingResourceException JavaDoc {
122     String JavaDoc fmt = getString(key);
123     return MessageFormat.format(fmt, new Object JavaDoc[]{arg0, arg1});
124   }
125
126   /**
127    * looks up a string value and formats via MessageFormat
128    */

129   public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] args) throws MissingResourceException JavaDoc {
130     String JavaDoc fmt = getString(key);
131     return MessageFormat.format(fmt, args);
132   }
133
134   /**
135    * looks up a boolean value
136    */

137   public boolean getBoolean(String JavaDoc key) throws MissingResourceException JavaDoc {
138     String JavaDoc s = getString(key);
139     return "true".equals(s) || "on".equals(s) || "yes".equals(s);
140   }
141
142   public boolean getOptionalBoolean(String JavaDoc key, boolean defaultValue) {
143     try {
144       return getBoolean(key);
145     } catch (MissingResourceException JavaDoc e) {
146       return defaultValue;
147     }
148   }
149
150   /**
151    * looks up an integervalue
152    */

153   public int getInteger(String JavaDoc key) throws MissingResourceException JavaDoc {
154     String JavaDoc s = getString(key);
155     return Integer.parseInt(s);
156   }
157
158   public int getOptionalInteger(String JavaDoc key, int defaultValue) {
159     try {
160       return getInteger(key);
161     } catch (MissingResourceException JavaDoc e) {
162       return defaultValue;
163     }
164   }
165   
166   /**
167    * looks up a long value
168    */

169   public long getLong(String JavaDoc key) throws MissingResourceException JavaDoc {
170     String JavaDoc s = getString(key);
171     return Long.parseLong(s);
172   }
173
174   public long getOptionalLong(String JavaDoc key, long defaultValue) {
175     try {
176       return getLong(key);
177     } catch (MissingResourceException JavaDoc e) {
178       return defaultValue;
179     }
180   }
181   
182   /**
183    * looks up a double value
184    */

185   public double getDouble(String JavaDoc key) throws MissingResourceException JavaDoc {
186     String JavaDoc s = getString(key);
187     return Double.parseDouble(s);
188   }
189
190   public double getOptionalDouble(String JavaDoc key, double defaultValue) {
191     try {
192       return getDouble(key);
193     } catch (MissingResourceException JavaDoc e) {
194       return defaultValue;
195     }
196   }
197   
198   /**
199    * returns a collection of all known keys. The list maybe incomplete
200    * eg. JNDI keys are not returned.
201    */

202   public Collection JavaDoc keySet() {
203     return provider.keySet();
204   }
205
206   /**
207    * returns the locale for this instance
208    */

209   public Locale JavaDoc getLocale() {
210     return locale;
211   }
212
213   /**
214    * returns the applications "home" directory which may contain
215    * properties files and other resources.
216    */

217   public File JavaDoc getHomeDir() {
218     return home;
219   }
220   
221   /**
222    * stores a key/value pair that will be persisted in the filesystem.
223    * @see #flush()
224    */

225   public void setPersistentString(String JavaDoc key, String JavaDoc value) {
226     persistentProvider.store(key, value);
227   }
228   
229   /**
230    * stores a key/value pair that will be persisted in the filesystem.
231    * @see #flush()
232    */

233   public void setPersistentBoolean(String JavaDoc key, boolean value) {
234     persistentProvider.store(key, value ? "true" : "false");
235   }
236
237   /**
238    * stores a key/value pair that will be persisted in the filesystem.
239    * @see #flush()
240    */

241   public void setPersistentInteger(String JavaDoc key, int value) {
242     persistentProvider.store(key, Integer.toString(value));
243   }
244
245   /**
246    * stores a key/value pair that will be persisted in the filesystem.
247    * @see #flush()
248    */

249   public void setPersistentLong(String JavaDoc key, long value) {
250     persistentProvider.store(key, Long.toString(value));
251   }
252
253   /**
254    * stores a key/value pair that will be persisted in the filesystem.
255    * @see #flush()
256    */

257   public void setPersistentDouble(String JavaDoc key, double value) {
258     persistentProvider.store(key, Double.toString(value));
259   }
260   
261   public void removePersistent(String JavaDoc key) {
262     persistentProvider.remove(key);
263   }
264   
265   /**
266    * commits changes into the storage.
267    */

268   public void flush() {
269     persistentProvider.flush();
270   }
271   
272   /**
273    * returns all key/value pairs for logging / debug
274    * @see StringDumper
275    */

276   public void dump(Dumper d) {
277     provider.dump(d);
278   }
279   
280   /**
281    * scans <code>s</code> for resource keys and replaces the keys by their values.
282    * <p />
283    * <code>s</code> may contain resource keys in $-notation like ant, for example
284    * <code>${java.io.tmpdir}</code>.
285    * If the property exists, its replaced by its value, e.g. <code>/tmp</code>.
286    * If the property does not exsist, the string is not changed, e.g. it remains
287    * <code>${java.io.tmpdir}</code>. A literal <code>${</code>
288    * is written as <code>$${</code> (like ant), i.e. <code>$${</code> is
289    * replaced by <code>${</code>
290    * <p />
291    * Example:
292    * <pre>
293    * Resources res = Resources.instance();
294    * String s = res.replace("${java.io.tmpdir}/mylogs");
295    * // s = "c:\\temp/mylogs" for example
296    * File myLogs = new File(s);
297    * </pre>
298    * <p />
299    * Property values are replaced automatically, e.g. if the properties file contains
300    * <pre>
301    * mylogs = ${java.io.tmpdir}/mylogs
302    * </pre>
303    * then <code>Resources.instance().getString("mylogs")</code> will return "c:\\temp/mylogs".
304    */

305   public String JavaDoc replace(String JavaDoc s) {
306     return provider.replace(s);
307   }
308   
309   /**
310    * returns the list of {@link ResourceProvider}'s that this instance
311    * searches for properties. The list may be modified.
312    * @see ResourceProvider
313    */

314   public List JavaDoc getProviders() {
315     return compositeProvider.getProviders();
316   }
317 }
Popular Tags