01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.config;
06:
07: import com.opensymphony.webwork.WebWorkException;
08:
09: import java.io.IOException;
10: import java.net.URL;
11: import java.util.Iterator;
12: import java.util.Properties;
13:
14: /**
15: * A class to handle configuration via a properties file.
16: *
17: * @author Rickard �berg
18: * @author Jason Carreira
19: * @author Bill Lynch (docs)
20: */
21: public class PropertiesConfiguration extends Configuration {
22:
23: Properties settings;
24:
25: /**
26: * Creates a new properties config given the name of a properties file. The name is expected to NOT have
27: * the ".properties" file extension. So when <tt>new PropertiesConfiguration("foo")</tt> is called
28: * this class will look in the classpath for the <tt>foo.properties</tt> file.
29: *
30: * @param name the name of the properties file, excluding the ".properties" extension.
31: */
32: public PropertiesConfiguration(String name) {
33: settings = new Properties();
34:
35: URL settingsUrl = Thread.currentThread()
36: .getContextClassLoader().getResource(
37: name + ".properties");
38:
39: if (settingsUrl == null) {
40: throw new IllegalStateException(name
41: + ".properties missing");
42: }
43:
44: // Load settings
45: try {
46: settings.load(settingsUrl.openStream());
47: } catch (IOException e) {
48: throw new WebWorkException("Could not load " + name
49: + ".properties:" + e);
50: }
51: }
52:
53: /**
54: * Sets a property in the properties file.
55: *
56: * @see #set(String, Object)
57: */
58: public void setImpl(String aName, Object 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 getImpl(String aName) throws IllegalArgumentException {
68: Object setting = settings.get(aName);
69:
70: if (setting == null) {
71: throw new IllegalArgumentException("No such setting:"
72: + aName);
73: }
74:
75: return setting;
76: }
77:
78: /**
79: * Tests to see if a property exists in the properties file.
80: *
81: * @see #isSet(String)
82: */
83: public boolean isSetImpl(String aName) {
84: if (settings.get(aName) != null) {
85: return true;
86: } else {
87: return false;
88: }
89: }
90:
91: /**
92: * Lists all keys in the properties file.
93: *
94: * @see #list()
95: */
96: public Iterator listImpl() {
97: return settings.keySet().iterator();
98: }
99: }
|