1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.lib.util; |
16 |
| |
17 |
| import java.util.HashMap; |
18 |
| import java.util.Iterator; |
19 |
| import java.util.LinkedList; |
20 |
| import java.util.Map; |
21 |
| import java.util.WeakHashMap; |
22 |
| |
23 |
| import org.apache.hivemind.service.ClassFabUtils; |
24 |
| import org.apache.hivemind.util.Defense; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class StrategyRegistryImpl implements StrategyRegistry |
34 |
| { |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| private Map _registrations = new HashMap(); |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| private Map _cache = new WeakHashMap(); |
46 |
| |
47 |
104
| public synchronized void register(Class registrationClass, Object adaptor)
|
48 |
| { |
49 |
104
| Defense.notNull(registrationClass, "registrationClass");
|
50 |
104
| Defense.notNull(adaptor, "adaptor");
|
51 |
| |
52 |
104
| if (_registrations.containsKey(registrationClass))
|
53 |
1
| throw new IllegalArgumentException(UtilMessages
|
54 |
| .duplicateRegistration(registrationClass)); |
55 |
| |
56 |
103
| _registrations.put(registrationClass, adaptor);
|
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
103
| _cache.clear();
|
63 |
| } |
64 |
| |
65 |
13
| public synchronized Object getStrategy(Class subjectClass)
|
66 |
| { |
67 |
13
| Defense.notNull(subjectClass, "subjectClass");
|
68 |
| |
69 |
13
| Object result = _cache.get(subjectClass);
|
70 |
| |
71 |
13
| if (result != null)
|
72 |
0
| return result;
|
73 |
| |
74 |
13
| result = searchForAdaptor(subjectClass);
|
75 |
| |
76 |
| |
77 |
| |
78 |
12
| _cache.put(subjectClass, result);
|
79 |
| |
80 |
12
| return result;
|
81 |
| } |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
13
| private Object searchForAdaptor(Class subjectClass)
|
108 |
| { |
109 |
13
| LinkedList queue = null;
|
110 |
13
| Object result = null;
|
111 |
| |
112 |
| |
113 |
| |
114 |
13
| Class searchClass = subjectClass;
|
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
13
| while (searchClass != Object.class && searchClass != null)
|
120 |
| { |
121 |
17
| result = _registrations.get(searchClass);
|
122 |
17
| if (result != null)
|
123 |
6
| return result;
|
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
11
| Class[] interfaces = searchClass.getInterfaces();
|
129 |
11
| int length = interfaces.length;
|
130 |
| |
131 |
11
| if (queue == null && length > 0)
|
132 |
6
| queue = new LinkedList();
|
133 |
| |
134 |
11
| for (int i = 0; i < length; i++)
|
135 |
11
| queue.addLast(interfaces[i]);
|
136 |
| |
137 |
| |
138 |
| |
139 |
11
| searchClass = getSuperclass(searchClass);
|
140 |
| |
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
7
| if (queue != null)
|
147 |
| { |
148 |
5
| while (!queue.isEmpty())
|
149 |
| { |
150 |
8
| searchClass = (Class) queue.removeFirst();
|
151 |
| |
152 |
8
| result = _registrations.get(searchClass);
|
153 |
8
| if (result != null)
|
154 |
2
| return result;
|
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
6
| Class[] interfaces = searchClass.getInterfaces();
|
160 |
6
| int length = interfaces.length;
|
161 |
| |
162 |
6
| for (int i = 0; i < length; i++)
|
163 |
0
| queue.addLast(interfaces[i]);
|
164 |
| } |
165 |
| } |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
5
| result = _registrations.get(Object.class);
|
171 |
5
| if (result != null)
|
172 |
4
| return result;
|
173 |
| |
174 |
| |
175 |
| |
176 |
1
| throw new IllegalArgumentException(UtilMessages.strategyNotFound(subjectClass));
|
177 |
| } |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
11
| private Class getSuperclass(Class searchClass)
|
188 |
| { |
189 |
11
| if (searchClass.isArray())
|
190 |
| { |
191 |
2
| Class componentType = searchClass.getComponentType();
|
192 |
| |
193 |
2
| if (!componentType.isPrimitive() && componentType != Object.class)
|
194 |
1
| return Object[].class;
|
195 |
| } |
196 |
| |
197 |
10
| return searchClass.getSuperclass();
|
198 |
| } |
199 |
| |
200 |
1
| public synchronized String toString()
|
201 |
| { |
202 |
1
| StringBuffer buffer = new StringBuffer();
|
203 |
1
| buffer.append("AdaptorRegistry[");
|
204 |
| |
205 |
1
| Iterator i = _registrations.entrySet().iterator();
|
206 |
1
| boolean showSep = false;
|
207 |
| |
208 |
1
| while (i.hasNext())
|
209 |
| { |
210 |
1
| if (showSep)
|
211 |
0
| buffer.append(' ');
|
212 |
| |
213 |
1
| Map.Entry entry = (Map.Entry) i.next();
|
214 |
| |
215 |
1
| Class registeredClass = (Class) entry.getKey();
|
216 |
| |
217 |
1
| buffer.append(ClassFabUtils.getJavaClassName(registeredClass));
|
218 |
1
| buffer.append("=");
|
219 |
1
| buffer.append(entry.getValue());
|
220 |
| |
221 |
1
| showSep = true;
|
222 |
| } |
223 |
| |
224 |
1
| buffer.append("]");
|
225 |
| |
226 |
1
| return buffer.toString();
|
227 |
| } |
228 |
| } |