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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.loader;
030:
031: /**
032: * Creates a ClassLoader dependent variable.
033: * The value of the ClassLoaderLocal
034: * variable depends on the context ClassLoader.
035: */
036: public class EnvironmentLocal<E> {
037: private static int _varCount;
038:
039: private String _varName;
040: private E _globalValue;
041:
042: /**
043: * Creates a new environment local variable with an anonymous
044: * identifier.
045: */
046: public EnvironmentLocal() {
047: _varName = "resin:var-" + _varCount++;
048: }
049:
050: public EnvironmentLocal(String varName) {
051: _varName = varName;
052: }
053:
054: public String getVariable() {
055: return _varName;
056: }
057:
058: /**
059: * Returns the variable for the context classloader.
060: */
061: public E get() {
062: Thread thread = Thread.currentThread();
063: ClassLoader loader = thread.getContextClassLoader();
064:
065: Object value = null;
066:
067: for (; loader != null; loader = loader.getParent()) {
068: if (loader instanceof EnvironmentClassLoader) {
069: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
070:
071: value = envLoader.getAttribute(_varName);
072:
073: if (value != null)
074: return (E) value;
075: }
076: }
077:
078: return _globalValue;
079: }
080:
081: /**
082: * Returns the variable for the context classloader.
083: */
084: public E get(ClassLoader loader) {
085: Object value = null;
086:
087: for (; loader != null; loader = loader.getParent()) {
088: if (loader instanceof EnvironmentClassLoader) {
089: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
090:
091: value = envLoader.getAttribute(_varName);
092:
093: if (value != null)
094: return (E) value;
095: }
096: }
097:
098: return _globalValue;
099: }
100:
101: /**
102: * Returns the variable for the context classloader.
103: */
104: public E getLevel() {
105: Thread thread = Thread.currentThread();
106: ClassLoader loader = thread.getContextClassLoader();
107:
108: for (; loader != null; loader = loader.getParent()) {
109: if (loader instanceof EnvironmentClassLoader) {
110: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
111:
112: return (E) envLoader.getAttribute(_varName);
113: }
114: }
115:
116: return _globalValue;
117: }
118:
119: /**
120: * Returns the variable for the context classloader.
121: */
122: public E getLevel(ClassLoader loader) {
123: for (; loader != null; loader = loader.getParent()) {
124: if (loader instanceof EnvironmentClassLoader) {
125: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
126:
127: return (E) envLoader.getAttribute(_varName);
128: }
129: }
130:
131: return _globalValue;
132: }
133:
134: /**
135: * Sets the variable for the context classloader.
136: *
137: * @param value the new value
138: *
139: * @return the old value
140: */
141: public final E set(E value) {
142: Thread thread = Thread.currentThread();
143: ClassLoader loader = thread.getContextClassLoader();
144:
145: for (; loader != null; loader = loader.getParent()) {
146: if (loader instanceof EnvironmentClassLoader) {
147: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
148:
149: return (E) envLoader.setAttribute(_varName, value);
150: }
151: }
152:
153: return setGlobal(value);
154: }
155:
156: /**
157: * Sets the variable for the context classloader.
158: *
159: * @param value the new value
160: *
161: * @return the old value
162: */
163: public final E set(E value, ClassLoader loader) {
164: for (; loader != null; loader = loader.getParent()) {
165: if (loader instanceof EnvironmentClassLoader) {
166: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
167:
168: return (E) envLoader.setAttribute(_varName, value);
169: }
170: }
171:
172: return setGlobal(value);
173: }
174:
175: /**
176: * Sets the variable for the context classloader.
177: *
178: * @param value the new value
179: *
180: * @return the old value
181: */
182: public final E remove() {
183: Thread thread = Thread.currentThread();
184: ClassLoader loader = thread.getContextClassLoader();
185:
186: for (; loader != null; loader = loader.getParent()) {
187: if (loader instanceof EnvironmentClassLoader) {
188: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
189:
190: return (E) envLoader.removeAttribute(_varName);
191: }
192: }
193:
194: return setGlobal(null);
195: }
196:
197: /**
198: * Sets the variable for the context classloader.
199: *
200: * @param value the new value
201: *
202: * @return the old value
203: */
204: public final E remove(ClassLoader loader) {
205: for (; loader != null; loader = loader.getParent()) {
206: if (loader instanceof EnvironmentClassLoader) {
207: EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader;
208:
209: return (E) envLoader.removeAttribute(_varName);
210: }
211: }
212:
213: return setGlobal(null);
214: }
215:
216: /**
217: * Sets the global value.
218: *
219: * @param value the new value
220: *
221: * @return the old value
222: */
223: public E setGlobal(E value) {
224: E oldValue = _globalValue;
225:
226: _globalValue = value;
227:
228: ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
229: if (systemLoader instanceof EnvironmentClassLoader)
230: ((EnvironmentClassLoader) systemLoader).setAttribute(
231: _varName, value);
232:
233: return oldValue;
234: }
235:
236: /**
237: * Returns the global value.
238: */
239: public E getGlobal() {
240: ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
241: if (systemLoader instanceof EnvironmentClassLoader)
242: return (E) ((EnvironmentClassLoader) systemLoader)
243: .getAttribute(_varName);
244:
245: return _globalValue;
246: }
247: }
|