KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > el > DefaultFunctionMapper


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.el;
16
17 import java.io.Externalizable JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInput JavaDoc;
20 import java.io.ObjectOutput JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.el.FunctionMapper;
27
28 import com.sun.el.util.ReflectionUtil;
29
30 /**
31  * Default implementation of the FunctionMapper
32  *
33  * @see java.lang.reflect.Method
34  * @see javax.el.FunctionMapper
35  *
36  * @author Jacob Hookom
37  * @version $Id: DefaultFunctionMapper.java,v 1.3 2005/08/24 04:38:56 jhook Exp $
38  */

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

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

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

84     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
85             ClassNotFoundException JavaDoc {
86         this.functions = (Map JavaDoc) in.readObject();
87     }
88     
89     public String JavaDoc toString() {
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(128);
91         sb.append("FunctionMapper[\n");
92         for (Iterator JavaDoc itr = this.functions.values().iterator(); itr.hasNext(); ) {
93             sb.append(itr.next()).append('\n');
94         }
95         sb.append(']');
96         return sb.toString();
97     }
98     
99     private static class Function implements Externalizable JavaDoc {
100     
101         private static final long serialVersionUID = 1L;
102         
103         protected transient Method JavaDoc m;
104         protected String JavaDoc owner;
105         protected String JavaDoc name;
106         protected String JavaDoc[] types;
107         protected String JavaDoc prefix;
108         protected String JavaDoc localName;
109     
110         /**
111          *
112          */

113         public Function(String JavaDoc prefix, String JavaDoc localName, Method JavaDoc m) {
114             if (localName == null) {
115                 throw new NullPointerException JavaDoc("LocalName cannot be null");
116             }
117             if (m == null) {
118                 throw new NullPointerException JavaDoc("Method cannot be null");
119             }
120             this.prefix = prefix;
121             this.localName = localName;
122             this.m = m;
123         }
124         
125         public Function() {
126             // for serialization
127
}
128     
129         /*
130          * (non-Javadoc)
131          *
132          * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
133          */

134         public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
135             out.writeUTF((this.prefix != null) ? this.prefix : "");
136             out.writeUTF(this.localName);
137             out.writeUTF(this.m.getDeclaringClass().getName());
138             out.writeUTF(this.m.getName());
139             out.writeObject(ReflectionUtil.toTypeNameArray(this.m.getParameterTypes()));
140         }
141     
142         /*
143          * (non-Javadoc)
144          *
145          * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
146          */

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

182         public boolean equals(Object JavaDoc obj) {
183             if (obj instanceof Function) {
184                 return this.hashCode() == obj.hashCode();
185             }
186             return false;
187         }
188         
189         /* (non-Javadoc)
190          * @see java.lang.Object#hashCode()
191          */

192         public int hashCode() {
193             return (this.prefix + this.localName).hashCode();
194         }
195         
196         public String JavaDoc toString() {
197             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(32);
198             sb.append("Function[");
199             if (this.prefix != null) {
200                 sb.append(this.prefix).append(':');
201             }
202             sb.append(this.name).append("] ");
203             sb.append(this.m);
204             return sb.toString();
205         }
206     }
207 }
208
Popular Tags