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 *****************************************************************************/
010package org.picocontainer.adapters;
011
012import org.picocontainer.Behavior;
013import org.picocontainer.ComponentAdapter;
014import org.picocontainer.ComponentLifecycle;
015import org.picocontainer.ComponentMonitor;
016import org.picocontainer.LifecycleStrategy;
017import org.picocontainer.PicoCompositionException;
018import org.picocontainer.PicoContainer;
019import org.picocontainer.lifecycle.NullLifecycleStrategy;
020import org.picocontainer.monitors.NullComponentMonitor;
021
022import 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")
040public 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    @Override
091    public String toString() {
092        Object componentKey = getComponentKey();
093        if (componentKey instanceof Class) {
094            componentKey = "of " + ((Class) componentKey).getName();
095        }
096        return getDescriptor() + componentKey;
097    }
098
099    public void start(PicoContainer container) {
100        start(componentInstance);
101    }
102
103    public void stop(PicoContainer container) {
104        stop(componentInstance);
105    }
106
107    public void dispose(PicoContainer container) {
108        dispose(componentInstance);
109    }
110
111    public boolean componentHasLifecycle() {
112        return hasLifecycle(componentInstance.getClass());
113    }
114
115    public boolean isStarted() {
116        return started;
117    }
118
119    // ~~~~~~~~ LifecycleStrategy ~~~~~~~~
120
121    public void start(Object component) {
122        lifecycleStrategy.start(componentInstance);
123        started = true;
124    }
125
126    public void stop(Object component) {
127        lifecycleStrategy.stop(componentInstance);
128        started = false;
129    }
130
131    public void dispose(Object component) {
132        lifecycleStrategy.dispose(componentInstance);
133    }
134
135    public boolean hasLifecycle(Class<?> type) {
136        return lifecycleStrategy.hasLifecycle(type);
137    }
138
139    public boolean isLazy(ComponentAdapter<?> adapter) {
140        return lifecycleStrategy.isLazy(adapter);
141    }
142}