KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > EnvironmentProperties


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.loader;
31
32 import java.util.Collection JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * Creates a ClassLoader-dependent properties table.
39  * The value of the EnvironmentLocal variable depends on the
40  * context ClassLoader.
41  */

42 public class EnvironmentProperties extends Properties JavaDoc {
43   private static Properties JavaDoc _origSystemProperties;
44   private static Properties JavaDoc _envSystemProperties;
45   
46   private transient EnvironmentLocal<Properties JavaDoc> _envProps
47     = new EnvironmentLocal<Properties JavaDoc>();
48
49   private Properties JavaDoc _global;
50   
51   public EnvironmentProperties(Properties JavaDoc global)
52   {
53     _global = global;
54   }
55   
56   public EnvironmentProperties()
57   {
58     this(new Properties JavaDoc());
59   }
60
61   public static void enableEnvironmentSystemProperties(boolean isEnable)
62   {
63     if (_origSystemProperties == null) {
64       _origSystemProperties = System.getProperties();
65       _envSystemProperties = new EnvironmentProperties(_origSystemProperties);
66     }
67
68     if (isEnable)
69       System.setProperties(_envSystemProperties);
70     else
71       System.setProperties(_origSystemProperties);
72   }
73
74   public Properties JavaDoc getGlobalProperties()
75   {
76     return _global;
77   }
78
79   public void setGlobalProperties(Properties JavaDoc global)
80   {
81     _global = global;
82   }
83
84   public int size()
85   {
86     return getEnvironmentProperties().size();
87   }
88
89   public boolean isEmpty()
90   {
91     return getEnvironmentProperties().isEmpty();
92   }
93
94   public Enumeration JavaDoc keys()
95   {
96     return getEnvironmentProperties().keys();
97   }
98
99   public Enumeration JavaDoc elements()
100   {
101     return getEnvironmentProperties().elements();
102   }
103
104   public boolean contains(Object JavaDoc value)
105   {
106     return getEnvironmentProperties().contains(value);
107   }
108
109   public boolean containsValue(Object JavaDoc value)
110   {
111     return getEnvironmentProperties().containsValue(value);
112   }
113
114   public boolean containsKey(Object JavaDoc value)
115   {
116     return getEnvironmentProperties().containsKey(value);
117   }
118
119   public Object JavaDoc get(String JavaDoc key)
120   {
121     Properties JavaDoc props = getEnvironmentProperties();
122
123     String JavaDoc value = props.getProperty(key);
124
125     if (value != null)
126       return value;
127     else
128       return _global.get(key);
129   }
130
131   public Object JavaDoc get(Object JavaDoc key)
132   {
133     return get((String JavaDoc) key);
134   }
135
136   public String JavaDoc getProperty(String JavaDoc key)
137   {
138     Properties JavaDoc props = getEnvironmentProperties();
139
140     String JavaDoc value = props.getProperty(key);
141
142     if (value != null)
143       return value;
144     else
145       return _global.getProperty(key);
146   }
147
148   public String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue)
149   {
150     Properties JavaDoc props = getEnvironmentProperties();
151
152     String JavaDoc value = props.getProperty(key);
153
154     if (value != null)
155       return value;
156     else
157       return _global.getProperty(key, defaultValue);
158   }
159
160   public Enumeration JavaDoc propertyNames()
161   {
162     return getEnvironmentProperties().propertyNames();
163   }
164
165   public String JavaDoc put(String JavaDoc key, String JavaDoc value)
166   {
167     return (String JavaDoc) getPutEnvironmentProperties().put(key, value);
168   }
169
170   public Object JavaDoc setProperty(String JavaDoc key, String JavaDoc value)
171   {
172     return put(key, value);
173   }
174
175   public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
176   {
177     return getPutEnvironmentProperties().put(key, value);
178   }
179
180   public String JavaDoc remove(Object JavaDoc key)
181   {
182     return (String JavaDoc) getPutEnvironmentProperties().remove(key);
183   }
184
185   /*
186   public void putAll(Map<K2,V2> map)
187   {
188     getPutEnvironmentProperties().putAll(map);
189   }
190   */

191
192   public void clear()
193   {
194     getPutEnvironmentProperties().clear();
195   }
196
197   public Object JavaDoc clone()
198   {
199     return getEnvironmentProperties().clone();
200   }
201
202   public String JavaDoc toString()
203   {
204     return getEnvironmentProperties().toString();
205   }
206
207   public Set JavaDoc keySet()
208   {
209     return getPutEnvironmentProperties().keySet();
210   }
211
212   public Set JavaDoc entrySet()
213   {
214     return getPutEnvironmentProperties().entrySet();
215   }
216
217   public Collection JavaDoc values()
218   {
219     return getPutEnvironmentProperties().values();
220   }
221
222   public boolean equals(Object JavaDoc o)
223   {
224     return getEnvironmentProperties().equals(o);
225   }
226
227   public int hashCode()
228   {
229     return getEnvironmentProperties().hashCode();
230   }
231
232   private Properties JavaDoc getEnvironmentProperties()
233   {
234     Properties JavaDoc props = _envProps.get();
235
236     if (props != null)
237       return props;
238     else
239       return _global;
240   }
241
242   private synchronized Properties JavaDoc getPutEnvironmentProperties()
243   {
244     Properties JavaDoc props = _envProps.getLevel();
245     
246     if (props == null) {
247       props = new Properties JavaDoc();
248       Properties JavaDoc parentProps = _envProps.get();
249       if (parentProps == null)
250         parentProps = _global;
251
252       props.putAll(parentProps);
253
254       _envProps.set(props);
255       
256       return props;
257     }
258     else
259       return props;
260   }
261
262   public Object JavaDoc writeReplace() throws java.io.ObjectStreamException JavaDoc
263   {
264     return getEnvironmentProperties();
265   }
266 }
267
Popular Tags