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 *****************************************************************************/ 009 package org.picocontainer.injectors; 010 011 import org.picocontainer.ComponentAdapter; 012 import org.picocontainer.ComponentMonitor; 013 import org.picocontainer.InjectionFactory; 014 import org.picocontainer.LifecycleStrategy; 015 import org.picocontainer.Parameter; 016 import org.picocontainer.PicoCompositionException; 017 import org.picocontainer.PicoContainer; 018 import org.picocontainer.Characteristics; 019 import org.picocontainer.behaviors.AbstractBehaviorFactory; 020 021 import java.lang.reflect.Type; 022 import java.util.Properties; 023 024 public class Reinjection extends CompositeInjection { 025 026 public Reinjection(InjectionFactory reinjectionFactory, final PicoContainer parent) { 027 super(new AbstractInjectionFactory() { 028 public <T> ComponentAdapter<T> createComponentAdapter( 029 ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, 030 Properties componentProperties, final Object componentKey, Class<T> componentImplementation, 031 Parameter... parameters) throws PicoCompositionException { 032 boolean useNames = AbstractBehaviorFactory.arePropertiesPresent(componentProperties, Characteristics.USE_NAMES, true); 033 return new ReinjectionInjector(componentKey, componentImplementation, parameters, componentMonitor, parent, useNames); 034 } 035 }, reinjectionFactory); 036 } 037 038 private static class ReinjectionInjector<T> extends AbstractInjector { 039 private final PicoContainer parent; 040 041 public ReinjectionInjector(Object componentKey, Class<T> componentImplementation, Parameter[] parameters, ComponentMonitor componentMonitor, PicoContainer parent, boolean useNames) { 042 super(componentKey, componentImplementation, parameters, componentMonitor, useNames); 043 this.parent = parent; 044 } 045 046 public Object getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException { 047 return parent.getComponent(getComponentKey()); 048 } 049 050 } 051 }