KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > runtime > ProtectedFunctionMapper


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

17
18 package org.apache.jasper.runtime;
19
20 import java.util.HashMap JavaDoc;
21 import java.security.AccessController JavaDoc;
22 import java.security.PrivilegedAction JavaDoc;
23 import java.security.PrivilegedExceptionAction JavaDoc;
24 import java.security.PrivilegedActionException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import javax.servlet.jsp.el.FunctionMapper JavaDoc;
27
28 import org.apache.jasper.security.SecurityUtil;
29
30 /**
31  * Maps EL functions to their Java method counterparts. Keeps the actual Method
32  * objects protected so that JSP pages can't indirectly do reflection.
33  *
34  * @author Mark Roth
35  * @author Kin-man Chung
36  */

37 public final class ProtectedFunctionMapper extends javax.el.FunctionMapper
38         implements FunctionMapper JavaDoc {
39
40     /**
41      * Maps "prefix:name" to java.lang.Method objects.
42      */

43     private HashMap JavaDoc fnmap = null;
44
45     /**
46      * If there is only one function in the map, this is the Method for it.
47      */

48     private Method JavaDoc theMethod = null;
49
50     /**
51      * Constructor has protected access.
52      */

53     private ProtectedFunctionMapper() {
54     }
55
56     /**
57      * Generated Servlet and Tag Handler implementations call this method to
58      * retrieve an instance of the ProtectedFunctionMapper. This is necessary
59      * since generated code does not have access to create instances of classes
60      * in this package.
61      *
62      * @return A new protected function mapper.
63      */

64     public static ProtectedFunctionMapper getInstance() {
65         ProtectedFunctionMapper funcMapper;
66         if (SecurityUtil.isPackageProtectionEnabled()) {
67             funcMapper = (ProtectedFunctionMapper) AccessController
68                     .doPrivileged(new PrivilegedAction JavaDoc() {
69                         public Object JavaDoc run() {
70                             return new ProtectedFunctionMapper();
71                         }
72                     });
73         } else {
74             funcMapper = new ProtectedFunctionMapper();
75         }
76         funcMapper.fnmap = new java.util.HashMap JavaDoc();
77         return funcMapper;
78     }
79
80     /**
81      * Stores a mapping from the given EL function prefix and name to the given
82      * Java method.
83      *
84      * @param fnQName
85      * The EL function qualified name (including prefix)
86      * @param c
87      * The class containing the Java method
88      * @param methodName
89      * The name of the Java method
90      * @param args
91      * The arguments of the Java method
92      * @throws RuntimeException
93      * if no method with the given signature could be found.
94      */

95     public void mapFunction(String JavaDoc fnQName, final Class JavaDoc c,
96             final String JavaDoc methodName, final Class JavaDoc[] args) {
97         java.lang.reflect.Method JavaDoc method;
98         if (SecurityUtil.isPackageProtectionEnabled()) {
99             try {
100                 method = (java.lang.reflect.Method JavaDoc) AccessController
101                         .doPrivileged(new PrivilegedExceptionAction JavaDoc() {
102
103                             public Object JavaDoc run() throws Exception JavaDoc {
104                                 return c.getDeclaredMethod(methodName, args);
105                             }
106                         });
107             } catch (PrivilegedActionException JavaDoc ex) {
108                 throw new RuntimeException JavaDoc(
109                         "Invalid function mapping - no such method: "
110                                 + ex.getException().getMessage());
111             }
112         } else {
113             try {
114                 method = c.getDeclaredMethod(methodName, args);
115             } catch (NoSuchMethodException JavaDoc e) {
116                 throw new RuntimeException JavaDoc(
117                         "Invalid function mapping - no such method: "
118                                 + e.getMessage());
119             }
120         }
121
122         this.fnmap.put(fnQName, method);
123     }
124
125     /**
126      * Creates an instance for this class, and stores the Method for the given
127      * EL function prefix and name. This method is used for the case when there
128      * is only one function in the EL expression.
129      *
130      * @param fnQName
131      * The EL function qualified name (including prefix)
132      * @param c
133      * The class containing the Java method
134      * @param methodName
135      * The name of the Java method
136      * @param args
137      * The arguments of the Java method
138      * @throws RuntimeException
139      * if no method with the given signature could be found.
140      */

141     public static ProtectedFunctionMapper getMapForFunction(String JavaDoc fnQName,
142             final Class JavaDoc c, final String JavaDoc methodName, final Class JavaDoc[] args) {
143         java.lang.reflect.Method JavaDoc method;
144         ProtectedFunctionMapper funcMapper;
145         if (SecurityUtil.isPackageProtectionEnabled()) {
146             funcMapper = (ProtectedFunctionMapper) AccessController
147                     .doPrivileged(new PrivilegedAction JavaDoc() {
148                         public Object JavaDoc run() {
149                             return new ProtectedFunctionMapper();
150                         }
151                     });
152
153             try {
154                 method = (java.lang.reflect.Method JavaDoc) AccessController
155                         .doPrivileged(new PrivilegedExceptionAction JavaDoc() {
156
157                             public Object JavaDoc run() throws Exception JavaDoc {
158                                 return c.getDeclaredMethod(methodName, args);
159                             }
160                         });
161             } catch (PrivilegedActionException JavaDoc ex) {
162                 throw new RuntimeException JavaDoc(
163                         "Invalid function mapping - no such method: "
164                                 + ex.getException().getMessage());
165             }
166         } else {
167             funcMapper = new ProtectedFunctionMapper();
168             try {
169                 method = c.getDeclaredMethod(methodName, args);
170             } catch (NoSuchMethodException JavaDoc e) {
171                 throw new RuntimeException JavaDoc(
172                         "Invalid function mapping - no such method: "
173                                 + e.getMessage());
174             }
175         }
176         funcMapper.theMethod = method;
177         return funcMapper;
178     }
179
180     /**
181      * Resolves the specified local name and prefix into a Java.lang.Method.
182      * Returns null if the prefix and local name are not found.
183      *
184      * @param prefix
185      * the prefix of the function
186      * @param localName
187      * the short name of the function
188      * @return the result of the method mapping. Null means no entry found.
189      */

190     public Method JavaDoc resolveFunction(String JavaDoc prefix, String JavaDoc localName) {
191         if (this.fnmap != null) {
192             return (Method JavaDoc) this.fnmap.get(prefix + ":" + localName);
193         }
194         return theMethod;
195     }
196 }
197
Popular Tags