|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.listener; |
|
16 |
| |
|
17 |
| import java.lang.reflect.Method; |
|
18 |
| import java.lang.reflect.Modifier; |
|
19 |
| import java.util.ArrayList; |
|
20 |
| import java.util.Arrays; |
|
21 |
| import java.util.Comparator; |
|
22 |
| import java.util.HashMap; |
|
23 |
| import java.util.Iterator; |
|
24 |
| import java.util.List; |
|
25 |
| import java.util.Map; |
|
26 |
| |
|
27 |
| import org.apache.hivemind.util.Defense; |
|
28 |
| import org.apache.tapestry.IPage; |
|
29 |
| import org.apache.tapestry.engine.ILink; |
|
30 |
| import org.apache.tapestry.event.ResetEventListener; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| public class ListenerMapSourceImpl implements ListenerMapSource, ResetEventListener |
|
37 |
| { |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| private static class ParameterCountComparator implements Comparator |
|
43 |
| { |
|
44 |
30120
| public int compare(Object o1, Object o2)
|
|
45 |
| { |
|
46 |
30120
| Method m1 = (Method) o1;
|
|
47 |
30120
| Method m2 = (Method) o2;
|
|
48 |
| |
|
49 |
30120
| return m2.getParameterTypes().length - m1.getParameterTypes().length;
|
|
50 |
| } |
|
51 |
| |
|
52 |
| } |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| private final Map _classToInvokerMap = new HashMap(); |
|
60 |
| |
|
61 |
105
| public ListenerMap getListenerMapForObject(Object object)
|
|
62 |
| { |
|
63 |
105
| Defense.notNull(object, "object");
|
|
64 |
| |
|
65 |
105
| Class objectClass = object.getClass();
|
|
66 |
| |
|
67 |
105
| Map invokerMap = findInvokerMap(objectClass);
|
|
68 |
| |
|
69 |
105
| return new ListenerMapImpl(object, invokerMap);
|
|
70 |
| } |
|
71 |
| |
|
72 |
0
| public synchronized void resetEventDidOccur()
|
|
73 |
| { |
|
74 |
0
| _classToInvokerMap.clear();
|
|
75 |
| } |
|
76 |
| |
|
77 |
105
| private synchronized Map findInvokerMap(Class targetClass)
|
|
78 |
| { |
|
79 |
105
| Map result = (Map) _classToInvokerMap.get(targetClass);
|
|
80 |
| |
|
81 |
105
| if (result == null)
|
|
82 |
| { |
|
83 |
99
| result = buildInvokerMapForClass(targetClass);
|
|
84 |
99
| _classToInvokerMap.put(targetClass, result);
|
|
85 |
| } |
|
86 |
| |
|
87 |
105
| return result;
|
|
88 |
| } |
|
89 |
| |
|
90 |
99
| private Map buildInvokerMapForClass(Class targetClass)
|
|
91 |
| { |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
99
| Map map = new HashMap();
|
|
97 |
| |
|
98 |
99
| Method[] methods = targetClass.getMethods();
|
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
99
| Arrays.sort(methods, new ParameterCountComparator());
|
|
105 |
| |
|
106 |
99
| for (int i = 0; i < methods.length; i++)
|
|
107 |
| { |
|
108 |
6957
| Method m = methods[i];
|
|
109 |
| |
|
110 |
6957
| if (!isAcceptibleListenerMethodReturnType(m))
|
|
111 |
2343
| continue;
|
|
112 |
| |
|
113 |
4614
| if (Modifier.isStatic(m.getModifiers()))
|
|
114 |
24
| continue;
|
|
115 |
| |
|
116 |
4590
| String name = m.getName();
|
|
117 |
| |
|
118 |
4590
| addMethodToMappedList(map, m, name);
|
|
119 |
| } |
|
120 |
| |
|
121 |
99
| return convertMethodListMapToInvokerMap(map);
|
|
122 |
| } |
|
123 |
| |
|
124 |
6975
| boolean isAcceptibleListenerMethodReturnType(Method m)
|
|
125 |
| { |
|
126 |
6975
| Class returnType = m.getReturnType();
|
|
127 |
| |
|
128 |
6975
| if (returnType == void.class || returnType == String.class)
|
|
129 |
4455
| return true;
|
|
130 |
| |
|
131 |
2520
| return IPage.class.isAssignableFrom(returnType) || ILink.class.isAssignableFrom(returnType);
|
|
132 |
| } |
|
133 |
| |
|
134 |
99
| private Map convertMethodListMapToInvokerMap(Map map)
|
|
135 |
| { |
|
136 |
99
| Map result = new HashMap();
|
|
137 |
| |
|
138 |
99
| Iterator i = map.entrySet().iterator();
|
|
139 |
99
| while (i.hasNext())
|
|
140 |
| { |
|
141 |
4158
| Map.Entry e = (Map.Entry) i.next();
|
|
142 |
| |
|
143 |
4158
| String name = (String) e.getKey();
|
|
144 |
4158
| List methodList = (List) e.getValue();
|
|
145 |
| |
|
146 |
4158
| Method[] methods = convertMethodListToArray(methodList);
|
|
147 |
| |
|
148 |
4158
| ListenerMethodInvoker invoker = createListenerMethodInvoker(name, methods);
|
|
149 |
| |
|
150 |
4158
| result.put(name, invoker);
|
|
151 |
| } |
|
152 |
| |
|
153 |
99
| return result;
|
|
154 |
| } |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
4158
| protected ListenerMethodInvoker createListenerMethodInvoker(String name, Method[] methods)
|
|
162 |
| { |
|
163 |
4158
| return new ListenerMethodInvokerImpl(name, methods);
|
|
164 |
| } |
|
165 |
| |
|
166 |
4158
| private Method[] convertMethodListToArray(List methodList)
|
|
167 |
| { |
|
168 |
4158
| int size = methodList.size();
|
|
169 |
4158
| Method[] result = new Method[size];
|
|
170 |
| |
|
171 |
4158
| return (Method[]) methodList.toArray(result);
|
|
172 |
| } |
|
173 |
| |
|
174 |
4590
| private void addMethodToMappedList(Map map, Method m, String name)
|
|
175 |
| { |
|
176 |
4590
| List l = (List) map.get(name);
|
|
177 |
| |
|
178 |
4590
| if (l == null)
|
|
179 |
| { |
|
180 |
4158
| l = new ArrayList();
|
|
181 |
4158
| map.put(name, l);
|
|
182 |
| } |
|
183 |
| |
|
184 |
4590
| l.add(m);
|
|
185 |
| } |
|
186 |
| } |