Cleaned up Spring interceptor and helpers

This commit is contained in:
Jonas Bonér 2010-01-02 15:28:55 +01:00
parent f30741d053
commit 21001a7e10
15 changed files with 301 additions and 441 deletions

View file

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="akkaService" class="se.scalablesolutions.akka.service.MyService">
</bean>
<bean id="interceptor" class="se.scalablesolutions.akka.interceptor.AkkaSpringInterceptor">
</bean>
<bean id="interceptedService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="akkaService"/>
</property>
<property name="interceptorNames">
<list>
<value>interceptor</value>
</list>
</property>
</bean>
</beans>

View file

@ -1,84 +0,0 @@
package se.scalablesolutions.akka.interceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import se.scalablesolutions.akka.actor.ActiveObjectAspect;
import se.scalablesolutions.akka.actor.AspectInit;
import se.scalablesolutions.akka.actor.AspectInitRegistry;
import se.scalablesolutions.akka.actor.Dispatcher;
import se.scalablesolutions.akka.util.AkkaSpringJoinPointWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.PlatformTransactionManager;
public class AkkaSpringInterceptor extends ActiveObjectAspect implements MethodInterceptor,BeanPostProcessor{
private final static String CLASSNAME = "org.springframework.transaction.support.TransactionSynchronizationManager";
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
//Determine transactional status
boolean transactional = getTransactionStatus();
Dispatcher dispatcher = new Dispatcher(transactional);
dispatcher.start();
AspectInitRegistry.register(methodInvocation.getThis(), new AspectInit(
methodInvocation.getThis().getClass(),
dispatcher,
1000));
AkkaSpringJoinPointWrapper asjp = AkkaSpringJoinPointWrapper.createSpringAkkaAspectWerkzWrapper(methodInvocation);
System.out.println("AkkaSpringInterceptor = " + Thread.currentThread());
Object obj = this.invoke(asjp);
dispatcher.stop();
return obj;
}
@Override
public Object postProcessAfterInitialization(Object bean, String arg1)
throws BeansException {
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String arg1)
throws BeansException {
return bean;
}
/*
* Checks if intercepted Spring bean is in a transaction
*
*/
private boolean getTransactionStatus() {
String status = null;
Boolean hasTransaction = null;
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
Class tsmClass = contextClassLoader.loadClass(CLASSNAME);
hasTransaction = (Boolean) tsmClass.getMethod("isActualTransactionActive", null).invoke(null, null);
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return hasTransaction;
}
}

View file

@ -1,19 +0,0 @@
package se.scalablesolutions.akka.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import se.scalablesolutions.akka.service.MyService;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("AkkaAppConfig.xml");
MyService myService = (MyService)context.getBean("interceptedService");
System.out.println(Thread.currentThread());
myService.getNumbers(777,"vfsh");
}
}

View file

@ -1,23 +0,0 @@
package se.scalablesolutions.akka.service;
import org.springframework.transaction.annotation.Transactional;
//import se.scalablesolutions.akka.annotation.oneway;
public class MyService {
public Integer getNumbers(int aTestNumber, String aText) {
System.out.println("MyService : " + Thread.currentThread());
return new Integer(aTestNumber);
}
//@oneway
public void calculate() {
for (int i = 1; i < 10000; i++) {
System.out.println("i=" + i);
}
}
}

View file

@ -0,0 +1,79 @@
/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka.spring;
import se.scalablesolutions.akka.actor.ActiveObjectAspect;
import se.scalablesolutions.akka.actor.AspectInit;
import se.scalablesolutions.akka.actor.AspectInitRegistry;
import se.scalablesolutions.akka.actor.Dispatcher;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import java.lang.reflect.Method;
public class AkkaSpringInterceptor extends ActiveObjectAspect implements MethodInterceptor, BeanPostProcessor {
static final String TRANSACTION_MANAGER_CLASS_NAME = "org.springframework.transaction.support.TransactionSynchronizationManager";
static final String IS_TRANSACTION_ALIVE_METHOD_NAME = "isActualTransactionActive";
static private Method IS_TRANSACTION_ALIVE_METHOD = null;
static {
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Class clazz = null;
if (contextClassLoader != null) {
clazz = contextClassLoader.loadClass(TRANSACTION_MANAGER_CLASS_NAME);
} else {
ClassLoader springClassLoader = AkkaSpringInterceptor.class.getClassLoader();
clazz = springClassLoader.loadClass(TRANSACTION_MANAGER_CLASS_NAME);
}
if (clazz != null) IS_TRANSACTION_ALIVE_METHOD = clazz.getDeclaredMethod(IS_TRANSACTION_ALIVE_METHOD_NAME, null);
} catch (Exception e) {
}
}
// FIXME make configurable
static final int TIME_OUT = 1000;
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Dispatcher dispatcher = new Dispatcher(isTransactional());
dispatcher.start();
AspectInitRegistry.register(methodInvocation.getThis(), new AspectInit(
methodInvocation.getThis().getClass(),
dispatcher,
TIME_OUT));
Object result = this.invoke(AkkaSpringJoinPointWrapper.createSpringAkkaAspectWerkzWrapper(methodInvocation));
dispatcher.stop();
return result;
}
@Override
public Object postProcessAfterInitialization(Object bean, String arg) throws BeansException {
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String arg) throws BeansException {
return bean;
}
/**
* Checks if intercepted Spring bean is in a transaction.
*/
private boolean isTransactional() {
try {
return (Boolean) IS_TRANSACTION_ALIVE_METHOD.invoke(null, null);
} catch (Exception e) {
throw new RuntimeException("Could not check if the Spring bean is executing within a transaction", e);
}
}
}

