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.adapters;
011    
012    import org.picocontainer.Behavior;
013    import org.picocontainer.PicoContainer;
014    import org.picocontainer.LifecycleStrategy;
015    import org.picocontainer.ComponentMonitor;
016    import org.picocontainer.PicoCompositionException;
017    import org.picocontainer.adapters.AbstractAdapter;
018    import org.picocontainer.lifecycle.NullLifecycleStrategy;
019    import org.picocontainer.monitors.NullComponentMonitor;
020    
021    import java.lang.reflect.Type;
022    
023    /**
024     * <p>
025     * Component adapter which wraps a component instance.
026     * </p>
027     * <p>
028     * This component adapter supports both a {@link Behavior Behavior} and a
029     * {@link org.picocontainer.LifecycleStrategy LifecycleStrategy} to control the lifecycle of the component.
030     * The lifecycle manager methods simply delegate to the lifecycle strategy methods 
031     * on the component instance.
032     * </p>
033     * 
034     * @author Aslak Helles&oslash;y
035     * @author Paul Hammant
036     * @author Mauro Talevi
037     */
038    @SuppressWarnings("serial")
039    public final class InstanceAdapter<T> extends AbstractAdapter<T> implements Behavior<T>, LifecycleStrategy {
040        
041    
042            /**
043             * The actual instance of the component.
044             */
045            private final T componentInstance;
046            
047            /**
048             * Lifecycle Strategy for the component adpater.
049             */
050        private final LifecycleStrategy lifecycleStrategy;
051    
052        
053        public InstanceAdapter(Object componentKey, T componentInstance, LifecycleStrategy lifecycleStrategy, ComponentMonitor componentMonitor) throws PicoCompositionException {
054            super(componentKey, getInstanceClass(componentInstance), componentMonitor);
055            this.componentInstance = componentInstance;
056            this.lifecycleStrategy = lifecycleStrategy;
057        }
058    
059        public InstanceAdapter(Object componentKey, T componentInstance) {
060            this(componentKey,componentInstance,new NullLifecycleStrategy(),new NullComponentMonitor());    
061        }
062    
063        public InstanceAdapter(Object componentKey, T componentInstance, LifecycleStrategy lifecycleStrategy) {
064            this(componentKey,componentInstance,lifecycleStrategy,new NullComponentMonitor());  
065        }
066    
067        public InstanceAdapter(Object componentKey, T componentInstance,  ComponentMonitor componentMonitor) {
068            this(componentKey,componentInstance,new NullLifecycleStrategy(),componentMonitor);  
069        }
070         
071         private static Class getInstanceClass(Object componentInstance) {
072            if (componentInstance == null) {
073                throw new NullPointerException("componentInstance cannot be null");
074            }
075            return componentInstance.getClass();
076        }
077    
078        public T getComponentInstance(PicoContainer container, Type into) {
079            return componentInstance;
080        }
081        
082        public void verify(PicoContainer container) {
083        }
084    
085        public String getDescriptor() {
086            return "Instance-";
087        }
088    
089        public void start(PicoContainer container) {
090            start(componentInstance);
091        }
092    
093        public void stop(PicoContainer container) {
094            stop(componentInstance);
095        }
096    
097        public void dispose(PicoContainer container) {
098            dispose(componentInstance);
099        }
100    
101        public boolean componentHasLifecycle() {
102            return hasLifecycle(componentInstance.getClass());
103        }
104    
105        // ~~~~~~~~ LifecycleStrategy ~~~~~~~~
106        
107        public void start(Object component) {
108            lifecycleStrategy.start(componentInstance);
109        }
110    
111        public void stop(Object component) {
112            lifecycleStrategy.stop(componentInstance);
113        }
114    
115        public void dispose(Object component) {
116            lifecycleStrategy.dispose(componentInstance);
117        }
118    
119        public boolean hasLifecycle(Class type) {
120            return lifecycleStrategy.hasLifecycle(type);
121        }
122    
123    }