001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.loader;
031:
032: import java.util.Collection;
033: import java.util.Enumeration;
034: import java.util.Properties;
035: import java.util.Set;
036:
037: /**
038: * Creates a ClassLoader-dependent properties table.
039: * The value of the EnvironmentLocal variable depends on the
040: * context ClassLoader.
041: */
042: public class EnvironmentProperties extends Properties {
043: private static Properties _origSystemProperties;
044: private static Properties _envSystemProperties;
045:
046: private transient EnvironmentLocal<Properties> _envProps = new EnvironmentLocal<Properties>();
047:
048: private Properties _global;
049:
050: public EnvironmentProperties(Properties global) {
051: _global = global;
052: }
053:
054: public EnvironmentProperties() {
055: this (new Properties());
056: }
057:
058: public static void enableEnvironmentSystemProperties(
059: boolean isEnable) {
060: if (_origSystemProperties == null) {
061: _origSystemProperties = System.getProperties();
062: _envSystemProperties = new EnvironmentProperties(
063: _origSystemProperties);
064: }
065:
066: if (isEnable)
067: System.setProperties(_envSystemProperties);
068: else
069: System.setProperties(_origSystemProperties);
070: }
071:
072: public Properties getGlobalProperties() {
073: return _global;
074: }
075:
076: public void setGlobalProperties(Properties global) {
077: _global = global;
078: }
079:
080: public int size() {
081: return getEnvironmentProperties().size();
082: }
083:
084: public boolean isEmpty() {
085: return getEnvironmentProperties().isEmpty();
086: }
087:
088: public Enumeration keys() {
089: return getEnvironmentProperties().keys();
090: }
091:
092: public Enumeration elements() {
093: return getEnvironmentProperties().elements();
094: }
095:
096: public boolean contains(Object value) {
097: return getEnvironmentProperties().contains(value);
098: }
099:
100: public boolean containsValue(Object value) {
101: return getEnvironmentProperties().containsValue(value);
102: }
103:
104: public boolean containsKey(Object value) {
105: return getEnvironmentProperties().containsKey(value);
106: }
107:
108: public Object get(String key) {
109: Properties props = getEnvironmentProperties();
110:
111: String value = props.getProperty(key);
112:
113: if (value != null)
114: return value;
115: else
116: return _global.get(key);
117: }
118:
119: public Object get(Object key) {
120: return get((String) key);
121: }
122:
123: public String getProperty(String key) {
124: Properties props = getEnvironmentProperties();
125:
126: String value = props.getProperty(key);
127:
128: if (value != null)
129: return value;
130: else
131: return _global.getProperty(key);
132: }
133:
134: public String getProperty(String key, String defaultValue) {
135: Properties props = getEnvironmentProperties();
136:
137: String value = props.getProperty(key);
138:
139: if (value != null)
140: return value;
141: else
142: return _global.getProperty(key, defaultValue);
143: }
144:
145: public Enumeration propertyNames() {
146: return getEnvironmentProperties().propertyNames();
147: }
148:
149: public String put(String key, String value) {
150: return (String) getPutEnvironmentProperties().put(key, value);
151: }
152:
153: public Object setProperty(String key, String value) {
154: return put(key, value);
155: }
156:
157: public Object put(Object key, Object value) {
158: return getPutEnvironmentProperties().put(key, value);
159: }
160:
161: public String remove(Object key) {
162: return (String) getPutEnvironmentProperties().remove(key);
163: }
164:
165: /*
166: public void putAll(Map<K2,V2> map)
167: {
168: getPutEnvironmentProperties().putAll(map);
169: }
170: */
171:
172: public void clear() {
173: getPutEnvironmentProperties().clear();
174: }
175:
176: public Object clone() {
177: return getEnvironmentProperties().clone();
178: }
179:
180: public String toString() {
181: return getEnvironmentProperties().toString();
182: }
183:
184: public Set keySet() {
185: return getPutEnvironmentProperties().keySet();
186: }
187:
188: public Set entrySet() {
189: return getPutEnvironmentProperties().entrySet();
190: }
191:
192: public Collection values() {
193: return getPutEnvironmentProperties().values();
194: }
195:
196: public boolean equals(Object o) {
197: return getEnvironmentProperties().equals(o);
198: }
199:
200: public int hashCode() {
201: return getEnvironmentProperties().hashCode();
202: }
203:
204: private Properties getEnvironmentProperties() {
205: Properties props = _envProps.get();
206:
207: if (props != null)
208: return props;
209: else
210: return _global;
211: }
212:
213: private synchronized Properties getPutEnvironmentProperties() {
214: Properties props = _envProps.getLevel();
215:
216: if (props == null) {
217: props = new Properties();
218: Properties parentProps = _envProps.get();
219: if (parentProps == null)
220: parentProps = _global;
221:
222: props.putAll(parentProps);
223:
224: _envProps.set(props);
225:
226: return props;
227: } else
228: return props;
229: }
230:
231: public Object writeReplace() throws java.io.ObjectStreamException {
232: return getEnvironmentProperties();
233: }
234: }
|