KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > HotSwappingComponentAdapter


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by *
9  *****************************************************************************/

10 package org.picocontainer.gems;
11
12 import com.thoughtworks.proxy.ProxyFactory;
13 import com.thoughtworks.proxy.factory.StandardProxyFactory;
14 import com.thoughtworks.proxy.toys.delegate.ObjectReference;
15 import com.thoughtworks.proxy.toys.hotswap.HotSwapping;
16 import com.thoughtworks.proxy.toys.multicast.ClassHierarchyIntrospector;
17 import org.picocontainer.ComponentAdapter;
18 import org.picocontainer.PicoContainer;
19 import org.picocontainer.defaults.DecoratingComponentAdapter;
20
21 /**
22  * This component adapter makes it possible to hide the implementation
23  * of a real subject (behind a proxy).
24  * If the key of the component is of type {@link Class} and that class represents an interface, the proxy
25  * will only implement the interface represented by that Class. Otherwise (if the key is
26  * something else), the proxy will implement all the interfaces of the underlying subject.
27  * In any case, the proxy will also implement
28  * {@link com.thoughtworks.proxy.toys.hotswap.Swappable}, making it possible to swap out the underlying
29  * subject at runtime.
30  * <p/>
31  * <em>
32  * Note that this class doesn't cache instances. If you want caching,
33  * use a {@link org.picocontainer.defaults.CachingComponentAdapter} around this one.
34  * </em>
35  *
36  * @author Paul Hammant
37  * @author Aslak Helles&oslash;y
38  * @version $Revision: 2002 $
39  */

40 public class HotSwappingComponentAdapter extends DecoratingComponentAdapter {
41     private final ProxyFactory proxyFactory;
42
43     private static class ImplementationHidingReference implements ObjectReference {
44         private final ComponentAdapter delegate;
45         private Object JavaDoc value;
46         private final PicoContainer container;
47
48         public ImplementationHidingReference(ComponentAdapter delegate, PicoContainer container) {
49             this.delegate = delegate;
50             this.container = container;
51         }
52
53         public Object JavaDoc get() {
54             if (value == null) {
55                 value = delegate.getComponentInstance(container);
56             }
57             return value;
58         }
59
60         public void set(Object JavaDoc item) {
61             value = item;
62         }
63     }
64
65     public HotSwappingComponentAdapter(final ComponentAdapter delegate, ProxyFactory proxyFactory) {
66         super(delegate);
67         this.proxyFactory = proxyFactory;
68     }
69
70     public HotSwappingComponentAdapter(ComponentAdapter delegate) {
71         this(delegate, new StandardProxyFactory());
72     }
73
74     public Object JavaDoc getComponentInstance(final PicoContainer container) {
75         Class JavaDoc[] proxyTypes;
76         if (getComponentKey() instanceof Class JavaDoc && proxyFactory.canProxy((Class JavaDoc) getComponentKey())) {
77             proxyTypes = new Class JavaDoc[]{(Class JavaDoc) getComponentKey()};
78         } else {
79             proxyTypes = ClassHierarchyIntrospector.addIfClassProxyingSupportedAndNotObject(getComponentImplementation(), getComponentImplementation().getInterfaces(), proxyFactory);
80         }
81         ObjectReference reference = new ImplementationHidingReference(getDelegate(), container);
82         return HotSwapping.object(proxyTypes, proxyFactory, reference, true);
83     }
84 }
85
Popular Tags