1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.el; |
20 | |
|
21 | |
import java.io.Externalizable; |
22 | |
import java.io.IOException; |
23 | |
import java.io.ObjectInput; |
24 | |
import java.io.ObjectOutput; |
25 | |
import java.lang.reflect.Method; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import javax.el.FunctionMapper; |
30 | |
|
31 | |
import org.apache.myfaces.view.facelets.util.ReflectionUtil; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public final class DefaultFunctionMapper extends FunctionMapper implements Externalizable |
43 | |
{ |
44 | |
private static final long serialVersionUID = 1L; |
45 | |
|
46 | 0 | private Map<String, Function> _functions = null; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public Method resolveFunction(String prefix, String localName) |
54 | |
{ |
55 | 0 | if (_functions != null) |
56 | |
{ |
57 | 0 | Function f = (Function) _functions.get(prefix + ":" + localName); |
58 | 0 | return f.getMethod(); |
59 | |
} |
60 | |
|
61 | 0 | return null; |
62 | |
} |
63 | |
|
64 | |
public void addFunction(String prefix, String localName, Method m) |
65 | |
{ |
66 | 0 | if (_functions == null) |
67 | |
{ |
68 | 0 | _functions = new HashMap<String, Function>(); |
69 | |
} |
70 | |
|
71 | 0 | Function f = new Function(prefix, localName, m); |
72 | 0 | synchronized (this) |
73 | |
{ |
74 | 0 | _functions.put(prefix + ":" + localName, f); |
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void writeExternal(ObjectOutput out) throws IOException |
84 | |
{ |
85 | 0 | out.writeObject(_functions); |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@SuppressWarnings("unchecked") |
94 | |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException |
95 | |
{ |
96 | 0 | _functions = (Map<String, Function>) in.readObject(); |
97 | 0 | } |
98 | |
|
99 | |
@Override |
100 | |
public String toString() |
101 | |
{ |
102 | 0 | StringBuffer sb = new StringBuffer(128); |
103 | 0 | sb.append("FunctionMapper[\n"); |
104 | 0 | for (Function function : _functions.values()) |
105 | |
{ |
106 | 0 | sb.append(function).append('\n'); |
107 | 0 | } |
108 | 0 | sb.append(']'); |
109 | |
|
110 | 0 | return sb.toString(); |
111 | |
} |
112 | |
|
113 | 0 | private static class Function implements Externalizable |
114 | |
{ |
115 | |
private static final long serialVersionUID = 1L; |
116 | |
|
117 | |
protected transient Method _m; |
118 | |
|
119 | |
protected String _owner; |
120 | |
|
121 | |
protected String _name; |
122 | |
|
123 | |
protected String[] _types; |
124 | |
|
125 | |
protected String _prefix; |
126 | |
|
127 | |
protected String _localName; |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public Function(String prefix, String localName, Method m) |
133 | 0 | { |
134 | 0 | if (localName == null) |
135 | |
{ |
136 | 0 | throw new NullPointerException("LocalName cannot be null"); |
137 | |
} |
138 | 0 | if (m == null) |
139 | |
{ |
140 | 0 | throw new NullPointerException("Method cannot be null"); |
141 | |
} |
142 | |
|
143 | 0 | _prefix = prefix; |
144 | 0 | _localName = localName; |
145 | 0 | _m = m; |
146 | 0 | } |
147 | |
|
148 | |
public Function() |
149 | 0 | { |
150 | |
|
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void writeExternal(ObjectOutput out) throws IOException |
159 | |
{ |
160 | 0 | out.writeUTF(_prefix != null ? _prefix : ""); |
161 | 0 | out.writeUTF(_localName); |
162 | 0 | out.writeUTF(_m.getDeclaringClass().getName()); |
163 | 0 | out.writeUTF(_m.getName()); |
164 | 0 | out.writeObject(ReflectionUtil.toTypeNameArray(this._m.getParameterTypes())); |
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException |
173 | |
{ |
174 | 0 | _prefix = in.readUTF(); |
175 | 0 | if ("".equals(_prefix)) |
176 | |
{ |
177 | 0 | _prefix = null; |
178 | |
} |
179 | |
|
180 | 0 | _localName = in.readUTF(); |
181 | 0 | _owner = in.readUTF(); |
182 | 0 | _name = in.readUTF(); |
183 | 0 | _types = (String[]) in.readObject(); |
184 | 0 | } |
185 | |
|
186 | |
public Method getMethod() |
187 | |
{ |
188 | 0 | if (_m == null) |
189 | |
{ |
190 | |
try |
191 | |
{ |
192 | 0 | Class<?> t = ReflectionUtil.forName(_owner); |
193 | 0 | Class<?>[] p = ReflectionUtil.toTypeArray(_types); |
194 | 0 | _m = t.getMethod(_name, p); |
195 | |
} |
196 | 0 | catch (Exception e) |
197 | |
{ |
198 | 0 | e.printStackTrace(); |
199 | 0 | } |
200 | |
} |
201 | |
|
202 | 0 | return _m; |
203 | |
} |
204 | |
|
205 | |
public boolean matches(String prefix, String localName) |
206 | |
{ |
207 | 0 | if (_prefix != null) |
208 | |
{ |
209 | 0 | if (prefix == null) |
210 | |
{ |
211 | 0 | return false; |
212 | |
} |
213 | 0 | if (!_prefix.equals(prefix)) |
214 | |
{ |
215 | 0 | return false; |
216 | |
} |
217 | |
} |
218 | |
|
219 | 0 | return _localName.equals(localName); |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
@Override |
228 | |
public boolean equals(Object obj) |
229 | |
{ |
230 | 0 | if (obj instanceof Function) |
231 | |
{ |
232 | 0 | return hashCode() == obj.hashCode(); |
233 | |
} |
234 | |
|
235 | 0 | return false; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
@Override |
244 | |
public int hashCode() |
245 | |
{ |
246 | 0 | return (_prefix + _localName).hashCode(); |
247 | |
} |
248 | |
|
249 | |
@Override |
250 | |
public String toString() |
251 | |
{ |
252 | 0 | StringBuffer sb = new StringBuffer(32); |
253 | 0 | sb.append("Function["); |
254 | 0 | if (_prefix != null) |
255 | |
{ |
256 | 0 | sb.append(_prefix).append(':'); |
257 | |
} |
258 | 0 | sb.append(_name).append("] "); |
259 | 0 | sb.append(_m); |
260 | |
|
261 | 0 | return sb.toString(); |
262 | |
} |
263 | |
} |
264 | |
} |