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