1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
package org.picocontainer.defaults;
|
11
|
|
|
12
|
|
import org.picocontainer.internals.ComponentRegistry;
|
13
|
|
import org.picocontainer.PicoInitializationException;
|
14
|
|
import org.picocontainer.PicoContainer;
|
15
|
|
import org.picocontainer.internals.ComponentSpecification;
|
16
|
|
|
17
|
|
import java.io.Serializable;
|
18
|
|
import java.util.*;
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
|
27
|
|
public class DefaultComponentRegistry implements ComponentRegistry, Serializable {
|
28
|
|
|
29
|
|
protected final List registeredComponentSpecifications;
|
30
|
|
|
31
|
|
|
32
|
|
protected final List orderedComponents;
|
33
|
|
|
34
|
|
protected final Map componentKeyToInstanceMap;
|
35
|
|
|
36
|
|
protected final Map componentToSpec;
|
37
|
|
|
38
|
|
|
39
|
116
|
public DefaultComponentRegistry() {
|
40
|
116
|
registeredComponentSpecifications = new ArrayList();
|
41
|
116
|
orderedComponents = new ArrayList();
|
42
|
116
|
componentKeyToInstanceMap = new HashMap();
|
43
|
116
|
componentToSpec = new HashMap();
|
44
|
|
}
|
45
|
|
|
46
|
172
|
public void registerComponent(ComponentSpecification compSpec) {
|
47
|
172
|
componentToSpec.put(compSpec.getComponentImplementation(), compSpec);
|
48
|
172
|
registeredComponentSpecifications.add(compSpec);
|
49
|
|
}
|
50
|
|
|
51
|
5
|
public void unregisterComponent(Object componentKey) {
|
52
|
5
|
for (Iterator iterator = registeredComponentSpecifications.iterator(); iterator.hasNext();) {
|
53
|
5
|
ComponentSpecification currentCompSpec = (ComponentSpecification) iterator.next();
|
54
|
|
|
55
|
5
|
if (currentCompSpec.getComponentKey().equals(componentKey)) {
|
56
|
5
|
registeredComponentSpecifications.remove(currentCompSpec);
|
57
|
5
|
componentKeyToInstanceMap.remove(componentKey);
|
58
|
5
|
break;
|
59
|
|
}
|
60
|
|
}
|
61
|
|
}
|
62
|
|
|
63
|
277
|
public Collection getComponentSpecifications() {
|
64
|
277
|
return registeredComponentSpecifications;
|
65
|
|
}
|
66
|
|
|
67
|
58
|
public List getOrderedComponents() {
|
68
|
58
|
return new ArrayList(orderedComponents);
|
69
|
|
}
|
70
|
|
|
71
|
140
|
public void addOrderedComponent(Object component) {
|
72
|
140
|
orderedComponents.add(component);
|
73
|
|
}
|
74
|
|
|
75
|
141
|
public void putComponent(Object componentKey, Object component) {
|
76
|
141
|
componentKeyToInstanceMap.put(componentKey, component);
|
77
|
|
}
|
78
|
|
|
79
|
160
|
public boolean contains(Object componentKey) {
|
80
|
160
|
return componentKeyToInstanceMap.containsKey(componentKey);
|
81
|
|
}
|
82
|
|
|
83
|
253
|
public Object getComponentInstance(Object componentKey) {
|
84
|
253
|
return componentKeyToInstanceMap.get(componentKey);
|
85
|
|
}
|
86
|
|
|
87
|
45
|
public Set getComponentInstanceKeys() {
|
88
|
45
|
Set types = componentKeyToInstanceMap.keySet();
|
89
|
45
|
return Collections.unmodifiableSet(types);
|
90
|
|
}
|
91
|
|
|
92
|
19
|
public Set getComponentInstances() {
|
93
|
|
|
94
|
|
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
19
|
Set result = new HashSet();
|
100
|
19
|
result.addAll(componentKeyToInstanceMap.values());
|
101
|
19
|
return Collections.unmodifiableSet(result);
|
102
|
|
}
|
103
|
|
|
104
|
15
|
public boolean hasComponentInstance(Object componentKey) {
|
105
|
15
|
return componentKeyToInstanceMap.containsKey(componentKey);
|
106
|
|
}
|
107
|
|
|
108
|
7
|
public ComponentSpecification getComponentSpec(Object componentKey) {
|
109
|
7
|
return (ComponentSpecification) componentToSpec.get(componentKey);
|
110
|
|
}
|
111
|
|
|
112
|
33
|
public Object findImplementingComponent(Class componentType) throws AmbiguousComponentResolutionException {
|
113
|
33
|
List found = new ArrayList();
|
114
|
|
|
115
|
33
|
for (Iterator iterator = getComponentInstanceKeys().iterator(); iterator.hasNext();) {
|
116
|
28
|
Object key = iterator.next();
|
117
|
28
|
Object component = getComponentInstance(key);
|
118
|
28
|
if (componentType.isInstance(component)) {
|
119
|
13
|
found.add(key);
|
120
|
|
}
|
121
|
|
}
|
122
|
|
|
123
|
33
|
if (found.size() > 1) {
|
124
|
1
|
Object[] ambiguousKeys = found.toArray();
|
125
|
1
|
throw new AmbiguousComponentResolutionException(componentType, ambiguousKeys);
|
126
|
|
}
|
127
|
|
|
128
|
32
|
return found.isEmpty() ? null : getComponentInstance(found.get(0));
|
129
|
|
}
|
130
|
|
|
131
|
24
|
public ComponentSpecification findImplementingComponentSpecification(Class componentType) throws AmbiguousComponentResolutionException {
|
132
|
24
|
List found = new ArrayList();
|
133
|
24
|
for (Iterator iterator = getComponentSpecifications().iterator(); iterator.hasNext();) {
|
134
|
49
|
ComponentSpecification componentSpecification = (ComponentSpecification) iterator.next();
|
135
|
|
|
136
|
49
|
if (componentType.isAssignableFrom(componentSpecification.getComponentImplementation())) {
|
137
|
20
|
found.add(componentSpecification);
|
138
|
|
}
|
139
|
|
}
|
140
|
|
|
141
|
24
|
if (found.size() > 1) {
|
142
|
1
|
Class[] foundClasses = new Class[found.size()];
|
143
|
1
|
for (int i = 0; i < foundClasses.length; i++) {
|
144
|
2
|
foundClasses[i] = ((ComponentSpecification) found.get(i)).getComponentImplementation();
|
145
|
|
}
|
146
|
1
|
throw new AmbiguousComponentResolutionException(componentType, foundClasses);
|
147
|
|
}
|
148
|
|
|
149
|
23
|
return found.isEmpty() ? null : ((ComponentSpecification) found.get(0));
|
150
|
|
}
|
151
|
|
|
152
|
|
|
153
|
158
|
public Object createComponent(ComponentSpecification componentSpecification) throws PicoInitializationException {
|
154
|
158
|
if (!contains(componentSpecification.getComponentKey())) {
|
155
|
124
|
Object component = componentSpecification.instantiateComponent(this);
|
156
|
118
|
addOrderedComponent(component);
|
157
|
|
|
158
|
118
|
putComponent(componentSpecification.getComponentKey(), component);
|
159
|
|
|
160
|
118
|
return component;
|
161
|
|
} else {
|
162
|
34
|
return getComponentInstance(componentSpecification.getComponentKey());
|
163
|
|
}
|
164
|
|
}
|
165
|
|
|
166
|
|
}
|
167
|
|
|