KickJava   Java API By Example, From Geeks To Geeks.

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

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