001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------------------
028: * SystemPropertyConfiguration.java
029: * --------------------------------
030: * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner
033: * Contributor(s): Stefan Prange;
034: *
035: * $Id: SystemPropertyConfiguration.java,v 1.3 2005/10/18 13:14:12 mungady Exp $
036: *
037: * Changes (from 8-Feb-2002)
038: * -------------------------
039: * 14-Jan-2003 : Initial Version, moved from inner class of ReportConfiguration
040: * 05-Feb-2003 : This implementation now handles SecurityExceptions.
041: *
042: */
043:
044: package org.jfree.base.config;
045:
046: import java.util.Enumeration;
047: import java.util.Vector;
048:
049: /**
050: * A property configuration based on system properties.
051: *
052: * @author Thomas Morgner
053: */
054: public class SystemPropertyConfiguration extends
055: HierarchicalConfiguration {
056:
057: /**
058: * Creates a report configuration that includes all the system properties (whether they are
059: * related to reports or not). The parent configuration is a
060: * <code>PropertyFileConfiguration</code>.
061: */
062: public SystemPropertyConfiguration() {
063: }
064:
065: /**
066: * Sets a configuration property.
067: *
068: * @param key the property key.
069: * @param value the property value.
070: */
071: public void setConfigProperty(final String key, final String value) {
072: throw new UnsupportedOperationException(
073: "The SystemPropertyConfiguration is readOnly");
074: }
075:
076: /**
077: * Returns the configuration property with the specified key (or the specified default value
078: * if there is no such property).
079: * <p>
080: * If the property is not defined in this configuration, the code will lookup the property in
081: * the parent configuration.
082: *
083: * @param key the property key.
084: * @param defaultValue the default value.
085: *
086: * @return the property value.
087: */
088: public String getConfigProperty(final String key,
089: final String defaultValue) {
090: try {
091: final String value = System.getProperty(key);
092: if (value != null) {
093: return value;
094: }
095: } catch (SecurityException se) {
096: // ignore security exceptions, continue as if the property was not set..
097: }
098: return super .getConfigProperty(key, defaultValue);
099: }
100:
101: /**
102: * Checks, whether the given key is locally defined in the system properties.
103: * @see HierarchicalConfiguration#isLocallyDefined(java.lang.String)
104: *
105: * @param key the key that should be checked.
106: * @return true, if the key is defined in the system properties, false otherwise.
107: */
108: public boolean isLocallyDefined(final String key) {
109: try {
110: return System.getProperties().containsKey(key);
111: } catch (SecurityException se) {
112: return false;
113: }
114: }
115:
116: /**
117: * Returns all defined configuration properties for the report. The enumeration
118: * contains all keys of the changed properties, properties set from files or
119: * the system properties are not included.
120: *
121: * @return all defined configuration properties for the report.
122: */
123: public Enumeration getConfigProperties() {
124: try {
125: return System.getProperties().keys();
126: } catch (SecurityException se) {
127: // should return an empty enumeration ...
128: return new Vector().elements();
129: }
130: }
131: }
|