1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
package org.picocontainer.defaults;
|
11
|
|
|
12
|
|
import org.picocontainer.ComponentAdapter;
|
13
|
|
import org.picocontainer.MutablePicoContainer;
|
14
|
|
import org.picocontainer.Parameter;
|
15
|
|
import org.picocontainer.PicoContainer;
|
16
|
|
import org.picocontainer.PicoException;
|
17
|
|
import org.picocontainer.PicoRegistrationException;
|
18
|
|
import org.picocontainer.PicoVerificationException;
|
19
|
|
import org.picocontainer.PicoVisitor;
|
20
|
|
import org.picocontainer.alternatives.ImmutablePicoContainer;
|
21
|
|
|
22
|
|
import java.io.Serializable;
|
23
|
|
import java.util.ArrayList;
|
24
|
|
import java.util.Collection;
|
25
|
|
import java.util.Collections;
|
26
|
|
import java.util.HashMap;
|
27
|
|
import java.util.HashSet;
|
28
|
|
import java.util.Iterator;
|
29
|
|
import java.util.List;
|
30
|
|
import java.util.Map;
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
public class DefaultPicoContainer implements MutablePicoContainer, Serializable {
|
59
|
|
|
60
|
|
private Map componentKeyToAdapterCache = new HashMap();
|
61
|
|
private ComponentAdapterFactory componentAdapterFactory;
|
62
|
|
private PicoContainer parent;
|
63
|
|
private List componentAdapters = new ArrayList();
|
64
|
|
|
65
|
|
|
66
|
|
private List orderedComponentAdapters = new ArrayList();
|
67
|
|
|
68
|
|
private boolean started = false;
|
69
|
|
private boolean disposed = false;
|
70
|
|
private HashSet children = new HashSet();
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
1052
|
public DefaultPicoContainer(ComponentAdapterFactory componentAdapterFactory, PicoContainer parent) {
|
86
|
0
|
if(componentAdapterFactory == null) throw new NullPointerException("componentAdapterFactory");
|
87
|
1052
|
this.componentAdapterFactory = componentAdapterFactory;
|
88
|
1052
|
this.parent = parent == null ? null : new ImmutablePicoContainer(parent);
|
89
|
|
}
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
200
|
public DefaultPicoContainer(PicoContainer parent) {
|
96
|
200
|
this(new DefaultComponentAdapterFactory(), parent);
|
97
|
|
}
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
118
|
public DefaultPicoContainer(ComponentAdapterFactory componentAdapterFactory) {
|
105
|
118
|
this(componentAdapterFactory, null);
|
106
|
|
}
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
|
|
111
|
354
|
public DefaultPicoContainer() {
|
112
|
354
|
this(new DefaultComponentAdapterFactory(), null);
|
113
|
|
}
|
114
|
|
|
115
|
3684
|
public Collection getComponentAdapters() {
|
116
|
3684
|
return Collections.unmodifiableList(componentAdapters);
|
117
|
|
}
|
118
|
|
|
119
|
3852
|
public final ComponentAdapter getComponentAdapter(Object componentKey) throws AmbiguousComponentResolutionException {
|
120
|
3852
|
ComponentAdapter adapter = (ComponentAdapter) componentKeyToAdapterCache.get(componentKey);
|
121
|
3852
|
if (adapter == null && parent != null) {
|
122
|
320
|
adapter = parent.getComponentAdapter(componentKey);
|
123
|
|
}
|
124
|
3852
|
return adapter;
|
125
|
|
}
|
126
|
|
|
127
|
2234
|
public ComponentAdapter getComponentAdapterOfType(Class componentType) {
|
128
|
|
|
129
|
2234
|
ComponentAdapter adapterByKey = getComponentAdapter(componentType);
|
130
|
2234
|
if (adapterByKey != null) {
|
131
|
740
|
return adapterByKey;
|
132
|
|
}
|
133
|
|
|
134
|
1494
|
List found = getComponentAdaptersOfType(componentType);
|
135
|
|
|
136
|
1494
|
if (found.size() == 1) {
|
137
|
336
|
return ((ComponentAdapter) found.get(0));
|
138
|
1158
|
} else if (found.size() == 0) {
|
139
|
1142
|
if (parent != null) {
|
140
|
66
|
return parent.getComponentAdapterOfType(componentType);
|
141
|
|
} else {
|
142
|
1076
|
return null;
|
143
|
|
}
|
144
|
|
} else {
|
145
|
16
|
Class[] foundClasses = new Class[found.size()];
|
146
|
16
|
for (int i = 0; i < foundClasses.length; i++) {
|
147
|
32
|
ComponentAdapter componentAdapter = (ComponentAdapter) found.get(i);
|
148
|
32
|
foundClasses[i] = componentAdapter.getComponentImplementation();
|
149
|
|
}
|
150
|
|
|
151
|
16
|
throw new AmbiguousComponentResolutionException(componentType, foundClasses);
|
152
|
|
}
|
153
|
|
}
|
154
|
|
|
155
|
2372
|
public List getComponentAdaptersOfType(Class componentType) {
|
156
|
2372
|
if(componentType == null) {
|
157
|
0
|
return Collections.EMPTY_LIST;
|
158
|
|
}
|
159
|
2372
|
List found = new ArrayList();
|
160
|
2372
|
for (Iterator iterator = getComponentAdapters().iterator(); iterator.hasNext();) {
|
161
|
5232
|
ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
|
162
|
|
|
163
|
5232
|
if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
|
164
|
682
|
found.add(componentAdapter);
|
165
|
|
}
|
166
|
|
}
|
167
|
2372
|
return found;
|
168
|
|
}
|
169
|
|
|
170
|
|
|
171
|
|
|
172
|
|
|
173
|
|
|
174
|
|
|
175
|
1422
|
public ComponentAdapter registerComponent(ComponentAdapter componentAdapter) throws DuplicateComponentKeyRegistrationException {
|
176
|
1422
|
Object componentKey = componentAdapter.getComponentKey();
|
177
|
1422
|
if (componentKeyToAdapterCache.containsKey(componentKey)) {
|
178
|
14
|
throw new DuplicateComponentKeyRegistrationException(componentKey);
|
179
|
|
}
|
180
|
1408
|
componentAdapters.add(componentAdapter);
|
181
|
1408
|
componentKeyToAdapterCache.put(componentKey, componentAdapter);
|
182
|
1408
|
return componentAdapter;
|
183
|
|
}
|
184
|
|
|
185
|
46
|
public ComponentAdapter unregisterComponent(Object componentKey) {
|
186
|
46
|
ComponentAdapter adapter = (ComponentAdapter) componentKeyToAdapterCache.remove(componentKey);
|
187
|
46
|
componentAdapters.remove(adapter);
|
188
|
46
|
orderedComponentAdapters.remove(adapter);
|
189
|
46
|
return adapter;
|
190
|
|
}
|
191
|
|
|
192
|
|
|
193
|
|
|
194
|
|
|
195
|
|
|
196
|
178
|
public ComponentAdapter registerComponentInstance(Object component) throws PicoRegistrationException {
|
197
|
178
|
return registerComponentInstance(component.getClass(), component);
|
198
|
|
}
|
199
|
|
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
288
|
public ComponentAdapter registerComponentInstance(Object componentKey, Object componentInstance) throws PicoRegistrationException {
|
205
|
288
|
if (componentInstance instanceof MutablePicoContainer) {
|
206
|
12
|
MutablePicoContainer pc = (MutablePicoContainer) componentInstance;
|
207
|
12
|
Object contrivedKey = new Object();
|
208
|
12
|
String contrivedComp = "";
|
209
|
12
|
pc.registerComponentInstance(contrivedKey, contrivedComp);
|
210
|
12
|
try {
|
211
|
12
|
if (this.getComponentInstance(contrivedKey) != null) {
|
212
|
12
|
throw new PicoRegistrationException("Cannot register a container to itself. The container is already implicitly registered.");
|
213
|
|
}
|
214
|
|
} finally {
|
215
|
12
|
pc.unregisterComponent(contrivedKey);
|
216
|
|
}
|
217
|
|
|
218
|
|
}
|
219
|
276
|
ComponentAdapter componentAdapter = new InstanceComponentAdapter(componentKey, componentInstance);
|
220
|
264
|
registerComponent(componentAdapter);
|
221
|
262
|
return componentAdapter;
|
222
|
|
}
|
223
|
|
|
224
|
|
|
225
|
|
|
226
|
|
|
227
|
|
|
228
|
|
|
229
|
522
|
public ComponentAdapter registerComponentImplementation(Class componentImplementation) throws PicoRegistrationException {
|
230
|
522
|
return registerComponentImplementation(componentImplementation, componentImplementation);
|
231
|
|
}
|
232
|
|
|
233
|
|
|
234
|
|
|
235
|
|
|
236
|
|
|
237
|
|
|
238
|
780
|
public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation) throws PicoRegistrationException {
|
239
|
780
|
return registerComponentImplementation(componentKey, componentImplementation, (Parameter[]) null);
|
240
|
|
}
|
241
|
|
|
242
|
|
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
|
|
247
|
926
|
public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, Parameter[] parameters) throws PicoRegistrationException {
|
248
|
926
|
ComponentAdapter componentAdapter = componentAdapterFactory.createComponentAdapter(componentKey, componentImplementation, parameters);
|
249
|
920
|
registerComponent(componentAdapter);
|
250
|
908
|
return componentAdapter;
|
251
|
|
}
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
2
|
public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, List parameters) throws PicoRegistrationException {
|
258
|
2
|
Parameter[] parametersAsArray = (Parameter[]) parameters.toArray(new Parameter[parameters.size()]);
|
259
|
2
|
return registerComponentImplementation(componentKey, componentImplementation, parametersAsArray);
|
260
|
|
}
|
261
|
|
|
262
|
1976
|
private void addOrderedComponentAdapter(ComponentAdapter componentAdapter) {
|
263
|
1976
|
if (!orderedComponentAdapters.contains(componentAdapter)) {
|
264
|
906
|
orderedComponentAdapters.add(componentAdapter);
|
265
|
|
}
|
266
|
|
}
|
267
|
|
|
268
|
152
|
public List getComponentInstances() throws PicoException {
|
269
|
152
|
return getComponentInstancesOfType(Object.class);
|
270
|
|
}
|
271
|
|
|
272
|
480
|
public List getComponentInstancesOfType(Class componentType) throws PicoException {
|
273
|
480
|
if(componentType == null) {
|
274
|
0
|
return Collections.EMPTY_LIST;
|
275
|
|
}
|
276
|
|
|
277
|
480
|
Map adapterToInstanceMap = new HashMap();
|
278
|
480
|
for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) {
|
279
|
668
|
ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
|
280
|
668
|
if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
|
281
|
472
|
Object componentInstance = getInstance(componentAdapter);
|
282
|
470
|
adapterToInstanceMap.put(componentAdapter, componentInstance);
|
283
|
|
|
284
|
|
|
285
|
|
|
286
|
470
|
addOrderedComponentAdapter(componentAdapter);
|
287
|
|
}
|
288
|
|
}
|
289
|
478
|
List result = new ArrayList();
|
290
|
478
|
for (Iterator iterator = orderedComponentAdapters.iterator(); iterator.hasNext();) {
|
291
|
628
|
Object componentAdapter = iterator.next();
|
292
|
628
|
final Object componentInstance = adapterToInstanceMap.get(componentAdapter);
|
293
|
628
|
if (componentInstance != null) {
|
294
|
|
|
295
|
|
|
296
|
468
|
result.add(componentInstance);
|
297
|
|
}
|
298
|
|
}
|
299
|
478
|
return result;
|
300
|
|
}
|
301
|
|
|
302
|
1206
|
public Object getComponentInstance(Object componentKey) throws PicoException {
|
303
|
1206
|
ComponentAdapter componentAdapter = getComponentAdapter(componentKey);
|
304
|
1206
|
if (componentAdapter != null) {
|
305
|
1174
|
return getInstance(componentAdapter);
|
306
|
|
} else {
|
307
|
32
|
return null;
|
308
|
|
}
|
309
|
|
}
|
310
|
|
|
311
|
86
|
public Object getComponentInstanceOfType(Class componentType) {
|
312
|
86
|
final ComponentAdapter componentAdapter = getComponentAdapterOfType(componentType);
|
313
|
86
|
return componentAdapter == null ? null : getInstance(componentAdapter);
|
314
|
|
}
|
315
|
|
|
316
|
1724
|
private Object getInstance(ComponentAdapter componentAdapter) {
|
317
|
|
|
318
|
|
|
319
|
1724
|
final boolean isLocal = componentAdapters.contains(componentAdapter);
|
320
|
|
|
321
|
1724
|
if (isLocal) {
|
322
|
1614
|
Object instance = componentAdapter.getComponentInstance(this);
|
323
|
|
|
324
|
1506
|
addOrderedComponentAdapter(componentAdapter);
|
325
|
|
|
326
|
1506
|
return instance;
|
327
|
110
|
} else if (parent != null) {
|
328
|
110
|
return parent.getComponentInstance(componentAdapter.getComponentKey());
|
329
|
|
}
|
330
|
|
|
331
|
|
|
332
|
|
|
333
|
0
|
return null;
|
334
|
|
}
|
335
|
|
|
336
|
|
|
337
|
898
|
public PicoContainer getParent() {
|
338
|
898
|
return parent;
|
339
|
|
}
|
340
|
|
|
341
|
14
|
public ComponentAdapter unregisterComponentByInstance(Object componentInstance) {
|
342
|
14
|
Collection componentAdapters = getComponentAdapters();
|
343
|
14
|
for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) {
|
344
|
4
|
ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
|
345
|
4
|
if (getInstance(componentAdapter).equals(componentInstance)) {
|
346
|
2
|
return unregisterComponent(componentAdapter.getComponentKey());
|
347
|
|
}
|
348
|
|
}
|
349
|
12
|
return null;
|
350
|
|
}
|
351
|
|
|
352
|
|
|
353
|
|
|
354
|
|
|
355
|
0
|
public void verify() throws PicoVerificationException {
|
356
|
0
|
new VerifyingVisitor().traverse(this);
|
357
|
|
}
|
358
|
|
|
359
|
|
|
360
|
|
|
361
|
|
|
362
|
|
|
363
|
|
|
364
|
|
|
365
|
|
|
366
|
90
|
public void start() {
|
367
|
2
|
if (disposed) throw new IllegalStateException("Already disposed");
|
368
|
4
|
if (started) throw new IllegalStateException("Already started");
|
369
|
84
|
LifecycleVisitor.start(this);
|
370
|
82
|
started = true;
|
371
|
|
}
|
372
|
|
|
373
|
|
|
374
|
|
|
375
|
|
|
376
|
|
|
377
|
|
|
378
|
|
|
379
|
|
|
380
|
68
|
public void stop() {
|
381
|
2
|
if (disposed) throw new IllegalStateException("Already disposed");
|
382
|
2
|
if (!started) throw new IllegalStateException("Not started");
|
383
|
64
|
LifecycleVisitor.stop(this);
|
384
|
64
|
started = false;
|
385
|
|
}
|
386
|
|
|
387
|
|
|
388
|
|
|
389
|
|
|
390
|
|
|
391
|
|
|
392
|
|
|
393
|
|
|
394
|
64
|
public void dispose() {
|
395
|
2
|
if (disposed) throw new IllegalStateException("Already disposed");
|
396
|
62
|
LifecycleVisitor.dispose(this);
|
397
|
62
|
disposed = true;
|
398
|
|
}
|
399
|
|
|
400
|
30
|
public MutablePicoContainer makeChildContainer() {
|
401
|
30
|
DefaultPicoContainer pc = new DefaultPicoContainer(componentAdapterFactory, this);
|
402
|
30
|
addChildContainer(pc);
|
403
|
30
|
return pc;
|
404
|
|
}
|
405
|
|
|
406
|
72
|
public boolean addChildContainer(PicoContainer child) {
|
407
|
72
|
return children.add(child);
|
408
|
|
}
|
409
|
|
|
410
|
12
|
public boolean removeChildContainer(PicoContainer child) {
|
411
|
12
|
final boolean result = children.remove(child);
|
412
|
12
|
return result;
|
413
|
|
}
|
414
|
|
|
415
|
360
|
public void accept(PicoVisitor visitor) {
|
416
|
360
|
visitor.visitContainer(this);
|
417
|
356
|
final List componentAdapters = new ArrayList(getComponentAdapters());
|
418
|
356
|
for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) {
|
419
|
506
|
ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next();
|
420
|
506
|
componentAdapter.accept(visitor);
|
421
|
|
}
|
422
|
356
|
final List allChildren = new ArrayList(children);
|
423
|
356
|
for (Iterator iterator = allChildren.iterator(); iterator.hasNext();) {
|
424
|
104
|
PicoContainer child = (PicoContainer) iterator.next();
|
425
|
104
|
child.accept(visitor);
|
426
|
|
}
|
427
|
|
}
|
428
|
|
}
|
429
|
|
|