View file

@ -0,0 +1,89 @@
/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka.spring;
import org.aopalliance.intercept.MethodInvocation;
import org.codehaus.aspectwerkz.joinpoint.*;
import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
public class AkkaSpringJoinPointWrapper implements JoinPoint {
private MethodInvocation methodInvocation = null;
public static AkkaSpringJoinPointWrapper createSpringAkkaAspectWerkzWrapper(MethodInvocation methodInvocation) {
AkkaSpringJoinPointWrapper joinPointWrapper = new AkkaSpringJoinPointWrapper();
joinPointWrapper.setMethodInvocation(methodInvocation);
return joinPointWrapper;
}
public MethodInvocation getMethodInvocation() {
return methodInvocation;
}
public void setMethodInvocation(MethodInvocation methodInvocation) {
this.methodInvocation = methodInvocation;
}
public Object proceed() throws Throwable {
return methodInvocation.proceed();
}
public Rtti getRtti() {
return new AkkaSpringRttiWrapper(methodInvocation);
}
public Object getTarget() {
return methodInvocation.getThis();
}
public Object getThis() {
return methodInvocation.getThis();
}
public Object getCallee() {
throw new UnsupportedOperationException();
}
public Object getCaller() {
throw new UnsupportedOperationException();
}
public void addMetaData(Object arg0, Object arg1) {
throw new UnsupportedOperationException();
}
public Class getCalleeClass() {
throw new UnsupportedOperationException();
}
public Class getCallerClass() {
throw new UnsupportedOperationException();
}
public EnclosingStaticJoinPoint getEnclosingStaticJoinPoint() {
throw new UnsupportedOperationException();
}
public Object getMetaData(Object arg0) {
throw new UnsupportedOperationException();
}
public Signature getSignature() {
throw new UnsupportedOperationException();
}
public Class getTargetClass() {
throw new UnsupportedOperationException();
}
public JoinPointType getType() {
throw new UnsupportedOperationException();
}
public StaticJoinPoint copy() {
throw new UnsupportedOperationException();
}
}

View file

@ -0,0 +1,73 @@
/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka.spring;
import org.aopalliance.intercept.MethodInvocation;
import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
import org.codehaus.aspectwerkz.joinpoint.Rtti;
import java.lang.reflect.Method;
public class AkkaSpringRttiWrapper implements MethodRtti {
private MethodInvocation methodInvocation = null;
public AkkaSpringRttiWrapper(MethodInvocation methodInvocation) {
this.methodInvocation = methodInvocation;
}
public Method getMethod() {
return methodInvocation.getMethod();
}
public Class getReturnType() {
throw new UnsupportedOperationException();
}
public Object getReturnValue() {
throw new UnsupportedOperationException();
}
public Class[] getExceptionTypes() {
throw new UnsupportedOperationException();
}
public Class[] getParameterTypes() {
throw new UnsupportedOperationException();
}
public Object[] getParameterValues() {
throw new UnsupportedOperationException();
}
public void setParameterValues(Object[] arg0) {
throw new UnsupportedOperationException();
}
public Rtti cloneFor(Object arg0, Object arg1) {
throw new UnsupportedOperationException();
}
public Class getDeclaringType() {
throw new UnsupportedOperationException();
}
public int getModifiers() {
throw new UnsupportedOperationException();
}
public String getName() {
throw new UnsupportedOperationException();
}
public Object getTarget() {
throw new UnsupportedOperationException();
}
public Object getThis() {
throw new UnsupportedOperationException();
}
}

View file

