Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The following page has been changed by MichaelKretinin:
http://wiki.apache.org/tapestry/Tapestry5_How_To_Integrate_Components_With_Spring_Transactions

New page:
#pragma section-numbers on
==== Make transactionInterceptor accessible from Spring IOC context. ====

{{{
<bean id="transactionManager" 
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>

    <bean 
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
    <bean 
class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"
          p:transactionInterceptor-ref="transactionInterceptor"/>

    <bean id="transactionInterceptor"
          
class="org.springframework.transaction.interceptor.TransactionInterceptor"
          p:transactionManager-ref="transactionManager"
          p:transactionAttributeSource-ref="transactionAttributeSource"/>

    <bean id="transactionAttributeSource"
          
class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource">
        <constructor-arg value="false"/>
    </bean>
}}}

==== Contribute transactional worker to components transformation chain. ====

{{{#!java
    public static void 
contributeComponentClassTransformWorker(OrderedConfiguration<ComponentClassTransformWorker>
 configuration,
            TransactionInterceptor transactionInterceptor) {
        configuration.add("Transactional", new 
TransactionalWorker(transactionInterceptor));
    }
}}}

==== Create TransactionalWorker. ====

{{{#!java
public class TransactionalWorker implements ComponentClassTransformWorker {
    private final TransactionInterceptor transactionInterceptor;
    private final ComponentMethodAdvice advice = new ComponentMethodAdvice() {
        public void advise(final ComponentMethodInvocation invocation) {
            try {
                transactionInterceptor.invoke(new 
MethodInvocationAdapter(invocation));
                if (invocation.isFail()) {
                    throw new 
RuntimeException(invocation.getThrown(Throwable.class));
                }
            }
            catch (RuntimeException ex) {
                throw ex;
            } catch (Throwable ex) {
                throw new RuntimeException(ex);
            }
        }
    };

    public TransactionalWorker(TransactionInterceptor transactionInterceptor) {
        this.transactionInterceptor = transactionInterceptor;
    }

    public void transform(ClassTransformation transformation, 
MutableComponentModel model) {
        for (TransformMethodSignature sig : 
transformation.findMethodsWithAnnotation(Transactional.class)) {
            transformation.advise(sig, advice);
        }
    }
}
}}}

==== Implement MethodInvocation adapter. ====

{{{#!java
import org.aopalliance.intercept.MethodInvocation;
import org.apache.tapestry.services.ComponentMethodInvocation;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

class MethodInvocationAdapter implements MethodInvocation {
    private final ComponentMethodInvocation invocation;

    public MethodInvocationAdapter(ComponentMethodInvocation invocation) {
        this.invocation = invocation;
    }

    public Object getThis() {
        return invocation.getComponentResources().getComponent();
    }

    public Method getMethod() {
        Method method = BeanUtils.findDeclaredMethod(getThisClass(), 
invocation.getMethodName(), getParameterTypes());
        if (method != null) {
            return method;
        }
        throw new RuntimeException(invocation.getMethodName() + " not found");
    }

    private Class getThisClass() {
        Class clazz = getThis().getClass();
        try {
            return Class.forName(clazz.getName()); // ClassLoader issue
        } catch (ClassNotFoundException e) {
            return clazz;
        }
    }

    private Class[] getParameterTypes() {
        int parameterCount = invocation.getParameterCount();
        Class[] classes = new Class[parameterCount];
        for (int i = 0; i < parameterCount; i++) {
            classes[i] = invocation.getParameterType(i);
        }
        return classes;
    }

    public Object proceed() throws Throwable {
        invocation.proceed();
        return invocation.getResult();
    }

    public Object[] getArguments() {
        int parameterCount = invocation.getParameterCount();
        Object[] args = new Object[parameterCount];
        for (int i = 0; i < parameterCount; i++) {
            args[i] = invocation.getParameter(i);
        }
        return args;
    }

    public AccessibleObject getStaticPart() {
        throw new UnsupportedOperationException();
    }
}
}}}

==== Annotate component method and run. ====

{{{#!java
 @Transactional(readOnly = true)
    void setupRender() {
…
    }
}}}
  
  
 ----
written by Michael Kretinin

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to