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.PicoContainer;
|
15
|
|
import org.picocontainer.defaults.DefaultComponentRegistry;
|
16
|
|
|
17
|
|
import java.util.ArrayList;
|
18
|
|
import java.util.HashSet;
|
19
|
|
import java.util.Iterator;
|
20
|
|
import java.util.List;
|
21
|
|
import java.util.Set;
|
22
|
|
import java.util.Collections;
|
23
|
|
import java.util.Collection;
|
24
|
|
import java.io.Serializable;
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
public class CompositePicoContainer implements PicoContainer, Serializable {
|
38
|
|
|
39
|
|
private final List containers = new ArrayList();
|
40
|
|
private final ComponentRegistry componentRegistry;
|
41
|
|
|
42
|
17
|
public CompositePicoContainer(final ComponentRegistry componentRegistry,
|
43
|
|
final PicoContainer[] containers) {
|
44
|
17
|
this.componentRegistry = componentRegistry;
|
45
|
|
|
46
|
17
|
if (containers == null) {
|
47
|
1
|
throw new NullPointerException("containers can't be null");
|
48
|
|
}
|
49
|
16
|
for (int i = 0; i < containers.length; i++) {
|
50
|
15
|
PicoContainer container = containers[i];
|
51
|
15
|
if (container == null) {
|
52
|
1
|
throw new NullPointerException("PicoContainer at position " + i + " was null");
|
53
|
|
}
|
54
|
14
|
this.containers.add(container);
|
55
|
|
}
|
56
|
|
}
|
57
|
|
|
58
|
|
public static class WithContainerArray extends CompositePicoContainer {
|
59
|
13
|
public WithContainerArray(final PicoContainer[] containers) {
|
60
|
13
|
super(new DefaultComponentRegistry(), containers);
|
61
|
|
}
|
62
|
|
}
|
63
|
|
|
64
|
|
public static class Default extends CompositePicoContainer {
|
65
|
1
|
public Default() {
|
66
|
1
|
super(new DefaultComponentRegistry(), new PicoContainer[]{new NullContainer()});
|
67
|
|
}
|
68
|
|
}
|
69
|
|
|
70
|
8
|
public Object getComponent(Object componentKey) {
|
71
|
8
|
Object answer = componentRegistry.getComponentInstance(componentKey);
|
72
|
8
|
if (answer == null) {
|
73
|
7
|
for (Iterator iter = containers.iterator(); iter.hasNext();) {
|
74
|
8
|
PicoContainer container = (PicoContainer) iter.next();
|
75
|
8
|
if (container.hasComponent(componentKey)) {
|
76
|
6
|
return container.getComponent(componentKey);
|
77
|
|
}
|
78
|
|
}
|
79
|
|
}
|
80
|
2
|
return answer;
|
81
|
|
}
|
82
|
|
|
83
|
2
|
public Collection getComponentKeys() {
|
84
|
2
|
Set componentTypes = new HashSet();
|
85
|
2
|
componentTypes.addAll(componentRegistry.getComponentInstanceKeys());
|
86
|
2
|
for (Iterator iter = containers.iterator(); iter.hasNext();) {
|
87
|
3
|
PicoContainer container = (PicoContainer) iter.next();
|
88
|
3
|
componentTypes.addAll(container.getComponentKeys());
|
89
|
|
}
|
90
|
2
|
return Collections.unmodifiableSet(componentTypes);
|
91
|
|
}
|
92
|
|
|
93
|
1
|
public void instantiateComponents() {
|
94
|
1
|
throw new UnsupportedOperationException();
|
95
|
|
}
|
96
|
|
|
97
|
7
|
public boolean hasComponent(Object componentKey) {
|
98
|
7
|
if (componentRegistry.hasComponentInstance(componentKey)) {
|
99
|
1
|
return true;
|
100
|
|
} else {
|
101
|
6
|
for (Iterator iter = containers.iterator(); iter.hasNext();) {
|
102
|
7
|
PicoContainer container = (PicoContainer) iter.next();
|
103
|
7
|
if (container.hasComponent(componentKey)) {
|
104
|
4
|
return true;
|
105
|
|
}
|
106
|
|
}
|
107
|
2
|
return false;
|
108
|
|
}
|
109
|
|
}
|
110
|
|
|
111
|
3
|
public Collection getComponents() {
|
112
|
3
|
Set componentTypes = new HashSet();
|
113
|
3
|
componentTypes.addAll(componentRegistry.getComponentInstanceKeys());
|
114
|
3
|
for (Iterator iter = containers.iterator(); iter.hasNext();) {
|
115
|
3
|
PicoContainer container = (PicoContainer) iter.next();
|
116
|
3
|
componentTypes.addAll(container.getComponentKeys());
|
117
|
|
}
|
118
|
3
|
Set set = new HashSet();
|
119
|
3
|
for (Iterator iterator = componentTypes.iterator(); iterator.hasNext();) {
|
120
|
3
|
Object key = (Object) iterator.next();
|
121
|
3
|
set.add(getComponent(key));
|
122
|
|
}
|
123
|
3
|
return Collections.unmodifiableSet(set);
|
124
|
|
}
|
125
|
|
|
126
|
1
|
public Object getComponentMulticaster() {
|
127
|
1
|
throw new UnsupportedOperationException();
|
128
|
|
}
|
129
|
|
|
130
|
1
|
public Object getComponentMulticaster(boolean callInInstantiationOrder, boolean callUnmanagedComponents) {
|
131
|
1
|
throw new UnsupportedOperationException();
|
132
|
|
}
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
|
|
138
|
1
|
protected void addContainer(PicoContainer container) {
|
139
|
1
|
containers.add(container);
|
140
|
|
}
|
141
|
|
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
1
|
protected void removeContainer(PicoContainer container) {
|
147
|
1
|
containers.remove(container);
|
148
|
|
}
|
149
|
|
|
150
|
|
}
|
151
|
|
|