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