001: /*
002: *
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package com.sun.cdc.config;
029:
030: import java.util.Hashtable;
031: import java.util.Properties;
032: import java.util.HashSet;
033: import java.util.Enumeration;
034:
035: /**
036: * This class holds information necessary for dynamic properties resolution.
037: */
038: public class DynamicProperties {
039: /** Table that holds providers for dynamic system properties. */
040: private static Hashtable propProviders = new Hashtable();
041:
042: /**
043: * Checks whether there are any property providers set.
044: *
045: * @return <code>true</code> if at least one provider is defined,
046: * <code>false</code> otherwise.
047: */
048: public static boolean isEmpty() {
049: return propProviders.isEmpty();
050: }
051:
052: /**
053: * Returns provider for the property corresponding to the key.
054: *
055: * @param key key for the dynamic property.
056: * @return <code>PropertyProvider</code> instance that corresponds to
057: * the given property key.
058: */
059: public static PropertyProvider get(String key) {
060: return (PropertyProvider) propProviders.get(key);
061: }
062:
063: /**
064: * Adds all dynamic properties in their current state to the given
065: * <code>Properties</code> object.
066: *
067: * @param props set of properties that dynamic property values will be
068: * added to.
069: */
070: public static void addSnapshot(Properties props) {
071: if (propProviders.isEmpty()) {
072: return;
073: }
074:
075: /*
076: * Cache all dynamic properties that support caching.
077: */
078: Object[] providers = new HashSet(propProviders.values())
079: .toArray();
080: Hashtable provCached = new Hashtable();
081: for (int i = 0; i < providers.length; i++) {
082: PropertyProvider pp = (PropertyProvider) providers[i];
083: provCached.put(pp, new Boolean(pp.cacheProperties()));
084: }
085:
086: Enumeration provKeys = propProviders.keys();
087: while (provKeys.hasMoreElements()) {
088: String key = (String) provKeys.nextElement();
089: PropertyProvider prov = (PropertyProvider) propProviders
090: .get(key);
091: String value = prov.getValue(key, ((Boolean) provCached
092: .get(prov)).booleanValue());
093: if (value != null) {
094: props.put(key, value);
095: }
096: }
097:
098: }
099:
100: /**
101: * Sets the specified <code>PropertyProvider</code> object to be called
102: * for dynamic property resolution. This method should be used at the time
103: * of properties initialization.
104: *
105: * @param key key for the dynamic property.
106: * @param provider <code>PropertyProvider</code> instance that will be used
107: * to resolve the dynamic property with the specified key.
108: */
109: public static void put(String key, PropertyProvider provider) {
110: propProviders.put(key, provider);
111: }
112: }
|