java - CDI: how to apply interceptor to library (third-party) code? -
i'm working in servlet container (tomcat) cdi (weld) , jpa (hibernate). i've found many examples online of creating "transactional" interceptor:
import java.lang.annotation.documented; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; import javax.interceptor.interceptorbinding; @target({ elementtype.method, elementtype.type }) @retention(retentionpolicy.runtime) @documented @interceptorbinding public @interface transactional {}
@transactional @interceptor public class transactionalinterceptor { @inject entitymanager em; @aroundinvoke public object wrapintransaction(invocationcontext invocation) throws exception { boolean transactionowner = !istransactioninprogress(); if (transactionowner) { em.gettransaction().begin(); } try { return invocation.proceed(); } catch (runtimeexception ex) { em.gettransaction().setrollbackonly(); throw ex; } { if (transactionowner) { if (em.gettransaction().getrollbackonly()) { em.gettransaction().rollback(); } else { em.gettransaction().commit(); } } } } private boolean istransactioninprogress() { return em.gettransaction().isactive(); } }
and works great on local code. however, i'd able apply transactional annotation (interceptor) code i'm not writing (i.e. library code i'm using). in case, hoping apply cdi interceptor struts2 interceptor ensure during entire handling of request, i'd have transaction open.
how apply transactional interceptor library code in way?
edit i've done via spring xml:
<!-- transactional demarcation --> <aop:config> <aop:pointcut id="transactionalpointcut" expression="execution(* utils.struts2.interceptor.transactioninterceptor.intercept(..))"/> <aop:advisor pointcut-ref="transactionalpointcut" advice-ref="transactionaladvice"/> </aop:config> ...
but i'm looking cdi alternative.
this becomes difficult cdi 1.0. you'd have crack open jar , add beans.xml in meta-inf, repackage jar , in war (i assume it's war) create portable extension add metadata interceptor annotation class. you'd need observe beforebeandiscovery
, add new annotatedtype
. deltaspike can part.
Comments
Post a Comment