KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > nanning > NanningComponentAdapter


1 /*****************************************************************************
2  * Copyright (C) NanoContainer 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 Jon Tirsen *
9  *****************************************************************************/

10
11 package org.nanocontainer.nanning;
12
13 import org.codehaus.nanning.AspectInstance;
14 import org.codehaus.nanning.Mixin;
15 import org.codehaus.nanning.config.AspectSystem;
16 import org.picocontainer.extras.DecoratingComponentAdapter;
17 import org.picocontainer.ComponentAdapter;
18 import org.picocontainer.defaults.AssignabilityRegistrationException;
19 import org.picocontainer.defaults.NotConcreteRegistrationException;
20 import org.picocontainer.PicoInitializationException;
21 import org.picocontainer.PicoIntrospectionException;
22 import org.picocontainer.MutablePicoContainer;
23
24 /**
25  * @author Jon Tirsen (tirsen@codehaus.org)
26  * @author Aslak Hellesoy
27  * @version $Revision: 1.1 $
28  */

29 public class NanningComponentAdapter extends DecoratingComponentAdapter {
30
31     private final AspectSystem aspectSystem;
32
33     public NanningComponentAdapter(AspectSystem aspectSystem, ComponentAdapter decoratedComponentAdapter) {
34         super(decoratedComponentAdapter);
35         this.aspectSystem = aspectSystem;
36     }
37
38     public Object JavaDoc getComponentInstance(MutablePicoContainer picoContainer) throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException {
39         Object JavaDoc component = super.getComponentInstance(picoContainer);
40         // TODO Nanning will at the moment only aspectify stuff when it has one and only one interface
41
if (component.getClass().getInterfaces().length == 1) {
42             Class JavaDoc intf = component.getClass().getInterfaces()[0];
43             // the trick: set up first mixin manually with the component as target
44
AspectInstance aspectInstance = new AspectInstance(intf);
45             Mixin mixin = new Mixin(intf, component);
46             aspectInstance.addMixin(mixin);
47
48             // let the aspects do its work
49
aspectSystem.initialize(aspectInstance);
50             component = aspectInstance.getProxy();
51         }
52
53         return component;
54     }
55 }
56
Popular Tags