1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
package org.picocontainer.extras;
|
12
|
|
|
13
|
|
import org.picocontainer.internals.ComponentRegistry;
|
14
|
|
import org.picocontainer.PicoInitializationException;
|
15
|
|
import org.picocontainer.defaults.AmbiguousComponentResolutionException;
|
16
|
|
import org.picocontainer.internals.ComponentSpecification;
|
17
|
|
import org.picocontainer.defaults.DefaultComponentRegistry;
|
18
|
|
|
19
|
|
import java.io.Serializable;
|
20
|
|
import java.util.Collection;
|
21
|
|
import java.util.Collections;
|
22
|
|
import java.util.HashSet;
|
23
|
|
import java.util.List;
|
24
|
|
import java.util.Set;
|
25
|
|
import java.util.ArrayList;
|
26
|
|
|
27
|
|
public class HierarchicalComponentRegistry implements ComponentRegistry, Serializable {
|
28
|
|
|
29
|
|
protected final ComponentRegistry parentRegistry;
|
30
|
|
protected final ComponentRegistry childRegistry;
|
31
|
|
|
32
|
6
|
protected HierarchicalComponentRegistry(ComponentRegistry parentRegistry, ComponentRegistry childRegistry) {
|
33
|
6
|
if (parentRegistry == null) {
|
34
|
1
|
throw new NullPointerException("parentRegistry cannot be null");
|
35
|
|
}
|
36
|
5
|
if (childRegistry == null) {
|
37
|
1
|
throw new NullPointerException("childRegistry cannot be null");
|
38
|
|
}
|
39
|
4
|
this.parentRegistry = parentRegistry;
|
40
|
4
|
this.childRegistry = childRegistry;
|
41
|
|
}
|
42
|
|
|
43
|
|
public static class Default extends HierarchicalComponentRegistry {
|
44
|
4
|
public Default(ComponentRegistry parentRegistry) {
|
45
|
4
|
super(parentRegistry, new DefaultComponentRegistry());
|
46
|
|
}
|
47
|
|
}
|
48
|
|
|
49
|
|
public static class WithChildRegistry extends HierarchicalComponentRegistry {
|
50
|
0
|
public WithChildRegistry(ComponentRegistry parentRegistry, ComponentRegistry childRegistry) {
|
51
|
0
|
super(parentRegistry, childRegistry);
|
52
|
|
}
|
53
|
|
}
|
54
|
|
|
55
|
2
|
public void registerComponent(ComponentSpecification compSpec) {
|
56
|
2
|
childRegistry.registerComponent(compSpec);
|
57
|
|
}
|
58
|
|
|
59
|
0
|
public void unregisterComponent(Object componentKey) {
|
60
|
0
|
childRegistry.unregisterComponent(componentKey);
|
61
|
|
}
|
62
|
|
|
63
|
5
|
public Collection getComponentSpecifications() {
|
64
|
5
|
return childRegistry.getComponentSpecifications();
|
65
|
|
}
|
66
|
|
|
67
|
1
|
public List getOrderedComponents() {
|
68
|
|
|
69
|
1
|
List types = new ArrayList(childRegistry.getOrderedComponents());
|
70
|
|
|
71
|
|
|
72
|
1
|
types.addAll(parentRegistry.getOrderedComponents());
|
73
|
|
|
74
|
1
|
return Collections.unmodifiableList(types);
|
75
|
|
}
|
76
|
|
|
77
|
2
|
public void addOrderedComponent(Object component) {
|
78
|
2
|
childRegistry.addOrderedComponent(component);
|
79
|
|
}
|
80
|
|
|
81
|
2
|
public void putComponent(Object componentKey, Object component) {
|
82
|
2
|
childRegistry.putComponent(componentKey, component);
|
83
|
|
}
|
84
|
|
|
85
|
2
|
public boolean contains(Object componentKey) {
|
86
|
2
|
return childRegistry.contains(componentKey);
|
87
|
|
}
|
88
|
|
|
89
|
6
|
public Object getComponentInstance(Object componentKey) {
|
90
|
|
|
91
|
|
|
92
|
6
|
Object result = childRegistry.getComponentInstance(componentKey);
|
93
|
|
|
94
|
|
|
95
|
6
|
if (result == null) {
|
96
|
4
|
result = parentRegistry.getComponentInstance(componentKey);
|
97
|
|
}
|
98
|
6
|
return result;
|
99
|
|
}
|
100
|
|
|
101
|
2
|
public Set getComponentInstanceKeys() {
|
102
|
|
|
103
|
|
|
104
|
2
|
Set types = new HashSet(childRegistry.getComponentInstanceKeys());
|
105
|
|
|
106
|
|
|
107
|
2
|
types.addAll(parentRegistry.getComponentInstanceKeys());
|
108
|
|
|
109
|
2
|
return Collections.unmodifiableSet(types);
|
110
|
|
|
111
|
|
}
|
112
|
|
|
113
|
2
|
public Set getComponentInstances() {
|
114
|
|
|
115
|
2
|
Set types = new HashSet(childRegistry.getComponentInstances());
|
116
|
|
|
117
|
|
|
118
|
2
|
types.addAll(parentRegistry.getComponentInstances());
|
119
|
|
|
120
|
2
|
return Collections.unmodifiableSet(types);
|
121
|
|
}
|
122
|
|
|
123
|
4
|
public boolean hasComponentInstance(Object componentKey) {
|
124
|
4
|
return childRegistry.hasComponentInstance(componentKey)
|
125
|
|
| parentRegistry.hasComponentInstance(componentKey);
|
126
|
|
}
|
127
|
|
|
128
|
0
|
public ComponentSpecification getComponentSpec(Object componentKey) {
|
129
|
|
|
130
|
0
|
ComponentSpecification result = childRegistry.getComponentSpec(componentKey);
|
131
|
|
|
132
|
|
|
133
|
0
|
if (result == null) {
|
134
|
0
|
result = parentRegistry.getComponentSpec(componentKey);
|
135
|
|
}
|
136
|
0
|
return result;
|
137
|
|
|
138
|
|
}
|
139
|
|
|
140
|
2
|
public Object findImplementingComponent(Class componentType) throws AmbiguousComponentResolutionException {
|
141
|
|
|
142
|
|
|
143
|
2
|
Object result = childRegistry.findImplementingComponent(componentType);
|
144
|
|
|
145
|
|
|
146
|
2
|
if (result == null) {
|
147
|
2
|
result = parentRegistry.findImplementingComponent(componentType);
|
148
|
|
}
|
149
|
2
|
return result;
|
150
|
|
|
151
|
|
}
|
152
|
|
|
153
|
1
|
public ComponentSpecification findImplementingComponentSpecification(Class componentType) throws AmbiguousComponentResolutionException {
|
154
|
|
|
155
|
|
|
156
|
1
|
ComponentSpecification result = childRegistry.findImplementingComponentSpecification(componentType);
|
157
|
|
|
158
|
|
|
159
|
1
|
if (result == null) {
|
160
|
1
|
result = parentRegistry.findImplementingComponentSpecification(componentType);
|
161
|
|
}
|
162
|
1
|
return result;
|
163
|
|
}
|
164
|
|
|
165
|
2
|
public Object createComponent(ComponentSpecification componentSpecification) throws PicoInitializationException {
|
166
|
2
|
if (!contains(componentSpecification.getComponentKey())) {
|
167
|
1
|
Object component = componentSpecification.instantiateComponent(this);
|
168
|
1
|
addOrderedComponent(component);
|
169
|
|
|
170
|
1
|
putComponent(componentSpecification.getComponentKey(), component);
|
171
|
|
|
172
|
1
|
return component;
|
173
|
|
} else {
|
174
|
1
|
return getComponentInstance(componentSpecification.getComponentKey());
|
175
|
|
}
|
176
|
|
}
|
177
|
|
|
178
|
|
}
|
179
|
|
|