1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.di.impl.providers;
17
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import net.sf.jguiraffe.di.Dependency;
22 import net.sf.jguiraffe.di.DependencyProvider;
23 import net.sf.jguiraffe.di.impl.ConstructorInvocation;
24
25 /**
26 * <p>
27 * A simple bean provider that creates new bean instances by invoking a
28 * constructor.
29 * </p>
30 * <p>
31 * This <code>BeanProvider</code> class is initialized with a
32 * {@link ConstructorInvocation} object. The
33 * <code>getBean()</code> method will trigger this invocation object and
34 * return the newly created instance.
35 * </p>
36 * <p>
37 * The <code>ConstructorInvocation</code> also determines the dependencies of
38 * this bean provider: these are the parameters to be passed to the constructor,
39 * which can be arbitrary other beans defined in the current bean store.
40 * </p>
41 * <p>
42 * A <code>ConstructorBeanProvider</code> is intended to be used together with
43 * another life-cycle-aware bean provider. It will then be used for creating new
44 * bean instances while its owning bean provider is responsible for further
45 * initialization and life-cycle support.
46 * </p>
47 *
48 * @author Oliver Heger
49 * @version $Id: ConstructorBeanProvider.java 205 2012-01-29 18:29:57Z oheger $
50 */
51 public class ConstructorBeanProvider extends SimpleBeanProvider
52 {
53 /** Stores the invocation object. */
54 private ConstructorInvocation invocation;
55
56 /**
57 * Creates a new instance of <code>ConstructorBeanProvider</code> and
58 * initializes it with the constructor invocation to be called. This object
59 * specifies the constructor to be invoked.
60 *
61 * @param ctorinv the <code>ConstructorInvocation</code> (must not be
62 * <b>null</b>)
63 * @throws IllegalArgumentException if the constructor invocation is
64 * undefined
65 */
66 public ConstructorBeanProvider(ConstructorInvocation ctorinv)
67 {
68 if (ctorinv == null)
69 {
70 throw new IllegalArgumentException(
71 "Constructor invocation must not be null!");
72 }
73 invocation = ctorinv;
74 }
75
76 /**
77 * Returns the <code>ConstructorInvocation</code> object that determines
78 * the constructor to be invoked.
79 *
80 * @return the used constructor invocation
81 */
82 public ConstructorInvocation getInvocation()
83 {
84 return invocation;
85 }
86
87 /**
88 * Returns the bean managed by this provider. This implementation will
89 * invoke the specified constructor for creating the new bean instance.
90 *
91 * @param dependencyProvider the dependency provider
92 * @return the newly created bean instance
93 * @throws net.sf.jguiraffe.di.InjectionException if an error occurs when
94 * creating the bean
95 */
96 public Object getBean(DependencyProvider dependencyProvider)
97 {
98 return getInvocation().invoke(dependencyProvider);
99 }
100
101 /**
102 * Returns the class of the managed bean. This class is also determined by
103 * the constructor invocation.
104 *
105 * @param dependencyProvider the dependency provider
106 * @return the class of the managed bean
107 */
108 public Class<?> getBeanClass(DependencyProvider dependencyProvider)
109 {
110 return getInvocation().getTargetClass().getTargetClass(
111 dependencyProvider);
112 }
113
114 /**
115 * Returns the dependencies of this bean provider. This implementation
116 * returns the dependencies required for the constructor invocation (i.e.
117 * the parameters to be passed to the constructor).
118 *
119 * @return a set with the dependencies of this bean provider
120 */
121 @Override
122 public Set<Dependency> getDependencies()
123 {
124 return new HashSet<Dependency>(getInvocation()
125 .getParameterDependencies());
126 }
127
128 /**
129 * Returns a string representation of this object. This string also contains
130 * information about the constructor that will be invoked.
131 *
132 * @return a string for this object
133 */
134 @Override
135 public String toString()
136 {
137 StringBuilder buf = new StringBuilder(getClass().getName());
138 buf.append('@').append(System.identityHashCode(this));
139 buf.append("[ ctor = ");
140 buf.append(getInvocation()).append(" ]");
141 return buf.toString();
142 }
143 }