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.EnvironmentClassLoader;
033: import com.caucho.loader.EnvironmentLocal;
034:
035: import javax.el.ELContext;
036: import javax.el.ELResolver;
037: import java.beans.FeatureDescriptor;
038: import java.util.ArrayList;
039: import java.util.Iterator;
040:
041: /**
042: * Creates a variable resolver based on the classloader.
043: */
044: public class EnvironmentELResolver extends ELResolver {
045: private static final EnvironmentLocal<EnvironmentELResolver> _local = new EnvironmentLocal<EnvironmentELResolver>();
046:
047: private ArrayList<ELResolver> _resolvers = new ArrayList<ELResolver>();
048:
049: private EnvironmentELResolver(ClassLoader loader) {
050: for (; loader != null; loader = loader.getParent()) {
051: if (loader instanceof EnvironmentClassLoader) {
052: EnvironmentLevelELResolver resolver = EnvironmentLevelELResolver
053: .create(loader);
054:
055: if (!_resolvers.contains(resolver))
056: _resolvers.add(resolver);
057: }
058: }
059:
060: EnvironmentLevelELResolver resolver = EnvironmentLevelELResolver
061: .create(ClassLoader.getSystemClassLoader());
062:
063: if (!_resolvers.contains(resolver))
064: _resolvers.add(resolver);
065: }
066:
067: /**
068: * Creates the resolver
069: */
070: public static EnvironmentELResolver create() {
071: return create(Thread.currentThread().getContextClassLoader());
072: }
073:
074: /**
075: * Creates the resolver
076: */
077: public static EnvironmentELResolver create(ClassLoader loader) {
078: EnvironmentELResolver elResolver = _local.getLevel(loader);
079:
080: if (elResolver == null) {
081: elResolver = new EnvironmentELResolver(loader);
082:
083: _local.set(elResolver, loader);
084: }
085:
086: return elResolver;
087: }
088:
089: /**
090: * Returns true for read-only.
091: */
092: @Override
093: public boolean isReadOnly(ELContext context, Object base,
094: Object property) {
095: if (property != null || !(base instanceof String))
096: return true;
097:
098: context.setPropertyResolved(true);
099:
100: return false;
101: }
102:
103: /**
104: * Returns the named variable value.
105: */
106: @Override
107: public Class<?> getType(ELContext context, Object base,
108: Object property) {
109: Object value = getValue(context, base, property);
110:
111: if (value != null)
112: return value.getClass();
113: else
114: return null;
115: }
116:
117: public Class<?> getCommonPropertyType(ELContext context, Object base) {
118: return null;
119: }
120:
121: public Iterator<FeatureDescriptor> getFeatureDescriptors(
122: ELContext context, Object base) {
123: return null;
124: }
125:
126: /**
127: * Returns the named variable value.
128: */
129: @Override
130: public Object getValue(ELContext context, Object base,
131: Object property) {
132: if (base != null)
133: return null;
134: else if (!(property instanceof String))
135: return null;
136:
137: context.setPropertyResolved(false);
138: for (int i = 0; i < _resolvers.size(); i++) {
139: Object value = _resolvers.get(i).getValue(context, base,
140: property);
141:
142: if (context.isPropertyResolved())
143: return value;
144: }
145:
146: return null;
147: }
148:
149: /**
150: * Sets the value for the named variable.
151: */
152: @Override
153: public void setValue(ELContext context, Object base,
154: Object property, Object value) {
155: if (base != null || !(property instanceof String))
156: return;
157:
158: context.setPropertyResolved(false);
159: for (int i = 0; i < _resolvers.size(); i++) {
160: _resolvers.get(i).setValue(context, base, property, value);
161:
162: if (context.isPropertyResolved()) {
163: return;
164: }
165: }
166: }
167:
168: public String toString() {
169: return "EnvironmentELResolver[]";
170: }
171: }
|