001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package org.apache.taglibs.jsptl.lang.support;
057:
058: import java.util.*;
059: import javax.servlet.*;
060: import javax.servlet.jsp.*;
061: import javax.servlet.jsp.tagext.*;
062:
063: /**
064: * <p>An implementation of the ExpressionEvaluatorManager called for by
065: * the JSPTL rev1 draft. This class is responsible for delegating a
066: * request for expression evaluating to the particular, "active"
067: * ExpressionEvaluator for the given point in the PageContext object
068: * passed in.</p>
069: *
070: * <p><b>WARNING</b>: This class supports experimentation for the EA1
071: * release of JSPTL; it is not expected to be part of the final RI or
072: * specification.</p>
073: *
074: * @author Shawn Bayern
075: */
076: public class ExpressionEvaluatorManager {
077:
078: //*********************************************************************
079: // Implementation overview
080:
081: /*
082: * We use a simple static table that indexes the currently active
083: * ExpressionEvaluator (EE) by a PageContext object. Our strategy
084: * is straightforward: we keep a java.util.Stack of
085: * ExpressionEvaluator objects for each PageContext, and if the
086: * stack is empty, we use the context parameter called for by the
087: * spec.
088: */
089:
090: //*********************************************************************
091: // Constants
092: private static final String EVALUATOR_PARAMETER = "javax.servlet.jsptl.ExpressionEvaluatorClass";
093: private static final String DEFAULT_EVALUATOR_CLASS = "org.apache.taglibs.jsptl.lang.spel.Evaluator";
094:
095: //*********************************************************************
096: // Internal, static state
097:
098: /** The static table */
099: private static HashMap eeTab = new HashMap();
100:
101: /** A separate static table to avoid unnecessary duplication of objects */
102: private static HashMap nameMap = new HashMap();
103:
104: //*********************************************************************
105: // Public static methods
106:
107: /**
108: * Establishes the given ExpressionEvaluator as the "active" evaluator,
109: * preserving the stack of formerly actiev evaluators underneath it.
110: */
111: public static synchronized void pushEvaluator(
112: PageContext pageContext, String expressionEvaluatorName)
113: throws JspException {
114:
115: // we'll use these throughout...
116: Stack s;
117: ExpressionEvaluator e;
118:
119: // create a Stack if one doesn't exist for our page
120: Object oStack = eeTab.get(pageContext);
121: if (oStack == null) {
122: s = new Stack();
123: eeTab.put(pageContext, s);
124: } else
125: s = (Stack) oStack;
126:
127: // "install" the evaluator
128: e = getEvaluatorByName(expressionEvaluatorName);
129: s.push(e);
130: }
131:
132: /**
133: * Retires the most recent ExpressionEvaluator from "activity," restoring
134: * the one immediately preceding it on the stack.
135: */
136: public static synchronized void popEvaluator(PageContext pageContext) {
137:
138: // sanity check
139: Object oStack = eeTab.get(pageContext);
140: if (oStack == null) {
141: throw new IllegalStateException("popEvaluator() called "
142: + "on empty stack");
143: }
144:
145: // remove the most recent evaluator
146: Stack s = (Stack) oStack;
147: s.pop();
148:
149: // if there are none left, forget about the stack
150: if (s.empty())
151: eeTab.remove(pageContext);
152: }
153:
154: /**
155: * Invokes the evaluate() method on the "active" ExpressionEvaluator
156: * for the given pageContext.
157: */
158: public static Object evaluate(String attributeName,
159: String expression, Class expectedType, Tag tag,
160: PageContext pageContext) throws JspException {
161:
162: // the evaluator we'll use
163: ExpressionEvaluator target;
164:
165: // figure out what evaluator to use, under a static lock
166: synchronized (ExpressionEvaluatorManager.class) {
167: Object oStack = eeTab.get(pageContext);
168: if (oStack == null || ((Stack) oStack).empty()) {
169: String name = pageContext.getServletContext()
170: .getInitParameter(EVALUATOR_PARAMETER);
171: if (name == null) {
172: target = getEvaluatorByName(DEFAULT_EVALUATOR_CLASS);
173: //throw new JspException("request to evaluate " +
174: // "expression but no expression language defined " +
175: // "(expression: '" + expression + "'; " +
176: // "attribute name: '" + attributeName + "')");
177: } else
178: target = getEvaluatorByName(name);
179: } else {
180: Stack s = (Stack) oStack;
181: target = (ExpressionEvaluator) s.peek();
182: }
183: }
184:
185: // delegate the call
186: return (target.evaluate(attributeName, expression,
187: expectedType, tag, pageContext));
188: }
189:
190: //*********************************************************************
191: // Public static utility method (un-spec'd, local to RI)
192:
193: /**
194: * Gets an ExpressionEvaluator from the cache, or seeds the cache
195: * if we haven't seen a particular ExpressionEvaluator before.
196: */
197: public static synchronized ExpressionEvaluator getEvaluatorByName(
198: String name) throws JspException {
199: try {
200:
201: Object oEvaluator = nameMap.get(name);
202: if (oEvaluator == null) {
203: ExpressionEvaluator e = (ExpressionEvaluator) Class
204: .forName(name).newInstance();
205: nameMap.put(name, e);
206: return (e);
207: } else
208: return ((ExpressionEvaluator) oEvaluator);
209:
210: } catch (ClassCastException ex) {
211: // just to display a better error message
212: throw new JspException("invalid ExpressionEvaluator: "
213: + ex.getMessage());
214: } catch (ClassNotFoundException ex) {
215: throw new JspException(
216: "couldn't find ExpressionEvaluator: "
217: + ex.getMessage());
218: } catch (IllegalAccessException ex) {
219: throw new JspException(
220: "couldn't access ExpressionEvaluator: "
221: + ex.getMessage());
222: } catch (InstantiationException ex) {
223: throw new JspException(
224: "couldn't instantiate ExpressionEvaluator: "
225: + ex.getMessage());
226: }
227: }
228:
229: }
|