001    package org.picocontainer.injectors;
002    
003    import org.picocontainer.*;
004    import org.picocontainer.Injector;
005    import org.picocontainer.lifecycle.AbstractMonitoringLifecycleStrategy;
006    import org.picocontainer.lifecycle.NullLifecycleStrategy;
007    
008    import java.io.Serializable;
009    import java.lang.reflect.Type;
010    
011    public abstract class AbstractInjectionFactory implements InjectionFactory, Serializable {
012    
013        public void verify(PicoContainer container) {
014        }
015    
016        public final void accept(PicoVisitor visitor) {
017            visitor.visitComponentFactory(this);
018        }
019    
020        protected ComponentAdapter wrapLifeCycle(final Injector injector, LifecycleStrategy lifecycleStrategy) {
021            if (lifecycleStrategy instanceof NullLifecycleStrategy) {
022                return injector;
023            } else {
024                return new LifecycleAdapter(injector, lifecycleStrategy);
025            }
026        }
027    
028        private static class LifecycleAdapter implements ComponentAdapter, LifecycleStrategy, ComponentMonitorStrategy, Serializable {
029            private final Injector injector;
030            private final LifecycleStrategy lifecycleStrategy;
031    
032            public LifecycleAdapter(Injector injector, LifecycleStrategy lifecycleStrategy) {
033                this.injector = injector;
034                this.lifecycleStrategy = lifecycleStrategy;
035            }
036    
037            public Object getComponentKey() {
038                return injector.getComponentKey();
039            }
040    
041            public Class getComponentImplementation() {
042                return injector.getComponentImplementation();
043            }
044    
045            public Object getComponentInstance(PicoContainer container) throws PicoCompositionException {
046                return injector.getComponentInstance(container);
047            }
048    
049            public Object getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
050                return injector.getComponentInstance(container, into);
051            }
052    
053            public void verify(PicoContainer container) throws PicoCompositionException {
054                injector.verify(container);
055            }
056    
057            public void accept(PicoVisitor visitor) {
058                injector.accept(visitor);
059            }
060    
061            public ComponentAdapter getDelegate() {
062                return injector;
063            }
064    
065            public ComponentAdapter findAdapterOfType(Class adapterType) {
066                return injector.findAdapterOfType(adapterType);
067            }
068    
069            public String getDescriptor() {
070                return "LifecycleAdapter";
071            }
072    
073            public String toString() {
074                return getDescriptor() + ":" + injector.toString();
075            }
076    
077            public void start(Object component) {
078                lifecycleStrategy.start(component);
079            }
080    
081            public void stop(Object component) {
082                lifecycleStrategy.stop(component);
083            }
084    
085            public void dispose(Object component) {
086                lifecycleStrategy.dispose(component);
087            }
088    
089            public boolean hasLifecycle(Class<?> type) {
090                return lifecycleStrategy.hasLifecycle(type);
091            }
092    
093            public boolean isLazy(ComponentAdapter<?> adapter) {
094                return lifecycleStrategy.isLazy(adapter);
095            }
096    
097            public void changeMonitor(ComponentMonitor monitor) {
098                if (injector instanceof ComponentMonitorStrategy) {
099                    ((ComponentMonitorStrategy) injector).changeMonitor(monitor);
100                }
101            }
102    
103            public ComponentMonitor currentMonitor() {
104                if (injector instanceof ComponentMonitorStrategy) {
105                    return ((ComponentMonitorStrategy) injector).currentMonitor();
106                }
107                return null;
108            }
109        }
110    }