|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ImplementationHidingCachingPicoContainer.java | 100% | 100% | 100% | 100% |
|
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 |
* Original code by the committers *
|
|
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.alternatives.AbstractDelegatingMutablePicoContainer;
|
|
18 |
import org.picocontainer.alternatives.ImplementationHidingComponentAdapter;
|
|
19 |
import org.picocontainer.alternatives.ImplementationHidingPicoContainer;
|
|
20 |
import org.picocontainer.defaults.CachingComponentAdapter;
|
|
21 |
import org.picocontainer.defaults.CachingComponentAdapterFactory;
|
|
22 |
import org.picocontainer.defaults.ComponentAdapterFactory;
|
|
23 |
import org.picocontainer.defaults.DefaultComponentAdapterFactory;
|
|
24 |
|
|
25 |
import java.io.Serializable;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* This special MutablePicoContainer hides implementations of components if the key is an interface.
|
|
29 |
* It's very simple. Instances that are registered directly and components registered without key
|
|
30 |
* are not hidden.
|
|
31 |
*
|
|
32 |
* @author Paul Hammant
|
|
33 |
* @version $Revision: 1.1 $
|
|
34 |
* @since 1.1
|
|
35 |
*/
|
|
36 |
public class ImplementationHidingCachingPicoContainer extends AbstractDelegatingMutablePicoContainer implements Serializable { |
|
37 |
|
|
38 |
private CachingComponentAdapterFactory caf;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Creates a new container with a parent container.
|
|
42 |
*/
|
|
43 | 98 |
public ImplementationHidingCachingPicoContainer(ComponentAdapterFactory caf, PicoContainer parent) {
|
44 | 98 |
this(parent, new CachingComponentAdapterFactory(caf)); |
45 |
} |
|
46 |
|
|
47 | 98 |
private ImplementationHidingCachingPicoContainer(PicoContainer parent, CachingComponentAdapterFactory caf) {
|
48 | 98 |
super(new ImplementationHidingPicoContainer(caf, parent)); |
49 | 98 |
this.caf =caf;
|
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Creates a new container with a parent container.
|
|
54 |
*/
|
|
55 | 96 |
public ImplementationHidingCachingPicoContainer(PicoContainer parent) {
|
56 | 96 |
this(new DefaultComponentAdapterFactory(), parent); |
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Creates a new container with no parent container.
|
|
62 |
*/
|
|
63 | 12 |
public ImplementationHidingCachingPicoContainer() {
|
64 | 12 |
this(null); |
65 |
} |
|
66 |
|
|
67 | 24 |
public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation) throws PicoRegistrationException { |
68 | 24 |
if (componentKey instanceof Class) { |
69 | 16 |
Class clazz = (Class) componentKey; |
70 | 16 |
if (clazz.isInterface()) {
|
71 | 14 |
ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, null);
|
72 | 14 |
return getDelegate().registerComponent(new CachingComponentAdapter(new ImplementationHidingComponentAdapter(delegate, true))); |
73 |
} |
|
74 |
} |
|
75 | 10 |
return getDelegate().registerComponentImplementation(componentKey, componentImplementation);
|
76 |
} |
|
77 |
|
|
78 | 10 |
public ComponentAdapter registerComponentImplementation(Object componentKey, Class componentImplementation, Parameter[] parameters) throws PicoRegistrationException { |
79 | 10 |
if (componentKey instanceof Class) { |
80 | 4 |
Class clazz = (Class) componentKey; |
81 | 4 |
if (clazz.isInterface()) {
|
82 | 2 |
ComponentAdapter delegate = caf.createComponentAdapter(componentKey, componentImplementation, parameters); |
83 | 2 |
ImplementationHidingComponentAdapter ihDelegate = new ImplementationHidingComponentAdapter(delegate, true); |
84 | 2 |
return getDelegate().registerComponent(new CachingComponentAdapter(ihDelegate)); |
85 |
} |
|
86 |
} |
|
87 | 8 |
return getDelegate().registerComponentImplementation(componentKey, componentImplementation, parameters);
|
88 |
} |
|
89 |
|
|
90 |
|
|
91 | 6 |
public MutablePicoContainer makeChildContainer() {
|
92 | 6 |
ImplementationHidingCachingPicoContainer pc = new ImplementationHidingCachingPicoContainer(this); |
93 | 6 |
getDelegate().addChildContainer(pc); |
94 | 6 |
return pc;
|
95 |
|
|
96 |
} |
|
97 |
|
|
98 |
} |
|
99 |
|
|