KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > config > PropertiesConfiguration


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.config;
6
7 import java.io.IOException JavaDoc;
8 import java.net.URL JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Properties JavaDoc;
11
12
13 /**
14  * A class to handle configuration via a properties file.
15  *
16  * @author Rickard Öberg
17  * @author Jason Carreira
18  * @author Bill Lynch (docs)
19  */

20 public class PropertiesConfiguration extends Configuration {
21     //~ Instance fields ////////////////////////////////////////////////////////
22

23     Properties JavaDoc settings;
24
25     //~ Constructors ///////////////////////////////////////////////////////////
26

27     /**
28      * Creates a new properties config given the name of a properties file. The name is expected to NOT have
29      * the ".properties" file extension. So when <tt>new PropertiesConfiguration("foo")</tt> is called
30      * this class will look in the classpath for the <tt>foo.properties</tt> file.
31      *
32      * @param name the name of the properties file, excluding the ".properties" extension.
33      */

34     public PropertiesConfiguration(String JavaDoc name) {
35         settings = new Properties JavaDoc();
36
37         URL JavaDoc settingsUrl = Thread.currentThread().getContextClassLoader().getResource(name + ".properties");
38
39         if (settingsUrl == null) {
40             throw new IllegalStateException JavaDoc(name + ".properties missing");
41         }
42
43         // Load settings
44
try {
45             settings.load(settingsUrl.openStream());
46         } catch (IOException JavaDoc e) {
47             throw new RuntimeException JavaDoc("Could not load " + name + ".properties:" + e);
48         }
49     }
50
51     //~ Methods ////////////////////////////////////////////////////////////////
52

53     /**
54      * Sets a property in the properties file.
55      *
56      * @see #set(String, Object)
57      */

58     public void setImpl(String JavaDoc aName, Object JavaDoc aValue) {
59         settings.put(aName, aValue);
60     }
61
62     /**
63      * Gets a property from the properties file.
64      *
65      * @see #get(String)
66      */

67     public Object JavaDoc getImpl(String JavaDoc aName) throws IllegalArgumentException JavaDoc {
68         Object JavaDoc setting = settings.get(aName);
69
70         if (setting == null) {
71             throw new IllegalArgumentException JavaDoc("No such setting:" + aName);
72         }
73
74         return setting;
75     }
76
77     /**
78      * Tests to see if a property exists in the properties file.
79      *
80      * @see #isSet(String)
81      */

82     public boolean isSetImpl(String JavaDoc aName) {
83         if (settings.get(aName) != null) {
84             return true;
85         } else {
86             return false;
87         }
88     }
89
90     /**
91      * Lists all keys in the properties file.
92      *
93      * @see #list()
94      */

95     public Iterator JavaDoc listImpl() {
96         return settings.keySet().iterator();
97     }
98 }
99
Popular Tags