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 Jon Tirsen *
009: *****************************************************************************/package org.picocontainer.behaviors;
010:
011: import java.io.Serializable;
012:
013: import org.picocontainer.ComponentAdapter;
014: import org.picocontainer.ComponentMonitor;
015: import org.picocontainer.Behavior;
016: import org.picocontainer.PicoContainer;
017: import org.picocontainer.PicoCompositionException;
018: import org.picocontainer.PicoVisitor;
019: import org.picocontainer.ComponentMonitorStrategy;
020: import org.picocontainer.LifecycleStrategy;
021:
022: /**
023: * <p>
024: * Component adapter which decorates another adapter.
025: * </p>
026: * <p>
027: * This adapter supports a {@link org.picocontainer.ComponentMonitorStrategy component monitor strategy}
028: * and will propagate change of monitor to the delegate if the delegate itself
029: * support the monitor strategy.
030: * </p>
031: * <p>
032: * This adapter also supports a {@link Behavior lifecycle manager} and a
033: * {@link org.picocontainer.LifecycleStrategy lifecycle strategy} if the delegate does.
034: * </p>
035: *
036: * @author Jon Tirsen
037: * @author Aslak Hellesoy
038: * @author Mauro Talevi
039: */
040: public abstract class AbstractBehavior<T> implements Behavior<T>,
041: ComponentMonitorStrategy, LifecycleStrategy, Serializable {
042:
043: protected final ComponentAdapter<T> delegate;
044:
045: public AbstractBehavior(ComponentAdapter<T> delegate) {
046: this .delegate = delegate;
047: }
048:
049: public Object getComponentKey() {
050: return delegate.getComponentKey();
051: }
052:
053: public Class<T> getComponentImplementation() {
054: return delegate.getComponentImplementation();
055: }
056:
057: public T getComponentInstance(PicoContainer container)
058: throws PicoCompositionException {
059: return (T) delegate.getComponentInstance(container);
060: }
061:
062: public void verify(PicoContainer container)
063: throws PicoCompositionException {
064: delegate.verify(container);
065: }
066:
067: public final ComponentAdapter<T> getDelegate() {
068: return delegate;
069: }
070:
071: @SuppressWarnings("unchecked")
072: public final <U extends ComponentAdapter> U findAdapterOfType(
073: Class<U> componentAdapterType) {
074: if (componentAdapterType.isAssignableFrom(this .getClass())) {
075: return (U) this ;
076: } else if (componentAdapterType.isAssignableFrom(delegate
077: .getClass())) {
078: return (U) delegate;
079: } else {
080: return delegate.findAdapterOfType(componentAdapterType);
081: }
082: }
083:
084: public void accept(PicoVisitor visitor) {
085: visitor.visitComponentAdapter(this );
086: delegate.accept(visitor);
087: }
088:
089: /**
090: * Delegates change of monitor if the delegate supports
091: * a component monitor strategy.
092: * {@inheritDoc}
093: */
094: public void changeMonitor(ComponentMonitor monitor) {
095: if (delegate instanceof ComponentMonitorStrategy) {
096: ((ComponentMonitorStrategy) delegate)
097: .changeMonitor(monitor);
098: }
099: }
100:
101: /**
102: * Returns delegate's current monitor if the delegate supports
103: * a component monitor strategy.
104: * {@inheritDoc}
105: * @throws PicoCompositionException if no component monitor is found in delegate
106: */
107: public ComponentMonitor currentMonitor() {
108: if (delegate instanceof ComponentMonitorStrategy) {
109: return ((ComponentMonitorStrategy) delegate)
110: .currentMonitor();
111: }
112: throw new PicoCompositionException(
113: "No component monitor found in delegate");
114: }
115:
116: /**
117: * Invokes delegate start method if the delegate is a Behavior
118: * {@inheritDoc}
119: */
120: public void start(PicoContainer container) {
121: if (delegate instanceof Behavior) {
122: ((Behavior<?>) delegate).start(container);
123: }
124: }
125:
126: /**
127: * Invokes delegate stop method if the delegate is a Behavior
128: * {@inheritDoc}
129: */
130: public void stop(PicoContainer container) {
131: if (delegate instanceof Behavior) {
132: ((Behavior<?>) delegate).stop(container);
133: }
134: }
135:
136: /**
137: * Invokes delegate dispose method if the delegate is a Behavior
138: * {@inheritDoc}
139: */
140: public void dispose(PicoContainer container) {
141: if (delegate instanceof Behavior) {
142: ((Behavior<?>) delegate).dispose(container);
143: }
144: }
145:
146: /**
147: * Invokes delegate hasLifecycle method if the delegate is a Behavior
148: * {@inheritDoc}
149: */
150: public boolean componentHasLifecycle() {
151: if (delegate instanceof Behavior) {
152: return ((Behavior<?>) delegate).componentHasLifecycle();
153: }
154: return false;
155: }
156:
157: // ~~~~~~~~ LifecycleStrategy ~~~~~~~~
158:
159: /**
160: * Invokes delegate start method if the delegate is a LifecycleStrategy
161: * {@inheritDoc}
162: */
163: public void start(Object component) {
164: if (delegate instanceof LifecycleStrategy) {
165: ((LifecycleStrategy) delegate).start(component);
166: }
167: }
168:
169: /**
170: * Invokes delegate stop method if the delegate is a LifecycleStrategy
171: * {@inheritDoc}
172: */
173: public void stop(Object component) {
174: if (delegate instanceof LifecycleStrategy) {
175: ((LifecycleStrategy) delegate).stop(component);
176: }
177: }
178:
179: /**
180: * Invokes delegate dispose method if the delegate is a LifecycleStrategy
181: * {@inheritDoc}
182: */
183: public void dispose(Object component) {
184: if (delegate instanceof LifecycleStrategy) {
185: ((LifecycleStrategy) delegate).dispose(component);
186: }
187: }
188:
189: /**
190: * Invokes delegate hasLifecycle(Class) method if the delegate is a LifecycleStrategy
191: * {@inheritDoc}
192: */
193: public boolean hasLifecycle(Class<?> type) {
194: return delegate instanceof LifecycleStrategy
195: && ((LifecycleStrategy) delegate).hasLifecycle(type);
196: }
197:
198: public String toString() {
199: return getDescriptor() + ":" + delegate.toString();
200: }
201: }
|