1 /******************************************************************************
2 * Copyright (C) PicoContainer Organization. All rights reserved. *
3 * ------------------------------------------------------------------------- *
4 * The software in this package is published under the terms of the BSD *
5 * style license a copy of which has been included with this distribution in *
6 * the LICENSE.txt file. *
7 * *
8 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
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 * CompositePicoContainer aggregates the the contents of more
28 * than one internals together for the sake of a single list of
29 * components. This list may be used as the parent internals for
30 * another PicoContainer. This will result in directive graphs of
31 * containers/components rather than just trees.
32 *
33 * It is not in itself, a Pico component (the array in the
34 * constructor puts paid to that).
35 *
36 */
37 public class CompositePicoContainer implements PicoContainer, Serializable {
38
39 private final List containers = new ArrayList();
40 private final ComponentRegistry componentRegistry;
41
42 public CompositePicoContainer(final ComponentRegistry componentRegistry,
43 final PicoContainer[] containers) {
44 this.componentRegistry = componentRegistry;
45
46 if (containers == null) {
47 throw new NullPointerException("containers can't be null");
48 }
49 for (int i = 0; i < containers.length; i++) {
50 PicoContainer container = containers[i];
51 if (container == null) {
52 throw new NullPointerException("PicoContainer at position " + i + " was null");
53 }
54 this.containers.add(container);
55 }
56 }
57
58 public static class WithContainerArray extends CompositePicoContainer {
59 public WithContainerArray(final PicoContainer[] containers) {
60 super(new DefaultComponentRegistry(), containers);
61 }
62 }
63
64 public static class Default extends CompositePicoContainer {
65 public Default() {
66 super(new DefaultComponentRegistry(), new PicoContainer[]{new NullContainer()});
67 }
68 }
69
70 public Object getComponent(Object componentKey) {
71 Object answer = componentRegistry.getComponentInstance(componentKey);
72 if (answer == null) {
73 for (Iterator iter = containers.iterator(); iter.hasNext();) {
74 PicoContainer container = (PicoContainer) iter.next();
75 if (container.hasComponent(componentKey)) {
76 return container.getComponent(componentKey);
77 }
78 }
79 }
80 return answer;
81 }
82
83 public Collection getComponentKeys() {
84 Set componentTypes = new HashSet();
85 componentTypes.addAll(componentRegistry.getComponentInstanceKeys());
86 for (Iterator iter = containers.iterator(); iter.hasNext();) {
87 PicoContainer container = (PicoContainer) iter.next();
88 componentTypes.addAll(container.getComponentKeys());
89 }
90 return Collections.unmodifiableSet(componentTypes);
91 }
92
93 public void instantiateComponents() {
94 throw new UnsupportedOperationException();
95 }
96
97 public boolean hasComponent(Object componentKey) {
98 if (componentRegistry.hasComponentInstance(componentKey)) {
99 return true;
100 } else {
101 for (Iterator iter = containers.iterator(); iter.hasNext();) {
102 PicoContainer container = (PicoContainer) iter.next();
103 if (container.hasComponent(componentKey)) {
104 return true;
105 }
106 }
107 return false;
108 }
109 }
110
111 public Collection getComponents() {
112 Set componentTypes = new HashSet();
113 componentTypes.addAll(componentRegistry.getComponentInstanceKeys());
114 for (Iterator iter = containers.iterator(); iter.hasNext();) {
115 PicoContainer container = (PicoContainer) iter.next();
116 componentTypes.addAll(container.getComponentKeys());
117 }
118 Set set = new HashSet();
119 for (Iterator iterator = componentTypes.iterator(); iterator.hasNext();) {
120 Object key = (Object) iterator.next();
121 set.add(getComponent(key));
122 }
123 return Collections.unmodifiableSet(set);
124 }
125
126 public Object getComponentMulticaster() {
127 throw new UnsupportedOperationException();
128 }
129
130 public Object getComponentMulticaster(boolean callInInstantiationOrder, boolean callUnmanagedComponents) {
131 throw new UnsupportedOperationException();
132 }
133
134 /***
135 * Adds a new Pico internals to this composite internals
136 * @param container
137 */
138 protected void addContainer(PicoContainer container) {
139 containers.add(container);
140 }
141
142 /***
143 * Removes a Pico internals from this composite internals
144 * @param container
145 */
146 protected void removeContainer(PicoContainer container) {
147 containers.remove(container);
148 }
149
150 }
This page was automatically generated by Maven