KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > aop > dynaop > ContainerSuppliedInterceptorFactory


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  * Idea by Rachel Davies, Original code by various *
9  *****************************************************************************/

10 package org.nanocontainer.aop.dynaop;
11
12 import dynaop.Interceptor;
13 import dynaop.InterceptorFactory;
14 import dynaop.Proxy;
15 import org.aopalliance.intercept.MethodInterceptor;
16 import org.picocontainer.PicoContainer;
17
18 import java.util.Properties JavaDoc;
19
20 /**
21  * Manufactures interceptors from a <code>PicoContainer</code>. Useful when
22  * an interceptor has dependencies on other components in the
23  * <code>PicoContainer</code>.
24  *
25  * @author Stephen Molitor
26  * @version $Revision: 3144 $
27  */

28 class ContainerSuppliedInterceptorFactory implements InterceptorFactory {
29
30     private final PicoContainer pico;
31     private final Object JavaDoc interceptorComponentKey;
32
33     /**
34      * Creates a new <code>ContainerSuppliedInterceptorFactory</code> that
35      * will manufacture interceptors by retrieving them from the
36      * <code>PicoContainer</code> using a given component key.
37      *
38      * @param pico the <code>PicoContainer</code> to retrieve the interceptor
39      * from.
40      * @param interceptorComponentKey the component key that will be used to
41      * retrieve the interceptor from the pico container.
42      */

43     ContainerSuppliedInterceptorFactory(PicoContainer pico, Object JavaDoc interceptorComponentKey) {
44         this.pico = pico;
45         this.interceptorComponentKey = interceptorComponentKey;
46     }
47
48     /**
49      * Manufactures an <code>Interceptor</code> by retrieving it from the
50      * <code>PicoContainer</code>.
51      *
52      * @param proxy not used.
53      * @return the <code>Interceptor</code> object.
54      * @throws NullPointerException if the interceptor can not be found in the
55      * pico container.
56      */

57     public Interceptor create(Proxy proxy) throws NullPointerException JavaDoc {
58         MethodInterceptor methodInterceptor = (MethodInterceptor) pico.getComponentInstance(interceptorComponentKey);
59         if (methodInterceptor == null) {
60             throw new NullPointerException JavaDoc("Interceptor with component key " + interceptorComponentKey
61                     + " + not found in PicoContainer");
62         }
63         return new MethodInterceptorAdapter(methodInterceptor);
64     }
65
66     /**
67      * Gets properties. Useful for debugging.
68      *
69      * @return a <code>Properties</code> object.
70      */

71     public Properties JavaDoc getProperties() {
72         Properties JavaDoc properties = new Properties JavaDoc();
73         properties.setProperty("advice", "method interceptor");
74         properties.setProperty("scope", "per-instance");
75         return properties;
76     }
77
78 }
Popular Tags