KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.loader;
30
31 import java.util.Collection JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 /**
37  * Creates a ClassLoader-dependent hashmap.
38  */

39 public class EnvironmentMap extends HashMap JavaDoc {
40   private EnvironmentLocal<HashMap JavaDoc> _envMap = new EnvironmentLocal<HashMap JavaDoc>();
41
42   private HashMap JavaDoc _global;
43
44   /**
45    * Creates a new environment map.
46    */

47   public EnvironmentMap(HashMap JavaDoc global)
48   {
49     _global = global;
50   }
51   
52   public EnvironmentMap()
53   {
54     this(new HashMap JavaDoc());
55   }
56
57   public HashMap JavaDoc getGlobal()
58   {
59     return _global;
60   }
61
62   public HashMap JavaDoc setGlobal(HashMap JavaDoc global)
63   {
64     HashMap JavaDoc old = _global;
65     
66     _global = global;
67
68     return old;
69   }
70
71   public int size()
72   {
73     return getEnvironmentMap().size();
74   }
75
76   public boolean isEmpty()
77   {
78     return getEnvironmentMap().isEmpty();
79   }
80
81   public boolean containsValue(Object JavaDoc value)
82   {
83     return getEnvironmentMap().containsValue(value);
84   }
85
86   public boolean containsKey(Object JavaDoc value)
87   {
88     return getEnvironmentMap().containsKey(value);
89   }
90
91   public Object JavaDoc get(Object JavaDoc key)
92   {
93     Map JavaDoc map = getEnvironmentMap();
94
95     Object JavaDoc value = map.get(key);
96
97     if (value != null)
98       return value;
99     else
100       return _global.get(key);
101   }
102
103   public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
104   {
105     return getPutEnvironmentMap().put(key, value);
106   }
107
108   public Object JavaDoc remove(Object JavaDoc key)
109   {
110     return getPutEnvironmentMap().remove(key);
111   }
112
113   public void putAll(Map JavaDoc map)
114   {
115     getPutEnvironmentMap().putAll(map);
116   }
117
118   public void clear()
119   {
120     getPutEnvironmentMap().clear();
121   }
122
123   public Object JavaDoc clone()
124   {
125     return getEnvironmentMap().clone();
126   }
127
128   public String JavaDoc toString()
129   {
130     return getEnvironmentMap().toString();
131   }
132
133   public Set JavaDoc keySet()
134   {
135     return getPutEnvironmentMap().keySet();
136   }
137
138   public Set JavaDoc entrySet()
139   {
140     return getPutEnvironmentMap().entrySet();
141   }
142
143   public Collection JavaDoc values()
144   {
145     return getPutEnvironmentMap().values();
146   }
147
148   public boolean equals(Object JavaDoc o)
149   {
150     return getEnvironmentMap().equals(o);
151   }
152
153   public int hashCode()
154   {
155     return getEnvironmentMap().hashCode();
156   }
157
158   private HashMap JavaDoc getEnvironmentMap()
159   {
160     HashMap JavaDoc map = _envMap.get();
161
162     if (map != null)
163       return map;
164     else
165       return _global;
166   }
167
168   private synchronized HashMap JavaDoc getPutEnvironmentMap()
169   {
170     HashMap JavaDoc map = _envMap.getLevel();
171     
172     if (map == null) {
173       map = new HashMap JavaDoc();
174       HashMap JavaDoc parentMap = _envMap.get();
175       if (parentMap == null)
176         parentMap = _global;
177
178       map.putAll(parentMap);
179
180       _envMap.set(map);
181       
182       return map;
183     }
184     else
185       return map;
186   }
187 }
188
Popular Tags