KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > proxy > container > GeneratedAOPProxyFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.aop.proxy.container;
23
24 import java.util.Arrays JavaDoc;
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 //import org.jboss.repository.spi.MetaDataContext;
31

32 /**
33  *
34  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
35  * @version $Revision: 57019 $
36  */

37 public class GeneratedAOPProxyFactory implements AOPProxyFactory
38 {
39    public Object JavaDoc 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    //FIXME - make metaDataContext a MetaDataContext once MC 2.0 is released
53
private Object JavaDoc createAdvisedProxy(boolean objectAsSuper, Class JavaDoc proxiedClass, Class JavaDoc[] interfaces,
54          AOPProxyFactoryMixin[] mixins, SimpleMetaData simpleMetaData, Object JavaDoc target, /*MetaDataContext*/Object JavaDoc 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 JavaDoc("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 JavaDoc.class;
75       }
76       
77       return getProxy(objectAsSuper, manager, proxiedClass, interfaces, mixins, simpleMetaData, target, metaDataContext, containerCache);
78    }
79
80    //FIXME - make metaDataContext a MetaDataContext once MC 2.0 is released
81
private Object JavaDoc getProxy(boolean objectAsSuper, AspectManager manager, Class JavaDoc proxiedClass,
82          Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData simpleMetaData, Object JavaDoc target, /*MetaDataContext*/ Object JavaDoc metaDataContext, ContainerCache containerCache)
83    {
84       try
85       {
86          Class JavaDoc 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 JavaDoc e)
116       {
117          throw new RuntimeException JavaDoc(e);
118       }
119    }
120    
121    private Class JavaDoc generateProxy(boolean objectAsSuper, ContainerCache cache) throws Exception JavaDoc
122    {
123       Class JavaDoc proxyClass = ContainerProxyFactory.getProxyClass(objectAsSuper, cache.getKey(), cache.getAdvisor());
124       
125       return proxyClass;
126    }
127    
128    private Object JavaDoc instantiateAndConfigureProxy(Class JavaDoc proxyClass, ContainerCache cache, SimpleMetaData metadata, Object JavaDoc target) throws Exception JavaDoc
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 JavaDoc());
150       }
151    
152       return proxy;
153    }
154 }
155
Popular Tags