001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.lang.support;
040:
041: import java.util.HashMap;
042:
043: import javax.servlet.jsp.JspException;
044: import javax.servlet.jsp.PageContext;
045: import javax.servlet.jsp.tagext.Tag;
046:
047: import org.apache.taglibs.standard.lang.jstl.Coercions;
048: import org.apache.taglibs.standard.lang.jstl.ELException;
049: import org.apache.taglibs.standard.lang.jstl.Logger;
050:
051: /**
052: * <p>A conduit to the JSTL EL. Based on...</p>
053: *
054: * <p>An implementation of the ExpressionEvaluatorManager called for by
055: * the JSTL rev1 draft. This class is responsible for delegating a
056: * request for expression evaluating to the particular, "active"
057: * ExpressionEvaluator for the given point in the PageContext object
058: * passed in.</p>
059: *
060: * @author Shawn Bayern
061: */
062: public class ExpressionEvaluatorManager {
063:
064: //*********************************************************************
065: // Constants
066:
067: public static final String EVALUATOR_CLASS = "org.apache.taglibs.standard.lang.jstl.Evaluator";
068: // private static final String EVALUATOR_PARAMETER =
069: // "javax.servlet.jsp.jstl.temp.ExpressionEvaluatorClass";
070:
071: //*********************************************************************
072: // Internal, static state
073:
074: private static HashMap nameMap = new HashMap();
075: private static Logger logger = new Logger(System.out);
076:
077: //*********************************************************************
078: // Public static methods
079:
080: /**
081: * Invokes the evaluate() method on the "active" ExpressionEvaluator
082: * for the given pageContext.
083: */
084: public static Object evaluate(String attributeName,
085: String expression, Class expectedType, Tag tag,
086: PageContext pageContext) throws JspException {
087:
088: // the evaluator we'll use
089: ExpressionEvaluator target = getEvaluatorByName(EVALUATOR_CLASS);
090:
091: // delegate the call
092: return (target.evaluate(attributeName, expression,
093: expectedType, tag, pageContext));
094: }
095:
096: /**
097: * Invokes the evaluate() method on the "active" ExpressionEvaluator
098: * for the given pageContext.
099: */
100: public static Object evaluate(String attributeName,
101: String expression, Class expectedType,
102: PageContext pageContext) throws JspException {
103:
104: // the evaluator we'll use
105: ExpressionEvaluator target = getEvaluatorByName(EVALUATOR_CLASS);
106:
107: // delegate the call
108: return (target.evaluate(attributeName, expression,
109: expectedType, null, pageContext));
110: }
111:
112: /**
113: * Gets an ExpressionEvaluator from the cache, or seeds the cache
114: * if we haven't seen a particular ExpressionEvaluator before.
115: */
116: public static ExpressionEvaluator getEvaluatorByName(String name)
117: throws JspException {
118:
119: Object oEvaluator = nameMap.get(name);
120: if (oEvaluator != null) {
121: return ((ExpressionEvaluator) oEvaluator);
122: }
123: try {
124: synchronized (nameMap) {
125: oEvaluator = nameMap.get(name);
126: if (oEvaluator != null) {
127: return ((ExpressionEvaluator) oEvaluator);
128: }
129: ExpressionEvaluator e = (ExpressionEvaluator) Class
130: .forName(name).newInstance();
131: nameMap.put(name, e);
132: return (e);
133: }
134: } catch (ClassCastException ex) {
135: // just to display a better error message
136: throw new JspException("invalid ExpressionEvaluator: "
137: + ex.toString(), ex);
138: } catch (ClassNotFoundException ex) {
139: throw new JspException(
140: "couldn't find ExpressionEvaluator: "
141: + ex.toString(), ex);
142: } catch (IllegalAccessException ex) {
143: throw new JspException(
144: "couldn't access ExpressionEvaluator: "
145: + ex.toString(), ex);
146: } catch (InstantiationException ex) {
147: throw new JspException(
148: "couldn't instantiate ExpressionEvaluator: "
149: + ex.toString(), ex);
150: }
151: }
152:
153: /** Performs a type conversion according to the EL's rules. */
154: public static Object coerce(Object value, Class classe)
155: throws JspException {
156: try {
157: // just delegate the call
158: return Coercions.coerce(value, classe, logger);
159: } catch (ELException ex) {
160: throw new JspException(ex);
161: }
162: }
163:
164: }
|