KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
32  * Creates a ClassLoader dependent variable.
33  * The value of the ClassLoaderLocal
34  * variable depends on the context ClassLoader.
35  */

36 public class EnvironmentLocal<E> {
37   private static int _varCount;
38   
39   private String JavaDoc _varName;
40   private E _globalValue;
41
42   /**
43    * Creates a new environment local variable with an anonymous
44    * identifier.
45    */

46   public EnvironmentLocal()
47   {
48     _varName = "resin:var-" + _varCount++;
49   }
50
51   public EnvironmentLocal(String JavaDoc varName)
52   {
53     _varName = varName;
54   }
55
56   public String JavaDoc getVariable()
57   {
58     return _varName;
59   }
60
61   /**
62    * Returns the variable for the context classloader.
63    */

64   public E get()
65   {
66     Thread JavaDoc thread = Thread.currentThread();
67     ClassLoader JavaDoc loader = thread.getContextClassLoader();
68
69     Object JavaDoc value = null;
70
71     for (; loader != null; loader = loader.getParent()) {
72       if (loader instanceof EnvironmentClassLoader) {
73         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
74
75         value = envLoader.getAttribute(_varName);
76
77         if (value != null)
78           return (E) value;
79       }
80     }
81
82     return _globalValue;
83   }
84
85   /**
86    * Returns the variable for the context classloader.
87    */

88   public E get(ClassLoader JavaDoc loader)
89   {
90     Object JavaDoc value = null;
91
92     for (; loader != null; loader = loader.getParent()) {
93       if (loader instanceof EnvironmentClassLoader) {
94         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
95
96         value = envLoader.getAttribute(_varName);
97
98         if (value != null)
99           return (E) value;
100       }
101     }
102
103     return _globalValue;
104   }
105
106   /**
107    * Returns the variable for the context classloader.
108    */

109   public E getLevel()
110   {
111     Thread JavaDoc thread = Thread.currentThread();
112     ClassLoader JavaDoc loader = thread.getContextClassLoader();
113
114     for (; loader != null; loader = loader.getParent()) {
115       if (loader instanceof EnvironmentClassLoader) {
116         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
117
118         return (E) envLoader.getAttribute(_varName);
119       }
120     }
121
122     return _globalValue;
123   }
124
125   /**
126    * Returns the variable for the context classloader.
127    */

128   public E getLevel(ClassLoader JavaDoc loader)
129   {
130     for (; loader != null; loader = loader.getParent()) {
131       if (loader instanceof EnvironmentClassLoader) {
132         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
133
134         return (E) envLoader.getAttribute(_varName);
135       }
136     }
137
138     return _globalValue;
139   }
140
141   /**
142    * Sets the variable for the context classloader.
143    *
144    * @param value the new value
145    *
146    * @return the old value
147    */

148   public final E set(E value)
149   {
150     Thread JavaDoc thread = Thread.currentThread();
151     ClassLoader JavaDoc loader = thread.getContextClassLoader();
152     
153     for (; loader != null; loader = loader.getParent()) {
154       if (loader instanceof EnvironmentClassLoader) {
155         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
156
157         return (E) envLoader.setAttribute(_varName, value);
158       }
159     }
160
161     return setGlobal(value);
162   }
163
164   /**
165    * Sets the variable for the context classloader.
166    *
167    * @param value the new value
168    *
169    * @return the old value
170    */

171   public final E set(E value, ClassLoader JavaDoc loader)
172   {
173     for (; loader != null; loader = loader.getParent()) {
174       if (loader instanceof EnvironmentClassLoader) {
175         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
176
177         return (E) envLoader.setAttribute(_varName, value);
178       }
179     }
180
181     return setGlobal(value);
182   }
183
184   /**
185    * Sets the variable for the context classloader.
186    *
187    * @param value the new value
188    *
189    * @return the old value
190    */

191   public final E remove()
192   {
193     Thread JavaDoc thread = Thread.currentThread();
194     ClassLoader JavaDoc loader = thread.getContextClassLoader();
195     
196     for (; loader != null; loader = loader.getParent()) {
197       if (loader instanceof EnvironmentClassLoader) {
198         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
199
200         return (E) envLoader.removeAttribute(_varName);
201       }
202     }
203
204     return setGlobal(null);
205   }
206
207   /**
208    * Sets the variable for the context classloader.
209    *
210    * @param value the new value
211    *
212    * @return the old value
213    */

214   public final E remove(ClassLoader JavaDoc loader)
215   {
216     for (; loader != null; loader = loader.getParent()) {
217       if (loader instanceof EnvironmentClassLoader) {
218         EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
219
220         return (E) envLoader.removeAttribute(_varName);
221       }
222     }
223
224     return setGlobal(null);
225   }
226
227   /**
228    * Sets the global value.
229    *
230    * @param value the new value
231    *
232    * @return the old value
233    */

234   public E setGlobal(E value)
235   {
236     E oldValue = _globalValue;
237     
238     _globalValue = value;
239
240     return oldValue;
241   }
242
243   /**
244    * Returns the global value.
245    */

246   public E getGlobal()
247   {
248     return _globalValue;
249   }
250 }
251
Popular Tags