KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcspring > AopProxyFactoryProtocol


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tcspring;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.springframework.aop.framework.AdvisedSupport;
9 import org.springframework.aop.framework.ProxyFactory;
10 import org.springframework.aop.framework.ProxyFactoryBean;
11 import org.springframework.beans.factory.BeanFactory;
12
13 import com.tc.aspectwerkz.joinpoint.StaticJoinPoint;
14
15 /**
16  * Intercepts the Spring AOP proxy creation and returns a FastProxy instead - falls back to regular creation upon failure
17  * or if some requirements are not fulfilled.
18  *
19  * @author Jonas Bonér
20  */

21 public class AopProxyFactoryProtocol {
22   private final transient Log logger = LogFactory.getLog(getClass());
23   /**
24    * Around advice that is intercepting the pointcut:
25    * <tt>
26    * execution(* org.springframework.aop.framework.AopProxyFactory+.createAopProxy(..)) && args(proxyFactory)
27    * </tt>
28    */

29   public Object JavaDoc createAopProxy(StaticJoinPoint jp, AdvisedSupport proxyFactory) throws Throwable JavaDoc {
30     ApplicationHelper applicationHelper = new ApplicationHelper(proxyFactory.getClass());
31     if (!applicationHelper.isDSOApplication() || !applicationHelper.isFastProxyEnabled()) { return jp.proceed(); }
32
33     try {
34       if (proxyFactory instanceof ProxyFactoryBean) {
35         return new FastAopProxy((ProxyFactoryBean) proxyFactory);
36       } else if (proxyFactory instanceof ProxyFactory && proxyFactory.isFrozen()) {
37         return jp.proceed(); // TODO implement support for ProxyFactory later if needed
38
} else {
39         return jp.proceed();
40       }
41     } catch (Throwable JavaDoc e) {
42       logger.warn("Falling back to using regular Spring AOP proxy creation, due to: " + e);
43       // if something goes wrong fall back on using cglib or dynamic proxy
44
// f.e. mixins are not supported so -> exception -> fallback to cglib
45
return jp.proceed();
46     }
47   }
48   
49   /**
50    * Advises PCD: before(execution(void saveBeanFactory(..)) && args(beanFactory) && this(bean))
51    */

52   public void saveBeanFactory(BeanFactoryAware bean, BeanFactory beanFactory) throws Throwable JavaDoc {
53       bean.tc$setBeanFactory(beanFactory);
54   }
55
56   /**
57    * Mixin to hold a publicly accessible reference to BeanFactory that created the bean
58    */

59   public static class BeanFactoryAwareMixin implements BeanFactoryAware {
60     private transient BeanFactory beanFactory;
61     
62     public void tc$setBeanFactory(BeanFactory beanFactory) {
63       this.beanFactory = beanFactory;
64     }
65
66     public BeanFactory tc$getBeanFactory() {
67       return beanFactory;
68     }
69   }
70   
71 }
72
73
Popular Tags