| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | package org.topazproject.ambra.struts2; |
|---|
| 22 | |
|---|
| 23 | import com.opensymphony.xwork2.interceptor.AbstractInterceptor; |
|---|
| 24 | import com.opensymphony.xwork2.ActionInvocation; |
|---|
| 25 | import com.opensymphony.xwork2.Action; |
|---|
| 26 | import com.opensymphony.xwork2.ActionProxy; |
|---|
| 27 | |
|---|
| 28 | import org.springframework.transaction.annotation.Transactional; |
|---|
| 29 | import org.springframework.transaction.PlatformTransactionManager; |
|---|
| 30 | import org.springframework.transaction.TransactionStatus; |
|---|
| 31 | import org.springframework.transaction.support.TransactionTemplate; |
|---|
| 32 | import org.springframework.transaction.support.TransactionCallback; |
|---|
| 33 | import org.springframework.beans.factory.annotation.Required; |
|---|
| 34 | |
|---|
| 35 | import org.slf4j.Logger; |
|---|
| 36 | import org.slf4j.LoggerFactory; |
|---|
| 37 | |
|---|
| 38 | import java.lang.reflect.Method; |
|---|
| 39 | import java.lang.annotation.Annotation; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | public class TransactionInterceptor extends AbstractInterceptor { |
|---|
| 50 | private static final Logger log = LoggerFactory.getLogger(TransactionInterceptor.class); |
|---|
| 51 | |
|---|
| 52 | private PlatformTransactionManager txManager; |
|---|
| 53 | |
|---|
| 54 | public String intercept(final ActionInvocation actionInvocation) throws Exception { |
|---|
| 55 | |
|---|
| 56 | Action action = (Action) actionInvocation.getAction(); |
|---|
| 57 | ActionProxy actionProxy = actionInvocation.getProxy(); |
|---|
| 58 | String methodName = actionProxy.getMethod(); |
|---|
| 59 | |
|---|
| 60 | Span span = getAnnotation(action.getClass(), methodName, Span.class); |
|---|
| 61 | if (span == null) { |
|---|
| 62 | return actionInvocation.invoke(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | if (log.isDebugEnabled()) |
|---|
| 66 | log.debug("Interceped " + action.getClass().getName() + "." + methodName + "(...)"); |
|---|
| 67 | |
|---|
| 68 | final Transactional transactional = span.value(); |
|---|
| 69 | |
|---|
| 70 | TransactionTemplate txTemplate = new TransactionTemplate(txManager); |
|---|
| 71 | txTemplate.setReadOnly(transactional.readOnly()); |
|---|
| 72 | txTemplate.setTimeout(transactional.timeout()); |
|---|
| 73 | txTemplate.setIsolationLevel(transactional.isolation().value()); |
|---|
| 74 | txTemplate.setPropagationBehavior(transactional.propagation().value()); |
|---|
| 75 | |
|---|
| 76 | CallbackResult callbackResult = (CallbackResult) txTemplate.execute(new TransactionCallback() { |
|---|
| 77 | public CallbackResult doInTransaction(TransactionStatus transactionStatus) { |
|---|
| 78 | CallbackResult result = new CallbackResult(); |
|---|
| 79 | try { |
|---|
| 80 | result.setResult(actionInvocation.invoke()); |
|---|
| 81 | } catch (Exception e) { |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | boolean noRollback = false; |
|---|
| 87 | |
|---|
| 88 | if (transactional.noRollbackFor() != null) { |
|---|
| 89 | for (Class<? extends Throwable> exception : transactional.noRollbackFor()) { |
|---|
| 90 | if (exception.isInstance(e)) { |
|---|
| 91 | noRollback = true; |
|---|
| 92 | break; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | if (!noRollback && transactional.rollbackFor() != null) { |
|---|
| 98 | for (Class<? extends Throwable> exception : transactional.rollbackFor()) { |
|---|
| 99 | if (exception.isInstance(e)) { |
|---|
| 100 | transactionStatus.setRollbackOnly(); |
|---|
| 101 | break; |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | result.setException(e); |
|---|
| 106 | } |
|---|
| 107 | return result; |
|---|
| 108 | } |
|---|
| 109 | }); |
|---|
| 110 | |
|---|
| 111 | if (callbackResult.getException() != null) |
|---|
| 112 | throw callbackResult.getException(); |
|---|
| 113 | |
|---|
| 114 | return callbackResult.getResult(); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | private <A extends Annotation> A getAnnotation(Class<? extends Action> actionClass, |
|---|
| 118 | String methodName, Class<A> annotationType) throws Exception { |
|---|
| 119 | A annotation = actionClass.getAnnotation(annotationType); |
|---|
| 120 | if (annotation == null) { |
|---|
| 121 | annotation = getMethodAnnotation(actionClass, methodName, annotationType); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | return annotation; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | private <A extends Annotation> A getMethodAnnotation(Class<? extends Action> actionClass, |
|---|
| 128 | String methodName, Class<A> annotationType) { |
|---|
| 129 | try { |
|---|
| 130 | Method method = actionClass.getDeclaredMethod(methodName); |
|---|
| 131 | A annotation = method.getAnnotation(annotationType); |
|---|
| 132 | if (annotation == null) { |
|---|
| 133 | Class parent = actionClass.getSuperclass(); |
|---|
| 134 | if (Action.class.isAssignableFrom(parent)) { |
|---|
| 135 | annotation = getMethodAnnotation((Class<? extends Action>) parent, |
|---|
| 136 | methodName, annotationType); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | return annotation; |
|---|
| 140 | } catch (NoSuchMethodException e) { |
|---|
| 141 | return null; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | @Required |
|---|
| 151 | public void setTxManager(PlatformTransactionManager txManager) { |
|---|
| 152 | this.txManager = txManager; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | private static class CallbackResult { |
|---|
| 159 | |
|---|
| 160 | private String result; |
|---|
| 161 | private Exception exception; |
|---|
| 162 | |
|---|
| 163 | public String getResult() { |
|---|
| 164 | return result; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | public void setResult(String result) { |
|---|
| 168 | this.result = result; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | public Exception getException() { |
|---|
| 172 | return exception; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | public void setException(Exception exception) { |
|---|
| 176 | this.exception = exception; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | } |
|---|