1 package com.atlassian.plugins.rest.common.transaction;
2
3 import com.atlassian.plugins.rest.common.interceptor.MethodInvocation;
4 import com.atlassian.plugins.rest.common.interceptor.ResourceInterceptor;
5 import com.atlassian.sal.api.transaction.TransactionCallback;
6 import com.atlassian.sal.api.transaction.TransactionTemplate;
7
8 import java.lang.reflect.InvocationTargetException;
9
10
11
12
13
14
15 public class TransactionInterceptor implements ResourceInterceptor
16 {
17 private final TransactionTemplate transactionTemplate;
18
19 public TransactionInterceptor(TransactionTemplate transactionTemplate)
20 {
21 this.transactionTemplate = transactionTemplate;
22 }
23
24 public void intercept(final MethodInvocation invocation) throws IllegalAccessException, InvocationTargetException
25 {
26 try
27 {
28 transactionTemplate.execute(new TransactionCallback()
29 {
30 public Object doInTransaction()
31 {
32 try
33 {
34 invocation.invoke();
35 }
36 catch (IllegalAccessException e)
37 {
38 throw new TransactionException(e);
39 }
40 catch (InvocationTargetException e)
41 {
42 throw new TransactionException(e);
43 }
44 return null;
45 }
46 });
47 }
48 catch (TransactionException ex)
49 {
50 Throwable t = ex.getCause();
51 if (t instanceof IllegalAccessException)
52 {
53 throw (IllegalAccessException)t;
54 }
55 else if (t instanceof InvocationTargetException)
56 {
57 throw (InvocationTargetException)t;
58 }
59 else
60 {
61 throw new RuntimeException("This should not be possible");
62 }
63 }
64 }
65
66 private static final class TransactionException extends RuntimeException
67 {
68 private TransactionException(Throwable throwable)
69 {
70 super(throwable);
71 }
72 }
73 }