1 package org.nanocontainer.nanning; 2 3 import org.codehaus.nanning.config.AspectSystem; 4 import org.codehaus.nanning.config.Aspect; 5 import org.codehaus.nanning.AspectInstance; 6 import org.codehaus.nanning.Mixin; 7 import org.picocontainer.extras.DecoratingComponentAdapterFactory; 8 import org.picocontainer.extras.DecoratingComponentAdapter; 9 import org.picocontainer.defaults.*; 10 import org.picocontainer.*; 11 12 import java.io.Serializable ; 13 import java.util.Arrays ; 14 import java.util.List ; 15 import java.util.Collections ; 16 import java.util.ArrayList ; 17 18 22 public class NanningComponentAdapterFactory extends DecoratingComponentAdapterFactory implements Serializable { 23 private AspectSystem aspectSystem; 24 25 public NanningComponentAdapterFactory(AspectSystem aspectSystem, 26 ComponentAdapterFactory delegate) { 27 super(delegate); 28 this.aspectSystem = aspectSystem; 29 } 30 31 public NanningComponentAdapterFactory() { 32 this(new AspectSystem(), new DefaultComponentAdapterFactory()); 33 } 34 35 public ComponentAdapter createComponentAdapter(Object componentKey, 36 Class componentImplementation, 37 Parameter[] parameters) 38 throws PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException { 39 ComponentAdapter componentAdapter = super.createComponentAdapter(componentKey, componentImplementation, parameters); 40 41 if (Aspect.class.isAssignableFrom(componentImplementation)) { 42 componentAdapter = new AspectAdapter(componentAdapter, aspectSystem); 43 44 } else if (canBeWeaved(componentImplementation)) { 45 Class componentInterface = getComponentInterface(componentImplementation); 46 componentAdapter = new WeavingAdapter(componentAdapter, aspectSystem, componentInterface); 47 } 48 49 return componentAdapter; 50 } 51 52 private Class getComponentInterface(Class componentImplementation) { 53 return (Class ) getAllInterfaces(componentImplementation).get(0); 54 } 55 56 List getAllInterfaces(Class componentImplementation) { 57 if (componentImplementation == null) { 58 return Collections.EMPTY_LIST; 59 } 60 List result = new ArrayList (Arrays.asList(componentImplementation.getInterfaces())); 61 result.addAll(getAllInterfaces(componentImplementation.getSuperclass())); 62 return result; 63 } 64 65 private boolean canBeWeaved(Class componentImplementation) { 66 return getAllInterfaces(componentImplementation).size() == 1; 67 } 68 69 public static class WeavingAdapter extends DecoratingComponentAdapter { 70 71 private final AspectSystem aspectSystem; 72 private Class componentInterface; 73 74 public WeavingAdapter(ComponentAdapter delegate, AspectSystem aspectSystem, Class componentInterface) { 75 super(delegate); 76 this.aspectSystem = aspectSystem; 77 this.componentInterface = componentInterface; 78 } 79 80 public Object getComponentInstance(MutablePicoContainer picoContainer) throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException { 81 Object component = super.getComponentInstance(picoContainer); 82 84 AspectInstance aspectInstance = new AspectInstance(componentInterface); 86 Mixin mixin = new Mixin(componentInterface, component); 87 aspectInstance.addMixin(mixin); 88 89 getAspectSystem().initialize(aspectInstance); 91 component = aspectInstance.getProxy(); 92 93 return component; 94 } 95 96 private AspectSystem getAspectSystem() { 97 return aspectSystem; 98 } 99 } 100 101 public static class AspectAdapter extends DecoratingComponentAdapter { 102 private AspectSystem aspectSystem; 103 104 public AspectAdapter(ComponentAdapter delegate, AspectSystem aspectSystem) { 105 super(delegate); 106 this.aspectSystem = aspectSystem; 107 } 108 109 public Object getComponentInstance(MutablePicoContainer picoContainer) 110 throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException { 111 Aspect aspect = (Aspect) super.getComponentInstance(picoContainer); 112 getAspectSystem().addAspect(aspect); 113 return aspect; 114 } 115 116 private AspectSystem getAspectSystem() { 117 return aspectSystem; 118 } 119 } 120 } 121 | Popular Tags |