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