CLOUDSTACK-1562: Replace @DB support to be the formal implementation instead of a temporary hacking one
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/07838395 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/07838395 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/07838395 Branch: refs/heads/4.1 Commit: 0783839530f18be0bac88857916f11298b5cf7e0 Parents: feee6bd Author: Kelven Yang <[email protected]> Authored: Wed Mar 6 16:43:48 2013 -0800 Committer: Chip Childers <[email protected]> Committed: Thu Mar 7 19:13:10 2013 -0500 ---------------------------------------------------------------------- .../cloud/utils/component/ComponentContext.java | 30 +++++--------- .../cloud/utils/db/TransactionContextBuilder.java | 20 ++++++++-- 2 files changed, 27 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/07838395/utils/src/com/cloud/utils/component/ComponentContext.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/ComponentContext.java b/utils/src/com/cloud/utils/component/ComponentContext.java index 82fe90d..ca7ad5c 100644 --- a/utils/src/com/cloud/utils/component/ComponentContext.java +++ b/utils/src/com/cloud/utils/component/ComponentContext.java @@ -28,10 +28,7 @@ import javax.management.NotCompliantMBeanException; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; -import org.springframework.aop.Advisor; import org.springframework.aop.framework.Advised; -import org.springframework.aop.framework.ProxyFactory; -import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.ApplicationContext; @@ -39,7 +36,6 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; -import com.cloud.utils.db.TransactionContextBuilder; import com.cloud.utils.mgmt.JmxUtil; import com.cloud.utils.mgmt.ManagementBean; @@ -55,15 +51,6 @@ public class ComponentContext implements ApplicationContextAware { private static ApplicationContext s_appContext; - private static Advisor s_advisor; - private static ProxyFactory s_pf; - static { - s_advisor = new DefaultPointcutAdvisor(new MatchAnyMethodPointcut(), - new TransactionContextBuilder()); - s_pf = new ProxyFactory(); - s_pf.addAdvisor(s_advisor); - } - @Override public void setApplicationContext(ApplicationContext applicationContext) { s_logger.info("Setup Spring Application context"); @@ -216,8 +203,17 @@ public class ComponentContext implements ApplicationContextAware { } public static <T> T inject(Class<T> clz) { - T instance = s_appContext.getAutowireCapableBeanFactory().createBean(clz); - return instance; + T instance; + try { + instance = clz.newInstance(); + return inject(instance); + } catch (InstantiationException e) { + s_logger.error("Unhandled InstantiationException", e); + throw new RuntimeException("Unable to instantiate object of class " + clz.getName() + ", make sure it has public constructor"); + } catch (IllegalAccessException e) { + s_logger.error("Unhandled IllegalAccessException", e); + throw new RuntimeException("Unable to instantiate object of class " + clz.getName() + ", make sure it has public constructor"); + } } public static <T> T inject(Object instance) { @@ -225,9 +221,5 @@ public class ComponentContext implements ApplicationContextAware { AutowireCapableBeanFactory beanFactory = s_appContext.getAutowireCapableBeanFactory(); beanFactory.autowireBean(instance); return (T)instance; -/* - s_pf.setTarget(instance); - return (T)s_pf.getProxy(); -*/ } } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/07838395/utils/src/com/cloud/utils/db/TransactionContextBuilder.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/db/TransactionContextBuilder.java b/utils/src/com/cloud/utils/db/TransactionContextBuilder.java index 744e75c..49c4184 100644 --- a/utils/src/com/cloud/utils/db/TransactionContextBuilder.java +++ b/utils/src/com/cloud/utils/db/TransactionContextBuilder.java @@ -29,8 +29,8 @@ public class TransactionContextBuilder implements MethodInterceptor { public Object AroundAnyMethod(ProceedingJoinPoint call) throws Throwable { MethodSignature methodSignature = (MethodSignature)call.getSignature(); - Method targetMethod = methodSignature.getMethod(); - if(true) { // TODO ??? needToIntercept(targetMethod)) { + Method targetMethod = methodSignature.getMethod(); + if(needToIntercept(targetMethod, call.getTarget())) { Transaction txn = Transaction.open(call.getSignature().getName()); Object ret = null; try { @@ -47,7 +47,7 @@ public class TransactionContextBuilder implements MethodInterceptor { public Object invoke(MethodInvocation method) throws Throwable { Method targetMethod = method.getMethod(); - if(needToIntercept(targetMethod)) { + if(needToIntercept(targetMethod, method.getThis())) { Transaction txn = Transaction.open(targetMethod.getName()); Object ret = null; try { @@ -60,13 +60,25 @@ public class TransactionContextBuilder implements MethodInterceptor { return method.proceed(); } - private boolean needToIntercept(Method method) { + private boolean needToIntercept(Method method, Object target) { DB db = method.getAnnotation(DB.class); if (db != null) { return true; } Class<?> clazz = method.getDeclaringClass(); + if(clazz.isInterface()) { + clazz = target.getClass(); + for(Method m : clazz.getMethods()) { + // it is supposed that we need to check against type arguments, + // this can be simplified by just checking method name + if(m.getName().equals(method.getName())) { + if(m.getAnnotation(DB.class) != null) + return true; + } + } + } + do { db = clazz.getAnnotation(DB.class); if (db != null) {