@ -1,130 +0,0 @@
package se.scalablesolutions.akka.util;
import org.aopalliance.intercept.MethodInvocation;
import org.codehaus.aspectwerkz.joinpoint.*;
import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
public class AkkaSpringJoinPointWrapper implements JoinPoint{
private MethodInvocation methodInvocation = null;
public MethodInvocation getMethodInvocation() {
return methodInvocation;
}
public void setMethodInvocation(MethodInvocation methodInvocation) {
this.methodInvocation = methodInvocation;
}
public static AkkaSpringJoinPointWrapper createSpringAkkaAspectWerkzWrapper(MethodInvocation methodInvocation){
AkkaSpringJoinPointWrapper asjp = new AkkaSpringJoinPointWrapper();
asjp.setMethodInvocation(methodInvocation);
return asjp;
}
@Override
public Object getCallee() {
System.out.println("public Object getCallee()");
// TODO Auto-generated method stub
return null;
}
@Override
public Object getCaller() {
System.out.println("public Object getCaller() ");
// TODO Auto-generated method stub
return null;
}
@Override
public Rtti getRtti() {
System.out.println("public Rtti getRtti()");
AkkaSpringRttiWrapper asrw = new AkkaSpringRttiWrapper(methodInvocation);
return asrw;
}
@Override
public Object getTarget() {
System.out.println("public Object getTarget() ");
return methodInvocation.getThis();
}
@Override
public Object getThis() {
System.out.println("public Object getThis()");
return methodInvocation.getThis();
}
@Override
public void addMetaData(Object arg0, Object arg1) {
System.out.println("public void addMetaData(Object arg0, Object arg1)");
// TODO Auto-generated method stub
}
@Override
public Class getCalleeClass() {
System.out.println("public Class getCalleeClass()");
// TODO Auto-generated method stub
return null;
}
@Override
public Class getCallerClass() {
System.out.println("public Class getCallerClass() ");
// TODO Auto-generated method stub
return null;
}
@Override
public EnclosingStaticJoinPoint getEnclosingStaticJoinPoint() {
System.out.println("public EnclosingStaticJoinPoint getEnclosingStaticJoinPoint()");
// TODO Auto-generated method stub
return null;
}
@Override
public Object getMetaData(Object arg0) {
System.out.println("public Object getMetaData(Object arg0) ");
// TODO Auto-generated method stub
return null;
}
@Override
public Signature getSignature() {
System.out.println("public Signature getSignature() ");
// TODO Auto-generated method stub
return null;
}
@Override
public Class getTargetClass() {
System.out.println("public Class getTargetClass() ");
// TODO Auto-generated method stub
return null;
}
@Override
public JoinPointType getType() {
System.out.println("public JoinPointType getType()");
// TODO Auto-generated method stub
return null;
}
@Override
public Object proceed() throws Throwable {
System.out.println("public Object proceed()");
return methodInvocation.proceed();
}
public StaticJoinPoint copy() {
// TODO Auto-generated method stub
return null;
}
}

View file

@ -1,95 +0,0 @@
package se.scalablesolutions.akka.util;
import org.aopalliance.intercept.MethodInvocation;
import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
import org.codehaus.aspectwerkz.joinpoint.Rtti;
import java.lang.reflect.Method;
public class AkkaSpringRttiWrapper implements MethodRtti {
private MethodInvocation methodInvocation = null;
public AkkaSpringRttiWrapper(MethodInvocation methodInvocation){
this.methodInvocation = methodInvocation;
}
@Override
public Method getMethod() {
return methodInvocation.getMethod();
}
@Override
public Class getReturnType() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getReturnValue() {
// TODO Auto-generated method stub
return null;
}
@Override
public Class[] getExceptionTypes() {
// TODO Auto-generated method stub
return null;
}
@Override
public Class[] getParameterTypes() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] getParameterValues() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setParameterValues(Object[] arg0) {
// TODO Auto-generated method stub
}
@Override
public Rtti cloneFor(Object arg0, Object arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public Class getDeclaringType() {
// TODO Auto-generated method stub
return null;
}
@Override
public int getModifiers() {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getTarget() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getThis() {
// TODO Auto-generated method stub
return null;
}
}

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

View file

@ -1,49 +0,0 @@
package se.scalablesolutions.akka;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import se.scalablesolutions.akka.service.MyService;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
public void testApp()
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring-test-config.xml");
MyService myService = (MyService)context.getBean("interceptedService");
System.out.println(Thread.currentThread());
Object obj = myService.getNumbers(12,"vfsh");
assertEquals(new Integer(12), obj);
}
}

View file

@ -0,0 +1,26 @@
package se.scalablesolutions.akka.spring;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AkkaSpringInterceptorTest extends TestCase {
public AkkaSpringInterceptorTest(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(AkkaSpringInterceptorTest.class);
}
public void testInvokingAkkaEnabledSpringBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-test-config.xml");
MyService myService = (MyService) context.getBean("actorBeanService");
Object obj = myService.getNumbers(12, "vfsh");
assertEquals(new Integer(12), obj);
}
}

View file

@ -0,0 +1,20 @@
package se.scalablesolutions.akka.spring;
import org.springframework.transaction.annotation.Transactional;
//import se.scalablesolutions.akka.annotation.oneway;
public class MyService {
public Integer getNumbers(int aTestNumber, String aText) {
return new Integer(aTestNumber);
}
//@oneway
public void calculate() {
for (int i = 1; i < 10000; i++) {
System.out.println("i=" + i);
}
}
}

View file

@ -1,22 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="akkaService" class="se.scalablesolutions.akka.service.MyService">
<bean id="akkaInterceptor" class="se.scalablesolutions.akka.spring.AkkaSpringInterceptor">
</bean>
<bean id="interceptor" class="se.scalablesolutions.akka.interceptor.AkkaSpringInterceptor">
<bean id="service" class="se.scalablesolutions.akka.spring.MyService">
</bean>
<bean id="interceptedService" class="org.springframework.aop.framework.ProxyFactoryBean">
<bean id="actorBeanService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="akkaService"/>
<ref bean="service"/>
</property>
<property name="interceptorNames">
<list>
<value>interceptor</value>
<value>akkaInterceptor</value>
</list>
</property>
</bean>