KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > base > config > SystemPropertyConfiguration


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------------------
28  * SystemPropertyConfiguration.java
29  * --------------------------------
30  * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner
33  * Contributor(s): Stefan Prange;
34  *
35  * $Id: SystemPropertyConfiguration.java,v 1.3 2005/10/18 13:14:12 mungady Exp $
36  *
37  * Changes (from 8-Feb-2002)
38  * -------------------------
39  * 14-Jan-2003 : Initial Version, moved from inner class of ReportConfiguration
40  * 05-Feb-2003 : This implementation now handles SecurityExceptions.
41  *
42  */

43
44 package org.jfree.base.config;
45
46 import java.util.Enumeration JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 /**
50  * A property configuration based on system properties.
51  *
52  * @author Thomas Morgner
53  */

54 public class SystemPropertyConfiguration extends HierarchicalConfiguration {
55
56     /**
57      * Creates a report configuration that includes all the system properties (whether they are
58      * related to reports or not). The parent configuration is a
59      * <code>PropertyFileConfiguration</code>.
60      */

61     public SystemPropertyConfiguration() {
62     }
63
64     /**
65      * Sets a configuration property.
66      *
67      * @param key the property key.
68      * @param value the property value.
69      */

70     public void setConfigProperty(final String JavaDoc key, final String JavaDoc value) {
71         throw new UnsupportedOperationException JavaDoc("The SystemPropertyConfiguration is readOnly");
72     }
73
74     /**
75      * Returns the configuration property with the specified key (or the specified default value
76      * if there is no such property).
77      * <p>
78      * If the property is not defined in this configuration, the code will lookup the property in
79      * the parent configuration.
80      *
81      * @param key the property key.
82      * @param defaultValue the default value.
83      *
84      * @return the property value.
85      */

86     public String JavaDoc getConfigProperty(final String JavaDoc key, final String JavaDoc defaultValue) {
87         try {
88             final String JavaDoc value = System.getProperty(key);
89             if (value != null) {
90                return value;
91             }
92         }
93         catch (SecurityException JavaDoc se) {
94             // ignore security exceptions, continue as if the property was not set..
95
}
96         return super.getConfigProperty(key, defaultValue);
97     }
98
99     /**
100      * Checks, whether the given key is locally defined in the system properties.
101      * @see HierarchicalConfiguration#isLocallyDefined(java.lang.String)
102      *
103      * @param key the key that should be checked.
104      * @return true, if the key is defined in the system properties, false otherwise.
105      */

106     public boolean isLocallyDefined(final String JavaDoc key) {
107         try {
108             return System.getProperties().containsKey(key);
109         }
110         catch (SecurityException JavaDoc se) {
111             return false;
112         }
113     }
114
115     /**
116      * Returns all defined configuration properties for the report. The enumeration
117      * contains all keys of the changed properties, properties set from files or
118      * the system properties are not included.
119      *
120      * @return all defined configuration properties for the report.
121      */

122     public Enumeration JavaDoc getConfigProperties() {
123         try {
124             return System.getProperties().keys();
125         }
126         catch (SecurityException JavaDoc se) {
127             // should return an empty enumeration ...
128
return new Vector JavaDoc().elements();
129         }
130     }
131 }
132
Popular Tags