1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| package org.picocontainer.alternatives; |
11 |
| |
12 |
| import org.picocontainer.ComponentAdapter; |
13 |
| import org.picocontainer.MutablePicoContainer; |
14 |
| import org.picocontainer.Parameter; |
15 |
| import org.picocontainer.PicoContainer; |
16 |
| import org.picocontainer.PicoRegistrationException; |
17 |
| import org.picocontainer.LifecycleManager; |
18 |
| import org.picocontainer.defaults.CachingComponentAdapter; |
19 |
| import org.picocontainer.defaults.CachingComponentAdapterFactory; |
20 |
| import org.picocontainer.defaults.ComponentAdapterFactory; |
21 |
| import org.picocontainer.defaults.DefaultComponentAdapterFactory; |
22 |
| import org.picocontainer.defaults.DefaultLifecycleManager; |
23 |
| |
24 |
| import java.io.Serializable; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public class ImplementationHidingCachingPicoContainer extends AbstractDelegatingMutablePicoContainer implements Serializable { |
36 |
| |
37 |
| private CachingComponentAdapterFactory caf; |
38 |
| private LifecycleManager lifecycleManager; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
84
| public ImplementationHidingCachingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent) {
|
45 |
84
| this(parent, new CachingComponentAdapterFactory(caf), new DefaultLifecycleManager());
|
46 |
| } |
47 |
| |
48 |
10
| public ImplementationHidingCachingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent, LifecycleManager lifecyleManager) {
|
49 |
10
| this(parent, new CachingComponentAdapterFactory(caf), lifecyleManager);
|
50 |
| } |
51 |
| |
52 |
102
| private ImplementationHidingCachingPicoContainer(PicoContainer parent, CachingComponentAdapterFactory caf, LifecycleManager lifecycleManager) {
|
53 |
102
| super(new ImplementationHidingPicoContainer(caf, parent, lifecycleManager));
|
54 |
102
| this.caf = caf;
|
55 |
102
| this.lifecycleManager = lifecycleManager;
|
56 |
| } |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
82
| public ImplementationHidingCachingPicoContainer(PicoContainer parent) {
|
62 |
82
| this(new DefaultComponentAdapterFactory(), parent);
|
63 |
| } |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
12
| public ImplementationHidingCachingPicoContainer() {
|
70 |
12
| this(null);
|
71 |
| } |
72 |
| |
73 |
30
| public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation) throws PicoRegistrationException {
|
74 |
30
| if (componentKey instanceof Class) {
|
75 |
16
| Class clazz = (Class) componentKey;
|
76 |
16
| if (clazz.isInterface()) {
|
77 |
14
| ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, null);
|
78 |
14
| return getDelegate().registerComponent(new CachingComponentAdapter(new ImplementationHidingComponentAdapter(delegate, true)));
|
79 |
| } |
80 |
| } |
81 |
16
| return getDelegate().registerComponentImplementation(componentKey, componentImplementation);
|
82 |
| } |
83 |
| |
84 |
10
| public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, Parameter[] parameters) throws PicoRegistrationException {
|
85 |
10
| if (componentKey instanceof Class) {
|
86 |
4
| Class clazz = (Class) componentKey;
|
87 |
4
| if (clazz.isInterface()) {
|
88 |
2
| ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, parameters);
|
89 |
2
| ImplementationHidingComponentAdapter ihDelegate = new ImplementationHidingComponentAdapter(delegate, true);
|
90 |
2
| return getDelegate().registerComponent(new CachingComponentAdapter(ihDelegate));
|
91 |
| } |
92 |
| } |
93 |
8
| return getDelegate().registerComponentImplementation(componentKey, componentImplementation, parameters);
|
94 |
| } |
95 |
| |
96 |
| |
97 |
8
| public MutablePicoContainer makeChildContainer() {
|
98 |
8
| ImplementationHidingCachingPicoContainer pc = new ImplementationHidingCachingPicoContainer(this, caf, lifecycleManager);
|
99 |
8
| getDelegate().addChildContainer(pc);
|
100 |
8
| return pc;
|
101 |
| |
102 |
| } |
103 |
| |
104 |
| } |