KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > lang > FunctionMapperImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package com.sun.el.lang;
26
27 import java.io.Externalizable JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInput JavaDoc;
30 import java.io.ObjectOutput JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.el.FunctionMapper;
36
37 import com.sun.el.util.ReflectionUtil;
38
39 /**
40  * @author Jacob Hookom [jacob@hookom.net]
41  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
42  */

43 public class FunctionMapperImpl extends FunctionMapper implements
44         Externalizable JavaDoc {
45
46     private static final long serialVersionUID = 1L;
47     
48     protected Map JavaDoc functions = null;
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see javax.el.FunctionMapper#resolveFunction(java.lang.String,
54      * java.lang.String)
55      */

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

79     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
80         out.writeObject(this.functions);
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
87      */

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

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

126         public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
127             
128             out.writeUTF((this.prefix != null) ? this.prefix : "");
129             out.writeUTF(this.localName);
130             
131             if (this.owner != null) {
132                 out.writeUTF(this.owner);
133             } else {
134                 out.writeUTF(this.m.getDeclaringClass().getName());
135             }
136             if (this.name != null) {
137                 out.writeUTF(this.name);
138             } else {
139                 out.writeUTF(this.m.getName());
140             }
141             if (this.types != null) {
142                 out.writeObject(this.types);
143             } else {
144                 out.writeObject(ReflectionUtil.toTypeNameArray(this.m.getParameterTypes()));
145             }
146         }
147     
148         /*
149          * (non-Javadoc)
150          *
151          * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
152          */

153         public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
154                 ClassNotFoundException JavaDoc {
155             
156             this.prefix = in.readUTF();
157             if ("".equals(this.prefix)) this.prefix = null;
158             this.localName = in.readUTF();
159             this.owner = in.readUTF();
160             this.name = in.readUTF();
161             this.types = (String JavaDoc[]) in.readObject();
162         }
163     
164         public Method JavaDoc getMethod() {
165             if (this.m == null) {
166                 try {
167                     Class JavaDoc t = Class.forName(this.owner);
168                     Class JavaDoc[] p = ReflectionUtil.toTypeArray(this.types);
169                     this.m = t.getMethod(this.name, p);
170                 } catch (Exception JavaDoc e) {
171                     e.printStackTrace();
172                 }
173             }
174             return this.m;
175         }
176         
177         public boolean matches(String JavaDoc prefix, String JavaDoc localName) {
178             if (this.prefix != null) {
179                 if (prefix == null) return false;
180                 if (!this.prefix.equals(prefix)) return false;
181             }
182             return this.localName.equals(localName);
183         }
184     
185         /* (non-Javadoc)
186          * @see java.lang.Object#equals(java.lang.Object)
187          */

188         public boolean equals(Object JavaDoc obj) {
189             if (obj instanceof Function) {
190                 return this.hashCode() == obj.hashCode();
191             }
192             return false;
193         }
194         
195         /* (non-Javadoc)
196          * @see java.lang.Object#hashCode()
197          */

198         public int hashCode() {
199             return (this.prefix + this.localName).hashCode();
200         }
201     }
202
203 }
204
Popular Tags