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 javax.el.ELContext;
033: import javax.el.ELResolver;
034: import java.beans.FeatureDescriptor;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037:
038: /**
039: * Stack-based composite resolver,
040: */
041: public class StackELResolver extends ELResolver {
042: public final ArrayList<ELResolver> _resolverStack = new ArrayList<ELResolver>();
043:
044: public StackELResolver() {
045: }
046:
047: public StackELResolver(ELResolver a, ELResolver b) {
048: push(b);
049: push(a);
050: }
051:
052: public void push(ELResolver elResolver) {
053: if (elResolver == null)
054: throw new NullPointerException();
055:
056: _resolverStack.add(elResolver);
057: }
058:
059: public ELResolver pop() {
060: if (_resolverStack.size() > 0)
061: return _resolverStack.remove(_resolverStack.size() - 1);
062: else
063: return null;
064: }
065:
066: @Override
067: public Class<?> getCommonPropertyType(ELContext env, Object base) {
068: for (int i = _resolverStack.size() - 1; i >= 0; i--) {
069: ELResolver resolver = _resolverStack.get(i);
070:
071: return getCommonPropertyType(env, base);
072: }
073:
074: return null;
075: }
076:
077: @Override
078: public Iterator<FeatureDescriptor> getFeatureDescriptors(
079: ELContext env, Object base) {
080: ArrayList<FeatureDescriptor> descriptors = null;
081:
082: for (int i = _resolverStack.size() - 1; i >= 0; i--) {
083: ELResolver resolver = _resolverStack.get(i);
084:
085: Iterator<FeatureDescriptor> iter = resolver
086: .getFeatureDescriptors(env, base);
087:
088: if (iter == null)
089: continue;
090:
091: if (descriptors == null)
092: descriptors = new ArrayList<FeatureDescriptor>();
093:
094: while (iter.hasNext()) {
095: FeatureDescriptor desc = iter.next();
096:
097: descriptors.add(desc);
098: }
099: }
100:
101: if (descriptors != null)
102: return descriptors.iterator();
103: else
104: return null;
105: }
106:
107: @Override
108: public Class<?> getType(ELContext context, Object base,
109: Object property) {
110: context.setPropertyResolved(false);
111:
112: for (int i = _resolverStack.size() - 1; i >= 0; i--) {
113: ELResolver resolver = _resolverStack.get(i);
114:
115: Class type = resolver.getType(context, base, property);
116:
117: if (context.isPropertyResolved())
118: return type;
119: }
120:
121: return null;
122: }
123:
124: @Override
125: public Object getValue(ELContext context, Object base,
126: Object property) {
127: context.setPropertyResolved(false);
128:
129: for (int i = _resolverStack.size() - 1; i >= 0; i--) {
130: ELResolver resolver = _resolverStack.get(i);
131:
132: Object value = resolver.getValue(context, base, property);
133:
134: if (context.isPropertyResolved()) {
135: return value;
136: }
137: }
138:
139: return null;
140: }
141:
142: @Override
143: public boolean isReadOnly(ELContext context, Object base,
144: Object property) {
145: context.setPropertyResolved(false);
146:
147: for (int i = _resolverStack.size() - 1; i >= 0; i--) {
148: ELResolver resolver = _resolverStack.get(i);
149:
150: boolean isReadOnly = resolver.isReadOnly(context, base,
151: property);
152:
153: if (context.isPropertyResolved())
154: return isReadOnly;
155: }
156:
157: return false;
158: }
159:
160: @Override
161: public void setValue(ELContext context, Object base,
162: Object property, Object value) {
163: context.setPropertyResolved(false);
164:
165: for (int i = _resolverStack.size() - 1; i >= 0; i--) {
166: ELResolver resolver = _resolverStack.get(i);
167:
168: resolver.setValue(context, base, property, value);
169:
170: if (context.isPropertyResolved()) {
171: return;
172: }
173: }
174: }
175: }
|