KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > support > ExpressionEvaluatorManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.support;
18
19 import java.util.HashMap JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.Tag JavaDoc;
24
25 import org.apache.taglibs.standard.lang.jstl.Coercions;
26 import org.apache.taglibs.standard.lang.jstl.ELException;
27 import org.apache.taglibs.standard.lang.jstl.Logger;
28
29 /**
30  * <p>A conduit to the JSTL EL. Based on...</p>
31  *
32  * <p>An implementation of the ExpressionEvaluatorManager called for by
33  * the JSTL rev1 draft. This class is responsible for delegating a
34  * request for expression evaluating to the particular, "active"
35  * ExpressionEvaluator for the given point in the PageContext object
36  * passed in.</p>
37  *
38  * @author Shawn Bayern
39  */

40 public class ExpressionEvaluatorManager {
41
42     //*********************************************************************
43
// Constants
44

45     public static final String JavaDoc EVALUATOR_CLASS =
46         "org.apache.taglibs.standard.lang.jstl.Evaluator";
47     // private static final String EVALUATOR_PARAMETER =
48
// "javax.servlet.jsp.jstl.temp.ExpressionEvaluatorClass";
49

50     //*********************************************************************
51
// Internal, static state
52

53     private static HashMap JavaDoc nameMap = new HashMap JavaDoc();
54     private static Logger logger = new Logger(System.out);
55
56     //*********************************************************************
57
// Public static methods
58

59     /**
60      * Invokes the evaluate() method on the "active" ExpressionEvaluator
61      * for the given pageContext.
62      */

63     public static Object JavaDoc evaluate(String JavaDoc attributeName,
64                                   String JavaDoc expression,
65                                   Class JavaDoc expectedType,
66                                   Tag JavaDoc tag,
67                                   PageContext JavaDoc pageContext)
68            throws JspException JavaDoc
69     {
70
71         // the evaluator we'll use
72
ExpressionEvaluator target = getEvaluatorByName(EVALUATOR_CLASS);
73
74         // delegate the call
75
return (target.evaluate(
76             attributeName, expression, expectedType, tag, pageContext));
77     }
78
79     /**
80      * Invokes the evaluate() method on the "active" ExpressionEvaluator
81      * for the given pageContext.
82      */

83     public static Object JavaDoc evaluate(String JavaDoc attributeName,
84                                   String JavaDoc expression,
85                                   Class JavaDoc expectedType,
86                                   PageContext JavaDoc pageContext)
87            throws JspException JavaDoc
88     {
89
90         // the evaluator we'll use
91
ExpressionEvaluator target = getEvaluatorByName(EVALUATOR_CLASS);
92
93         // delegate the call
94
return (target.evaluate(
95             attributeName, expression, expectedType, null, pageContext));
96     }
97
98     /**
99      * Gets an ExpressionEvaluator from the cache, or seeds the cache
100      * if we haven't seen a particular ExpressionEvaluator before.
101      */

102     public static
103         ExpressionEvaluator getEvaluatorByName(String JavaDoc name)
104             throws JspException JavaDoc {
105
106         Object JavaDoc oEvaluator = nameMap.get(name);
107         if (oEvaluator != null) {
108             return ((ExpressionEvaluator) oEvaluator);
109         }
110         try {
111             synchronized (nameMap) {
112                 oEvaluator = nameMap.get(name);
113                 if (oEvaluator != null) {
114                     return ((ExpressionEvaluator) oEvaluator);
115                 }
116                 ExpressionEvaluator e = (ExpressionEvaluator)
117                     Class.forName(name).newInstance();
118                 nameMap.put(name, e);
119                 return (e);
120             }
121         } catch (ClassCastException JavaDoc ex) {
122             // just to display a better error message
123
throw new JspException JavaDoc("invalid ExpressionEvaluator: " +
124                 ex.toString(), ex);
125         } catch (ClassNotFoundException JavaDoc ex) {
126             throw new JspException JavaDoc("couldn't find ExpressionEvaluator: " +
127                 ex.toString(), ex);
128         } catch (IllegalAccessException JavaDoc ex) {
129             throw new JspException JavaDoc("couldn't access ExpressionEvaluator: " +
130                 ex.toString(), ex);
131         } catch (InstantiationException JavaDoc ex) {
132             throw new JspException JavaDoc(
133                 "couldn't instantiate ExpressionEvaluator: " +
134                 ex.toString(), ex);
135         }
136     }
137
138     /** Performs a type conversion according to the EL's rules. */
139     public static Object JavaDoc coerce(Object JavaDoc value, Class JavaDoc classe)
140             throws JspException JavaDoc {
141     try {
142         // just delegate the call
143
return Coercions.coerce(value, classe, logger);
144     } catch (ELException ex) {
145         throw new JspException JavaDoc(ex);
146     }
147     }
148
149 }
150
Popular Tags