001 /*****************************************************************************
002 * Copyright (C) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 * Original code by *
009 *****************************************************************************/
010 package org.picocontainer.injectors;
011
012 import java.lang.reflect.Constructor;
013 import java.lang.reflect.InvocationTargetException;
014 import java.lang.reflect.Member;
015 import java.lang.reflect.Modifier;
016 import java.lang.reflect.Type;
017 import java.util.Arrays;
018 import java.util.LinkedList;
019 import java.util.List;
020 import java.util.Set;
021
022 import org.picocontainer.ComponentAdapter;
023 import org.picocontainer.ComponentMonitor;
024 import org.picocontainer.Injector;
025 import org.picocontainer.LifecycleStrategy;
026 import org.picocontainer.ObjectReference;
027 import org.picocontainer.Parameter;
028 import org.picocontainer.PicoCompositionException;
029 import org.picocontainer.PicoContainer;
030 import org.picocontainer.PicoVisitor;
031 import org.picocontainer.adapters.AbstractAdapter;
032 import org.picocontainer.parameters.ComponentParameter;
033
034 /**
035 * This ComponentAdapter will instantiate a new object for each call to
036 * {@link org.picocontainer.ComponentAdapter#getComponentInstance(PicoContainer, Class)}.
037 * That means that when used with a PicoContainer, getComponent will
038 * return a new object each time.
039 *
040 * @author Aslak Hellesøy
041 * @author Paul Hammant
042 * @author Jörg Schaible
043 * @author Mauro Talevi
044 */
045 @SuppressWarnings("serial")
046 public abstract class AbstractInjector<T> extends AbstractAdapter<T> implements LifecycleStrategy, Injector<T> {
047 /** The cycle guard for the verification. */
048 protected transient ThreadLocalCyclicDependencyGuard verifyingGuard;
049 /** The parameters to use for initialization. */
050 protected transient Parameter[] parameters;
051
052 /** The strategy used to control the lifecycle */
053 protected LifecycleStrategy lifecycleStrategy;
054 private final boolean useNames;
055
056 /**
057 * Constructs a new ComponentAdapter for the given key and implementation.
058 * @param componentKey the search key for this implementation
059 * @param componentImplementation the concrete implementation
060 * @param parameters the parameters to use for the initialization
061 * @param monitor the component monitor used by this ComponentAdapter
062 * @param lifecycleStrategy the lifecycle strategy used by this ComponentAdapter
063 * @throws org.picocontainer.injectors.AbstractInjector.NotConcreteRegistrationException if the implementation is not a concrete class
064 * @throws NullPointerException if one of the parameters is <code>null</code>
065 */
066 protected AbstractInjector(final Object componentKey, final Class<?> componentImplementation, final Parameter[] parameters,
067 final ComponentMonitor monitor, final LifecycleStrategy lifecycleStrategy, final boolean useNames) {
068 super(componentKey, componentImplementation, monitor);
069 this.useNames = useNames;
070 checkConcrete();
071 if (parameters != null) {
072 for (int i = 0; i < parameters.length; i++) {
073 if(parameters[i] == null) {
074 throw new NullPointerException("Parameter " + i + " is null");
075 }
076 }
077 }
078 this.parameters = parameters;
079 this.lifecycleStrategy = lifecycleStrategy;
080 }
081
082 public boolean useNames() {
083 return useNames;
084 }
085
086 private void checkConcrete() throws NotConcreteRegistrationException {
087 // Assert that the component class is concrete.
088 boolean isAbstract = (getComponentImplementation().getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT;
089 if (getComponentImplementation().isInterface() || isAbstract) {
090 throw new NotConcreteRegistrationException(getComponentImplementation());
091 }
092 }
093
094 /**
095 * Create default parameters for the given types.
096 *
097 * @param parameters the parameter types
098 * @return the array with the default parameters.
099 */
100 protected Parameter[] createDefaultParameters(final Type[] parameters) {
101 Parameter[] componentParameters = new Parameter[parameters.length];
102 for (int i = 0; i < parameters.length; i++) {
103 componentParameters[i] = ComponentParameter.DEFAULT;
104 }
105 return componentParameters;
106 }
107
108 public void verify(PicoContainer container) throws PicoCompositionException {
109 }
110
111 public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
112 return getComponentInstance(container, NOTHING.class);
113 }
114
115 public abstract T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException;
116
117 @Override
118 public void accept(final PicoVisitor visitor) {
119 super.accept(visitor);
120 if (parameters != null) {
121 for (Parameter parameter : parameters) {
122 parameter.accept(visitor);
123 }
124 }
125 }
126 public void start(final Object component) {
127 lifecycleStrategy.start(component);
128 }
129
130 public void stop(final Object component) {
131 lifecycleStrategy.stop(component);
132 }
133
134 public void dispose(final Object component) {
135 lifecycleStrategy.dispose(component);
136 }
137
138 public boolean hasLifecycle(final Class<?> type) {
139 return lifecycleStrategy.hasLifecycle(type);
140 }
141
142 public String getDescriptor() {
143 return "Asbtract Injector";
144 }
145
146 /**
147 * Instantiate an object with given parameters and respect the accessible flag.
148 *
149 * @param constructor the constructor to use
150 * @param parameters the parameters for the constructor
151 * @return the new object.
152 * @throws InstantiationException
153 * @throws IllegalAccessException
154 * @throws InvocationTargetException
155 */
156 protected T newInstance(final Constructor<T> constructor, final Object[] parameters) throws InstantiationException, IllegalAccessException, InvocationTargetException {
157 return constructor.newInstance(parameters);
158 }
159 /**
160 * inform monitor about component instantiation failure
161 * @param componentMonitor
162 * @param constructor
163 * @param e
164 * @param container
165 * @return
166 */
167 protected T caughtInstantiationException(final ComponentMonitor componentMonitor,
168 final Constructor<T> constructor,
169 final InstantiationException e, final PicoContainer container) {
170 // can't get here because checkConcrete() will catch it earlier, but see PICO-191
171 componentMonitor.instantiationFailed(container, this, constructor, e);
172 throw new PicoCompositionException("Should never get here");
173 }
174
175 /**
176 * inform monitor about access exception.
177 * @param componentMonitor
178 * @param constructor
179 * @param e
180 * @param container
181 * @return
182 */
183 protected T caughtIllegalAccessException(final ComponentMonitor componentMonitor,
184 final Constructor<T> constructor,
185 final IllegalAccessException e, final PicoContainer container) {
186 // can't get here because either filtered or access mode set
187 componentMonitor.instantiationFailed(container, this, constructor, e);
188 throw new PicoCompositionException(e);
189 }
190
191 /**
192 * inform monitor about exception while instantiating component
193 * @param componentMonitor
194 * @param member
195 * @param componentInstance
196 * @param e
197 * @return
198 */
199 protected T caughtInvocationTargetException(final ComponentMonitor componentMonitor,
200 final Member member,
201 final Object componentInstance, final InvocationTargetException e) {
202 componentMonitor.invocationFailed(member, componentInstance, e);
203 if (e.getTargetException() instanceof RuntimeException) {
204 throw (RuntimeException) e.getTargetException();
205 } else if (e.getTargetException() instanceof Error) {
206 throw (Error) e.getTargetException();
207 }
208 throw new PicoCompositionException(e.getTargetException());
209 }
210
211 protected Object caughtIllegalAccessException(final ComponentMonitor componentMonitor,
212 final Member member,
213 final Object componentInstance, final IllegalAccessException e) {
214 componentMonitor.invocationFailed(member, componentInstance, e);
215 throw new PicoCompositionException(e);
216 }
217
218 protected Type box(Type parameterType) {
219 if (parameterType instanceof Class && ((Class) parameterType).isPrimitive()) {
220 String parameterTypeName = ((Class) parameterType).getName();
221 if (parameterTypeName == "int") {
222 return Integer.class;
223 } else if (parameterTypeName == "boolean") {
224 return Boolean.class;
225 } else if (parameterTypeName == "long") {
226 return Long.class;
227 } else if (parameterTypeName == "float") {
228 return Float.class;
229 } else if (parameterTypeName == "double") {
230 return Double.class;
231 } else if (parameterTypeName == "char") {
232 return Character.class;
233 } else if (parameterTypeName == "byte") {
234 return Byte.class;
235 } else if (parameterTypeName == "short") {
236 return Short.class;
237 }
238 }
239 return parameterType;
240 }
241
242 /**
243 * Abstract utility class to detect recursion cycles.
244 * Derive from this class and implement {@link ThreadLocalCyclicDependencyGuard#run}.
245 * The method will be called by {@link ThreadLocalCyclicDependencyGuard#observe}. Select
246 * an appropriate guard for your scope. Any {@link ObjectReference} can be
247 * used as long as it is initialized with <code>Boolean.FALSE</code>.
248 *
249 * @author Jörg Schaible
250 */
251 static abstract class ThreadLocalCyclicDependencyGuard<T> extends ThreadLocal<Boolean> {
252
253 protected PicoContainer guardedContainer;
254
255 @Override
256 protected Boolean initialValue() {
257 return Boolean.FALSE;
258 }
259
260 /**
261 * Derive from this class and implement this function with the functionality
262 * to observe for a dependency cycle.
263 *
264 * @return a value, if the functionality result in an expression,
265 * otherwise just return <code>null</code>
266 */
267 public abstract T run();
268
269 /**
270 * Call the observing function. The provided guard will hold the {@link Boolean} value.
271 * If the guard is already <code>Boolean.TRUE</code> a {@link CyclicDependencyException}
272 * will be thrown.
273 *
274 * @param stackFrame the current stack frame
275 * @return the result of the <code>run</code> method
276 */
277 public final T observe(final Class<?> stackFrame) {
278 if (Boolean.TRUE.equals(get())) {
279 throw new CyclicDependencyException(stackFrame);
280 }
281 T result = null;
282 try {
283 set(Boolean.TRUE);
284 result = run();
285 } catch (final CyclicDependencyException e) {
286 e.push(stackFrame);
287 throw e;
288 } finally {
289 set(Boolean.FALSE);
290 }
291 return result;
292 }
293
294 public void setGuardedContainer(final PicoContainer container) {
295 this.guardedContainer = container;
296 }
297
298 }
299
300 @SuppressWarnings("serial")
301 public static class CyclicDependencyException extends PicoCompositionException {
302 private final List<Class> stack;
303
304 /**
305 * @param element
306 */
307 public CyclicDependencyException(final Class<?> element) {
308 super((Throwable)null);
309 this.stack = new LinkedList<Class>();
310 push(element);
311 }
312
313 /**
314 * @param element
315 */
316 public void push(final Class<?> element) {
317 stack.add(element);
318 }
319
320 public Class[] getDependencies() {
321 return stack.toArray(new Class[stack.size()]);
322 }
323
324 @Override
325 public String getMessage() {
326 return "Cyclic dependency: " + stack.toString();
327 }
328 }
329
330 /**
331 * Exception that is thrown as part of the introspection. Raised if a PicoContainer cannot resolve a
332 * type dependency because the registered {@link org.picocontainer.ComponentAdapter}s are not
333 * distinct.
334 *
335 * @author Paul Hammant
336 * @author Aslak Hellesøy
337 * @author Jon Tirsén
338 */
339 @SuppressWarnings("serial")
340 public static final class AmbiguousComponentResolutionException extends PicoCompositionException {
341
342
343 private Class<?> component;
344 private final Class<?> ambiguousDependency;
345 private final Object[] ambiguousComponentKeys;
346
347
348 /**
349 * Construct a new exception with the ambigous class type and the ambiguous component keys.
350 *
351 * @param ambiguousDependency the unresolved dependency type
352 * @param componentKeys the ambiguous keys.
353 */
354 public AmbiguousComponentResolutionException(final Class<?> ambiguousDependency, final Object[] componentKeys) {
355 super("");
356 this.ambiguousDependency = ambiguousDependency;
357 this.ambiguousComponentKeys = new Class[componentKeys.length];
358 System.arraycopy(componentKeys, 0, ambiguousComponentKeys, 0, componentKeys.length);
359 }
360
361 /**
362 * @return Returns a string containing the unresolved class type and the ambiguous keys.
363 */
364 @Override
365 public String getMessage() {
366 StringBuffer msg = new StringBuffer();
367 msg.append(component != null ? component : "<no-component>");
368 msg.append(" needs a '");
369 msg.append(ambiguousDependency.getName());
370 msg.append("' injected, but there are too many choices to inject. These:");
371 msg.append(Arrays.asList(getAmbiguousComponentKeys()));
372 msg.append(", refer http://picocontainer.org/ambiguous-injectable-help.html");
373 return msg.toString();
374 }
375
376 /**
377 * @return Returns the ambiguous component keys as array.
378 */
379 public Object[] getAmbiguousComponentKeys() {
380 return ambiguousComponentKeys;
381 }
382
383 public void setComponent(final Class<?> component) {
384 this.component = component;
385 }
386 }
387
388 /**
389 * Exception thrown when some of the component's dependencies are not satisfiable.
390 *
391 * @author Aslak Hellesøy
392 * @author Mauro Talevi
393 */
394 public static class UnsatisfiableDependenciesException extends PicoCompositionException {
395
396
397 private final ComponentAdapter<?> instantiatingComponentAdapter;
398 private final Set unsatisfiableDependencies;
399 private final Type unsatisfiedDependencyType;
400
401 /**
402 * The original container requesting the instantiation of the component.
403 */
404 private final PicoContainer leafContainer;
405
406 public UnsatisfiableDependenciesException(final ComponentAdapter<?> instantiatingComponentAdapter,
407 final Type unsatisfiedDependencyType, final Set unsatisfiableDependencies,
408 final PicoContainer leafContainer) {
409 super(instantiatingComponentAdapter.getComponentImplementation().getName() + " has unsatisfied dependency: " + unsatisfiedDependencyType
410 +" among unsatisfiable dependencies: "+unsatisfiableDependencies + " where " + leafContainer
411 + " was the leaf container being asked for dependencies.");
412 this.instantiatingComponentAdapter = instantiatingComponentAdapter;
413 this.unsatisfiableDependencies = unsatisfiableDependencies;
414 this.unsatisfiedDependencyType = unsatisfiedDependencyType;
415 this.leafContainer = leafContainer;
416 }
417
418 public ComponentAdapter<?> getUnsatisfiableComponentAdapter() {
419 return instantiatingComponentAdapter;
420 }
421
422 public Set getUnsatisfiableDependencies() {
423 return unsatisfiableDependencies;
424 }
425
426 public Type getUnsatisfiedDependencyType() {
427 return unsatisfiedDependencyType;
428 }
429
430 public PicoContainer getLeafContainer() {
431 return leafContainer;
432 }
433
434 }
435
436 /**
437 * @author Aslak Hellesoy
438 */
439 public static class NotConcreteRegistrationException extends PicoCompositionException {
440
441 private final Class<?> componentImplementation;
442
443 public NotConcreteRegistrationException(final Class<?> componentImplementation) {
444 super("Bad Access: '" + componentImplementation.getName() + "' is not instantiable");
445 this.componentImplementation = componentImplementation;
446 }
447
448 public Class<?> getComponentImplementation() {
449 return componentImplementation;
450 }
451 }
452 }