KickJava   Java API By Example, From Geeks To Geeks.

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


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 Joerg Schaible *
9  *****************************************************************************/

10 package org.picocontainer.defaults;
11
12 import java.io.Serializable JavaDoc;
13 import java.lang.reflect.InvocationHandler JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.lang.reflect.Proxy JavaDoc;
17
18 import org.picocontainer.Disposable;
19 import org.picocontainer.PicoContainer;
20 import org.picocontainer.Startable;
21
22
23 /**
24  * A factory for immutable PicoContainer proxies.
25  *
26  * @author Jörg Schaible
27  * @since 1.2
28  */

29 public class ImmutablePicoContainerProxyFactory implements InvocationHandler JavaDoc, Serializable JavaDoc {
30
31     private static final Class JavaDoc[] interfaces = new Class JavaDoc[]{PicoContainer.class};
32     protected static Method JavaDoc startMethod = null;
33     protected static Method JavaDoc stopMethod = null;
34     protected static Method JavaDoc disposeMethod = null;
35     protected static Method JavaDoc equalsMethod = null;
36
37     static {
38         try {
39             startMethod = Startable.class.getMethod("start", new Class JavaDoc[0]);
40             stopMethod = Startable.class.getMethod("stop", new Class JavaDoc[0]);
41             disposeMethod = Disposable.class.getMethod("dispose", new Class JavaDoc[0]);
42             equalsMethod = Object JavaDoc.class.getMethod("equals", new Class JavaDoc[]{Object JavaDoc.class});
43         } catch (final NoSuchMethodException JavaDoc e) {
44             throw new InternalError JavaDoc(e.getMessage());
45         }
46     }
47
48     private final PicoContainer pico;
49
50     /**
51      * Construct a ImmutablePicoContainerProxyFactory.
52      *
53      * @param pico the container to hide
54      * @throws NullPointerException if <tt>pico</tt> is <code>null</code>
55      * @since 1.2
56      */

57     protected ImmutablePicoContainerProxyFactory(final PicoContainer pico) {
58         if (pico == null) {
59             throw new NullPointerException JavaDoc();
60         }
61         this.pico = pico;
62     }
63
64     public Object JavaDoc invoke(final Object JavaDoc proxy, final Method JavaDoc method, final Object JavaDoc[] args) throws Throwable JavaDoc {
65         if (method.equals(startMethod) || method.equals(stopMethod) || method.equals(disposeMethod)) {
66             throw new UnsupportedOperationException JavaDoc("This container is immutable, "
67                     + method.getName()
68                     + " is not allowed");
69         } else if (method.equals(equalsMethod)) { // necessary for JDK 1.3
70
return new Boolean JavaDoc(args[0] != null && args[0].equals(pico));
71         }
72         try {
73             return method.invoke(pico, args);
74         } catch (final InvocationTargetException JavaDoc e) {
75             throw e.getTargetException();
76         }
77     }
78
79     /**
80      * Create a new immutable PicoContainer proxy. The proxy will completly hide the implementation of the given
81      * {@link PicoContainer} and will also prevent the invocation of any methods of the lifecycle methods from
82      * {@link Startable} or {@link Disposable}.
83      *
84      * @param pico
85      * @return the new proxy
86      * @throws NullPointerException if <tt>pico</tt> is <code>null</code>
87      * @since 1.2
88      */

89     public static PicoContainer newProxyInstance(final PicoContainer pico) {
90         return (PicoContainer)Proxy.newProxyInstance(
91                 PicoContainer.class.getClassLoader(), interfaces,
92                 new ImmutablePicoContainerProxyFactory(pico));
93     }
94 }
95
Popular Tags