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 Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import com.caucho.loader.*;
033: import com.caucho.webbeans.component.ComponentImpl;
034: import com.caucho.webbeans.manager.WebBeansContainer;
035:
036: import javax.el.ELContext;
037: import javax.el.ELResolver;
038: import java.beans.FeatureDescriptor;
039: import java.util.Iterator;
040:
041: /**
042: * Creates a variable resolver based on the classloader.
043: */
044: public class EnvironmentLevelELResolver extends ELResolver {
045: private static final EnvironmentLocal<EnvironmentLevelELResolver> _local = new EnvironmentLocal<EnvironmentLevelELResolver>();
046:
047: private final ClassLoader _loader;
048: private final WebBeansContainer _webBeans;
049:
050: private EnvironmentLevelELResolver(ClassLoader loader) {
051: _loader = loader;
052:
053: if (Environment.getEnvironmentClassLoader(loader) != null)
054: _webBeans = WebBeansContainer.create(loader);
055: else
056: _webBeans = null;
057: }
058:
059: /**
060: * Creates the resolver
061: */
062: public static EnvironmentLevelELResolver create() {
063: return create(Thread.currentThread().getContextClassLoader());
064: }
065:
066: /**
067: * Creates the resolver
068: */
069: public static EnvironmentLevelELResolver create(ClassLoader loader) {
070: EnvironmentLevelELResolver elResolver = _local.getLevel(loader);
071:
072: if (elResolver == null) {
073: for (; loader != null; loader = loader.getParent()) {
074: if (loader instanceof EnvironmentClassLoader) {
075: elResolver = new EnvironmentLevelELResolver(loader);
076: _local.set(elResolver, loader);
077:
078: return elResolver;
079: }
080: }
081:
082: loader = ClassLoader.getSystemClassLoader();
083: elResolver = new EnvironmentLevelELResolver(loader);
084: _local.set(elResolver, loader);
085: }
086:
087: return elResolver;
088: }
089:
090: /**
091: * Returns true for read-only.
092: */
093: @Override
094: public boolean isReadOnly(ELContext context, Object base,
095: Object property) {
096: if (property != null || !(base instanceof String))
097: return true;
098:
099: context.setPropertyResolved(true);
100:
101: return false;
102: }
103:
104: /**
105: * Returns the named variable value.
106: */
107: @Override
108: public Class<?> getType(ELContext context, Object base,
109: Object property) {
110: Object value = getValue(context, base, property);
111:
112: if (value != null)
113: return value.getClass();
114: else
115: return null;
116: }
117:
118: public Class<?> getCommonPropertyType(ELContext context, Object base) {
119: return null;
120: }
121:
122: public Iterator<FeatureDescriptor> getFeatureDescriptors(
123: ELContext context, Object base) {
124: return null;
125: }
126:
127: /**
128: * Returns the named variable value.
129: */
130: @Override
131: public Object getValue(ELContext env, Object base, Object property) {
132: if (base != null)
133: return null;
134: else if (!(property instanceof String))
135: return null;
136:
137: String var = (String) property;
138:
139: if (_webBeans != null) {
140: ComponentImpl comp = _webBeans.findByName(var);
141:
142: if (comp != null) {
143: Object value = comp.get();
144: env.setPropertyResolved(true);
145: return value;
146: }
147: }
148:
149: Object value = EL.getLevelVar(var, _loader);
150:
151: if (value == null)
152: return null;
153:
154: env.setPropertyResolved(true);
155:
156: if (value == EL.NULL)
157: return null;
158: else
159: return value;
160: }
161:
162: /**
163: * Sets the value for the named variable.
164: */
165: @Override
166: public void setValue(ELContext env, Object base, Object property,
167: Object value) {
168: if (base != null || !(property instanceof String))
169: return;
170:
171: env.setPropertyResolved(true);
172:
173: String name = (String) property;
174:
175: EL.putVar(name, value, _loader);
176: }
177:
178: public boolean equals(Object o) {
179: if (this == o)
180: return true;
181: else if (!(o instanceof EnvironmentLevelELResolver))
182: return false;
183:
184: EnvironmentLevelELResolver resolver = (EnvironmentLevelELResolver) o;
185:
186: return _loader == resolver._loader;
187: }
188:
189: public String toString() {
190: return "EnvironmentLevelELResolver[" + _loader + "]";
191: }
192: }
|