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.ComponentAdapter;
018 import org.picocontainer.adapters.AbstractAdapter;
019
020 /**
021 * <p>
022 * Component adapter which wraps a component instance.
023 * </p>
024 * <p>
025 * This component adapter supports both a {@link Behavior Behavior} and a
026 * {@link org.picocontainer.LifecycleStrategy LifecycleStrategy} to control the lifecycle of the component.
027 * The lifecycle manager methods simply delegate to the lifecycle strategy methods
028 * on the component instance.
029 * </p>
030 *
031 * @author Aslak Hellesøy
032 * @author Paul Hammant
033 * @author Mauro Talevi
034 */
035 public final class InstanceAdapter extends AbstractAdapter implements Behavior, LifecycleStrategy {
036 private final Object componentInstance;
037 private final LifecycleStrategy lifecycleStrategy;
038
039 public InstanceAdapter(Object componentKey, Object componentInstance, LifecycleStrategy lifecycleStrategy, ComponentMonitor componentMonitor) throws PicoCompositionException {
040 super(componentKey, getInstanceClass(componentInstance), componentMonitor);
041 this.componentInstance = componentInstance;
042 this.lifecycleStrategy = lifecycleStrategy;
043 }
044
045 private static Class getInstanceClass(Object componentInstance) {
046 if (componentInstance == null) {
047 throw new NullPointerException("componentInstance cannot be null");
048 }
049 return componentInstance.getClass();
050 }
051
052 public Object getComponentInstance(PicoContainer container) {
053 return componentInstance;
054 }
055
056 public void verify(PicoContainer container) {
057 }
058
059 public void start(PicoContainer container) {
060 start(componentInstance);
061 }
062
063 public void stop(PicoContainer container) {
064 stop(componentInstance);
065 }
066
067 public void dispose(PicoContainer container) {
068 dispose(componentInstance);
069 }
070
071 public boolean componentHasLifecycle() {
072 return hasLifecycle(componentInstance.getClass());
073 }
074
075 // ~~~~~~~~ LifecycleStrategy ~~~~~~~~
076
077 public void start(Object component) {
078 lifecycleStrategy.start(componentInstance);
079 }
080
081 public void stop(Object component) {
082 lifecycleStrategy.stop(componentInstance);
083 }
084
085 public void dispose(Object component) {
086 lifecycleStrategy.dispose(componentInstance);
087 }
088
089 public boolean hasLifecycle(Class type) {
090 return lifecycleStrategy.hasLifecycle(type);
091 }
092
093 public String toString() {
094 return "Instance-" + super.toString();
095 }
096 }