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