KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > lang > FunctionMapperImpl


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.el.lang;
19
20 import java.io.Externalizable JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInput JavaDoc;
23 import java.io.ObjectOutput JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.el.FunctionMapper;
29
30 import org.apache.el.util.ReflectionUtil;
31
32
33 /**
34  * @author Jacob Hookom [jacob@hookom.net]
35  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
36  */

37 public class FunctionMapperImpl extends FunctionMapper implements
38         Externalizable JavaDoc {
39
40     private static final long serialVersionUID = 1L;
41     
42     protected Map JavaDoc functions = null;
43
44     /*
45      * (non-Javadoc)
46      *
47      * @see javax.el.FunctionMapper#resolveFunction(java.lang.String,
48      * java.lang.String)
49      */

50     public Method JavaDoc resolveFunction(String JavaDoc prefix, String JavaDoc localName) {
51         if (this.functions != null) {
52             Function f = (Function) this.functions.get(prefix + ":" + localName);
53             return f.getMethod();
54         }
55         return null;
56     }
57
58     public void addFunction(String JavaDoc prefix, String JavaDoc localName, Method JavaDoc m) {
59         if (this.functions == null) {
60             this.functions = new HashMap JavaDoc();
61         }
62         Function f = new Function(prefix, localName, m);
63         synchronized (this) {
64             this.functions.put(prefix+":"+localName, f);
65         }
66     }
67
68     /*
69      * (non-Javadoc)
70      *
71      * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
72      */

73     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
74         out.writeObject(this.functions);
75     }
76
77     /*
78      * (non-Javadoc)
79      *
80      * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
81      */

82     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
83             ClassNotFoundException JavaDoc {
84         this.functions = (Map JavaDoc) in.readObject();
85     }
86     
87     public static class Function implements Externalizable JavaDoc {
88     
89         protected transient Method JavaDoc m;
90         protected String JavaDoc owner;
91         protected String JavaDoc name;
92         protected String JavaDoc[] types;
93         protected String JavaDoc prefix;
94         protected String JavaDoc localName;
95     
96         /**
97          *
98          */

99         public Function(String JavaDoc prefix, String JavaDoc localName, Method JavaDoc m) {
100             if (localName == null) {
101                 throw new NullPointerException JavaDoc("LocalName cannot be null");
102             }
103             if (m == null) {
104                 throw new NullPointerException JavaDoc("Method cannot be null");
105             }
106             this.prefix = prefix;
107             this.localName = localName;
108             this.m = m;
109         }
110         
111         public Function() {
112             // for serialization
113
}
114     
115         /*
116          * (non-Javadoc)
117          *
118          * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
119          */

120         public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
121             out.writeUTF((this.prefix != null) ? this.prefix : "");
122             out.writeUTF(this.localName);
123             out.writeUTF(this.m.getDeclaringClass().getName());
124             out.writeUTF(this.m.getName());
125             out.writeObject(ReflectionUtil.toTypeNameArray(this.m.getParameterTypes()));
126         }
127     
128         /*
129          * (non-Javadoc)
130          *
131          * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
132          */

133         public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
134                 ClassNotFoundException JavaDoc {
135             
136             this.prefix = in.readUTF();
137             if ("".equals(this.prefix)) this.prefix = null;
138             this.localName = in.readUTF();
139             this.owner = in.readUTF();
140             this.name = in.readUTF();
141             this.types = (String JavaDoc[]) in.readObject();
142         }
143     
144         public Method JavaDoc getMethod() {
145             if (this.m == null) {
146                 try {
147                     Class JavaDoc t = Class.forName(this.owner);
148                     Class JavaDoc[] p = ReflectionUtil.toTypeArray(this.types);
149                     this.m = t.getMethod(this.name, p);
150                 } catch (Exception JavaDoc e) {
151                     e.printStackTrace();
152                 }
153             }
154             return this.m;
155         }
156         
157         public boolean matches(String JavaDoc prefix, String JavaDoc localName) {
158             if (this.prefix != null) {
159                 if (prefix == null) return false;
160                 if (!this.prefix.equals(prefix)) return false;
161             }
162             return this.localName.equals(localName);
163         }
164     
165         /* (non-Javadoc)
166          * @see java.lang.Object#equals(java.lang.Object)
167          */

168         public boolean equals(Object JavaDoc obj) {
169             if (obj instanceof Function) {
170                 return this.hashCode() == obj.hashCode();
171             }
172             return false;
173         }
174         
175         /* (non-Javadoc)
176          * @see java.lang.Object#hashCode()
177          */

178         public int hashCode() {
179             return (this.prefix + this.localName).hashCode();
180         }
181     }
182
183 }
184
Popular Tags