KickJava   Java API By Example, From Geeks To Geeks.

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


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.Aspects;
13 import dynaop.Interceptor;
14 import dynaop.Invocation;
15 import dynaop.Pointcuts;
16 import dynaop.ProxyFactory;
17 import org.picocontainer.PicoContainer;
18
19 /**
20  * Creates dynamically generated <code>PicoContainer</code> proxy objects that
21  * delegate to a <code>PicoContainer</code> supplied by a
22  * <code>ContainerLoader</code>.
23  *
24  * @author Stephen Molitor
25  * @version $Revision: 3144 $
26  */

27 class PicoContainerProxy implements Interceptor {
28
29     private final ContainerLoader containerLoader;
30
31     /**
32      * Creates a <code>PicoContainer</code> proxy that delegates to a
33      * <code>PicoContainer</code> provided by <code>containerLoader</code>.
34      *
35      * @param containerLoader the container loader.
36      * @return the dynamically generated proxy.
37      */

38     static PicoContainer create(ContainerLoader containerLoader) {
39         Aspects aspects = new Aspects();
40         aspects.interceptor(Pointcuts.ALL_CLASSES, Pointcuts.ALL_METHODS, new PicoContainerProxy(containerLoader));
41         aspects.interfaces(Pointcuts.ALL_CLASSES, new Class JavaDoc[]{PicoContainer.class});
42         return (PicoContainer) ProxyFactory.getInstance(aspects).wrap(new Object JavaDoc());
43     }
44
45     public Object JavaDoc intercept(Invocation invocation) throws Throwable JavaDoc {
46         return invocation.getMethod().invoke(containerLoader.getContainer(), invocation.getArguments());
47     }
48
49     private PicoContainerProxy(ContainerLoader containerLoader) {
50         this.containerLoader = containerLoader;
51     }
52
53 }
Popular Tags