KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > ImplementationHidingComponentAdapter


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.defaults;
11
12 import java.lang.reflect.InvocationHandler JavaDoc;
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.lang.reflect.Proxy JavaDoc;
16
17 import org.picocontainer.ComponentAdapter;
18 import org.picocontainer.ComponentMonitor;
19 import org.picocontainer.PicoContainer;
20 import org.picocontainer.PicoInitializationException;
21 import org.picocontainer.PicoIntrospectionException;
22
23 /**
24  * This component adapter makes it possible to hide the implementation
25  * of a real subject (behind a proxy) provided the key is an interface.
26  * <p/>
27  * This class exists here, because a) it has no deps on external jars, b) dynamic proxy is quite easy.
28  * The user is prompted to look at picocontainer-gems for alternate and bigger implementations.
29  *
30  * @author Aslak Helles&oslash;y
31  * @author Paul Hammant
32  * @see org.picocontainer.gems.HotSwappingComponentAdapter for a more feature-rich version of this class.
33  * @since 1.2, moved from package {@link org.picocontainer.alternatives}
34  */

35 public class ImplementationHidingComponentAdapter extends DecoratingComponentAdapter {
36     private final boolean strict;
37
38     /**
39      * Creates an ImplementationHidingComponentAdapter with a delegate
40      * @param delegate the component adapter to which this adapter delegates
41      * @param strict the scrict mode boolean
42      */

43     public ImplementationHidingComponentAdapter(ComponentAdapter delegate, boolean strict) {
44         super(delegate);
45         this.strict = strict;
46     }
47
48     public Object JavaDoc getComponentInstance(final PicoContainer container)
49             throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
50
51         Object JavaDoc componentKey = getDelegate().getComponentKey();
52         Class JavaDoc[] classes = null;
53         if (componentKey instanceof Class JavaDoc && ((Class JavaDoc) getDelegate().getComponentKey()).isInterface()) {
54             classes = new Class JavaDoc[]{(Class JavaDoc) getDelegate().getComponentKey()};
55         } else if (componentKey instanceof Class JavaDoc[]) {
56             classes = (Class JavaDoc[]) componentKey;
57         } else {
58             if(strict) {
59                 throw new PicoIntrospectionException("In strict mode, " + getClass().getName() + " only allows components registered with interface keys (java.lang.Class or java.lang.Class[])");
60             }
61             return getDelegate().getComponentInstance(container);
62         }
63
64         Class JavaDoc[] interfaces = verifyInterfacesOnly(classes);
65         return createProxy(interfaces, container, getDelegate().getComponentImplementation().getClassLoader());
66     }
67
68     private Object JavaDoc createProxy(Class JavaDoc[] interfaces, final PicoContainer container, final ClassLoader JavaDoc classLoader) {
69         return Proxy.newProxyInstance(classLoader,
70                 interfaces, new InvocationHandler JavaDoc() {
71                     public Object JavaDoc invoke(final Object JavaDoc proxy, final Method JavaDoc method,
72                                          final Object JavaDoc[] args)
73                             throws Throwable JavaDoc {
74                         Object JavaDoc componentInstance = getDelegate().getComponentInstance(container);
75                         ComponentMonitor componentMonitor = currentMonitor();
76                         try {
77                             componentMonitor.invoking(method, componentInstance);
78                             long startTime = System.currentTimeMillis();
79                             Object JavaDoc object = method.invoke(componentInstance, args);
80                             componentMonitor.invoked(method, componentInstance, System.currentTimeMillis() - startTime);
81                             return object;
82                         } catch (final InvocationTargetException JavaDoc ite) {
83                             componentMonitor.invocationFailed(method, componentInstance, ite);
84                             throw ite.getTargetException();
85                         }
86                     }
87                 });
88     }
89
90     private Class JavaDoc[] verifyInterfacesOnly(Class JavaDoc[] classes) {
91         for (int i = 0; i < classes.length; i++) {
92             if(!classes[i].isInterface()) {
93                 throw new PicoIntrospectionException("Class keys must be interfaces. " + classes[i] + " is not an interface.");
94             }
95         }
96         return classes;
97     }
98
99 }
100
Popular Tags