1 package org.picocontainer.internals;
2
3 import org.picocontainer.PicoContainer;
4 import org.picocontainer.PicoInitializationException;
5 import org.picocontainer.defaults.UnsatisfiedDependencyInstantiationException;
6 import org.picocontainer.internals.Parameter;
7 import org.picocontainer.internals.ComponentRegistry;
8 import org.picocontainer.internals.ComponentSpecification;
9
10 import java.io.Serializable;
11
12 /***
13 * @author Jon Tirsen (tirsen@codehaus.org)
14 * @version $Revision: 1.4 $
15 */
16 public class ComponentParameter implements Parameter, Serializable {
17
18 private Object componentKey;
19
20 public ComponentParameter() {
21 }
22
23 public ComponentParameter(Object componentKey) {
24 this.componentKey = componentKey;
25 }
26
27 public Object resolve(ComponentRegistry componentRegistry, ComponentSpecification compSpec, Class requestedType)
28 throws PicoInitializationException {
29
30 Object componentInstance;
31 ComponentSpecification componentSpecification = null;
32
33 if (componentKey != null) {
34
35 // is specified dependency already instantiated?
36 componentInstance = componentRegistry.getComponentInstance(componentKey);
37
38 if (componentInstance == null) {
39 // try to find the component based on the key
40 componentSpecification = componentRegistry.getComponentSpec(componentKey);
41 }
42
43 } else {
44
45 // try to find it directly using the requestType as the key
46 componentInstance = componentRegistry.getComponentInstance(requestedType);
47
48 if (componentInstance == null) {
49
50 // is there already an instantiated instance that satisfies the dependency?
51 componentInstance = componentRegistry.findImplementingComponent(requestedType);
52
53
54 if (componentInstance == null) {
55 // try to find components that satisfy the interface (implements the component service asked for)
56 componentSpecification = componentRegistry.findImplementingComponentSpecification(requestedType);
57 }
58 }
59
60 }
61
62 if (componentSpecification != null) {
63 // if the component does not exist yet, instantiate it
64 componentInstance = componentRegistry.createComponent(componentSpecification);
65 }
66
67 if (componentInstance == null) {
68 throw new UnsatisfiedDependencyInstantiationException(compSpec.getComponentImplementation(), componentKey, requestedType);
69 }
70
71 return componentInstance;
72 }
73 }
This page was automatically generated by Maven