1 22 package org.jboss.aop.proxy.container; 23 24 import java.util.Arrays ; 25 26 import org.jboss.aop.Advised; 27 import org.jboss.aop.AspectManager; 28 import org.jboss.aop.instrument.Untransformable; 29 import org.jboss.aop.metadata.SimpleMetaData; 30 32 37 public class GeneratedAOPProxyFactory implements AOPProxyFactory 38 { 39 public Object createAdvisedProxy(AOPProxyFactoryParameters params) 40 { 41 return createAdvisedProxy( 42 params.isObjectAsSuperClass(), 43 params.getProxiedClass(), 44 params.getInterfaces(), 45 params.getMixins(), 46 params.getSimpleMetaData(), 47 params.getTarget(), 48 params.getMetaDataContext(), 49 params.getContainerCache()); 50 } 51 52 private Object createAdvisedProxy(boolean objectAsSuper, Class proxiedClass, Class [] interfaces, 54 AOPProxyFactoryMixin[] mixins, SimpleMetaData simpleMetaData, Object target, Object metaDataContext, ContainerCache containerCache) 55 { 56 AspectManager manager = AspectManager.instance(); 57 58 if (target != null) 59 { 60 if (proxiedClass != null) 61 { 62 if (proxiedClass.isAssignableFrom(target.getClass()) == false) 63 { 64 throw new RuntimeException ("Specified class type " + proxiedClass.getName() + " and target " + target.getClass().getName() + " are not compatible"); 65 } 66 } 67 else 68 { 69 proxiedClass = target.getClass(); 70 } 71 } 72 else if (proxiedClass == null) 73 { 74 proxiedClass = Object .class; 75 } 76 77 return getProxy(objectAsSuper, manager, proxiedClass, interfaces, mixins, simpleMetaData, target, metaDataContext, containerCache); 78 } 79 80 private Object getProxy(boolean objectAsSuper, AspectManager manager, Class proxiedClass, 82 Class [] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData simpleMetaData, Object target, Object metaDataContext, ContainerCache containerCache) 83 { 84 try 85 { 86 Class proxyClass = null; 87 88 boolean isAdvised = Advised.class.isAssignableFrom(proxiedClass); 89 90 if (target instanceof Untransformable || (isAdvised && interfaces == null && mixins == null && metaDataContext == null && simpleMetaData == null)) 91 { 92 return target; 93 } 94 95 96 synchronized (ContainerCache.mapLock) 97 { 98 if (containerCache == null) 99 { 100 containerCache = ContainerCache.initialise(manager, proxiedClass, interfaces, mixins, metaDataContext, simpleMetaData); 101 } 102 103 if (!containerCache.hasAspects() && !containerCache.requiresInstanceAdvisor()) 104 { 105 return target; 106 } 107 else 108 { 109 proxyClass = generateProxy(objectAsSuper, containerCache); 110 } 111 } 112 113 return instantiateAndConfigureProxy(proxyClass, containerCache, simpleMetaData, target); 114 } 115 catch (Exception e) 116 { 117 throw new RuntimeException (e); 118 } 119 } 120 121 private Class generateProxy(boolean objectAsSuper, ContainerCache cache) throws Exception 122 { 123 Class proxyClass = ContainerProxyFactory.getProxyClass(objectAsSuper, cache.getKey(), cache.getAdvisor()); 124 125 return proxyClass; 126 } 127 128 private Object instantiateAndConfigureProxy(Class proxyClass, ContainerCache cache, SimpleMetaData metadata, Object target) throws Exception 129 { 130 AspectManaged proxy = (AspectManaged)proxyClass.newInstance(); 131 proxy.setAdvisor(cache.getClassAdvisor()); 132 133 if (cache.getInstanceContainer() != null) 134 { 135 proxy.setInstanceAdvisor(cache.getInstanceContainer()); 136 } 137 138 if (metadata != null) 139 { 140 proxy.setMetadata(metadata); 141 } 142 143 if (target != null) 144 { 145 ((Delegate)proxy).setDelegate(target); 146 } 147 else 148 { 149 ((Delegate)proxy).setDelegate(new Object ()); 150 } 151 152 return proxy; 153 } 154 } 155 | Popular Tags |