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 /*
12 TODO (Aslak):
13
14 1) Factor out a DependencyAnalyzer:
15 public interface DependencyAnalyzer {
16 InstantiationSpecification[] getOrderedInstantiationSpecifications();
17 }
18
19 ConstructorDependencyAnalyzer would emerge from refactoring this class.
20
21 2) Refactor the ContainerFactory's createComponent method to take a
22 InstantiationSpecification argument. This class/intf should contain'
23 everything needed to instantiate a component.
24
25 */
26
27 package picocontainer.hierarchical;
28
29
30 import picocontainer.defaults.DefaultPicoContainer;
31 import picocontainer.ClassRegistrationPicoContainer;
32 import picocontainer.ComponentFactory;
33 import picocontainer.PicoContainer;
34 import picocontainer.defaults.DefaultComponentFactory;
35 import picocontainer.defaults.NullContainer;
36
37 import java.util.List;
38 import java.util.Arrays;
39 import java.util.HashSet;
40 import java.util.Set;
41
42 public class HierarchicalPicoContainer extends DefaultPicoContainer implements ClassRegistrationPicoContainer {
43
44 private final PicoContainer parentContainer;
45
46 public HierarchicalPicoContainer(ComponentFactory componentFactory, PicoContainer parentContainer) {
47 super(componentFactory);
48
49 if (parentContainer == null) {
50 throw new NullPointerException("parentContainer cannot be null");
51 }
52 this.parentContainer = parentContainer;
53 }
54
55 public static class Default extends HierarchicalPicoContainer {
56 public Default() {
57 super(new DefaultComponentFactory(), new NullContainer());
58 }
59
60 }
61
62 public static class WithParentContainer extends HierarchicalPicoContainer {
63 public WithParentContainer(PicoContainer parentContainer) {
64 super(new DefaultComponentFactory(), parentContainer);
65 }
66 }
67
68 public static class WithComponentFactory extends HierarchicalPicoContainer {
69 public WithComponentFactory(ComponentFactory componentFactory) {
70 super(componentFactory, new NullContainer());
71 }
72 }
73
74 public Object getComponent(Class componentType) {
75 // First look in myself
76 Object result = super.getComponent(componentType);
77
78 // Then look in parent if we had nothing
79 if (result == null) {
80 result = parentContainer.getComponent(componentType);
81 }
82 return result;
83 }
84
85 public Class[] getComponentTypes() {
86 // Get my own types
87 List myTypes = Arrays.asList(super.getComponentTypes());
88
89 // Get those from my parent.
90 Set types = new HashSet(myTypes);
91 types.addAll(Arrays.asList(parentContainer.getComponentTypes()));
92
93 return (Class[]) types.toArray(new Class[types.size()]);
94 }
95
96 protected Object getComponentForParam(Class parameter) throws AmbiguousComponentResolutionException {
97 // If the parent container has the component type
98 // it can be seen to be dominant. No need to check
99 // for ambiguities
100 if (parentContainer.hasComponent(parameter)) {
101 return parentContainer.getComponent(parameter);
102 } else {
103 return super.getComponentForParam(parameter);
104 }
105 }
106 }
This page was automatically generated by Maven