KickJava   Java API By Example, From Geeks To Geeks.

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


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.MixinFactory;
13 import dynaop.Proxy;
14 import org.picocontainer.ComponentAdapter;
15 import org.picocontainer.PicoContainer;
16 import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
17
18 import java.util.Properties JavaDoc;
19
20 /**
21  * Manufactures mixins from a <code>PicoContainer</code>. Useful when a mixin
22  * has dependencies on other components in the <code>PicoContainer</code>.
23  *
24  * @author Stephen Molitor
25  * @version $Revision: 3144 $
26  */

27 class ContainerSuppliedMixinFactory implements MixinFactory {
28
29     private final PicoContainer pico;
30     private final Class JavaDoc mixinClass;
31
32     /**
33      * Creates a new <code>ContainerSuppliedMixinFactory</code> that will
34      * manufacture mixins by retrieving them from the <code>PicoContainer</code>
35      * using a given component key.
36      *
37      * @param pico the <code>PicoContainer</code> to retrieve the mixin from.
38      * @param mixinComponentKey the component key that will be used to retrieve
39      * the mixin object from the pico container.
40      */

41     ContainerSuppliedMixinFactory(PicoContainer pico, Class JavaDoc mixinClass) {
42         this.pico = pico;
43         this.mixinClass = mixinClass;
44     }
45
46     /**
47      * Manufactures a <code>Mixin</code> by retrieving it from the
48      * <code>PicoContainer</code>.
49      *
50      * @param proxy the proxy that the interceptor will wrap.
51      * @return the <code>Mixin</code> object.
52      * @throws NullPointerException if the mixin can not be found in the pico
53      * container.
54      */

55     public Object JavaDoc create(Proxy proxy) throws NullPointerException JavaDoc {
56         Object JavaDoc mixin = pico.getComponentInstanceOfType(mixinClass);
57         if (mixin == null) {
58             ComponentAdapter adapter = new ConstructorInjectionComponentAdapter(mixinClass, mixinClass);
59             mixin = adapter.getComponentInstance(pico);
60         }
61         return mixin;
62     }
63
64     /**
65      * Gets properties. Useful for debugging.
66      *
67      * @return an empty <code>Properties</code> object.
68      */

69     public Properties JavaDoc getProperties() {
70         Properties JavaDoc properties = new Properties JavaDoc();
71         properties.setProperty("advice", "mixin");
72         return properties;
73     }
74
75 }
Popular Tags