1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.spring.transaction;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.lang.reflect.Method;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.springframework.transaction.TransactionException;
26  import org.springframework.transaction.TransactionStatus;
27  import org.springframework.transaction.TransactionSystemException;
28  
29  /**
30   * <a href="TransactionStatusClp.java.html"><b><i>View Source</i></b></a>
31   *
32   * <p>
33   * A class loader proxy implementation for a transaction status object created
34   * by the transaction manager within the portal and serialized back and forth
35   * between the portal and plugin class loader.
36   * </p>
37   *
38   * @author Micha Kiener
39   * @author Brian Wing Shun Chan
40   */
41  public class TransactionStatusClp implements TransactionStatus {
42  
43      public TransactionStatusClp(Object remoteTransactionStatus) {
44          _remoteTransactionStatus = remoteTransactionStatus;
45  
46          if (_remoteMethods == null) {
47              initRemoteMethods(remoteTransactionStatus);
48          }
49      }
50  
51      public Object createSavepoint() throws TransactionException {
52          try {
53              Method method = _remoteMethods.get("createSavepoint");
54  
55              return method.invoke(_remoteTransactionStatus);
56          }
57          catch (Exception e) {
58              _log.error(e, e);
59  
60              throw new TransactionSystemException(e.getMessage());
61          }
62      }
63  
64      public void flush() {
65          try {
66              Method method = _remoteMethods.get("flush");
67  
68              method.invoke(_remoteTransactionStatus);
69          }
70          catch (Exception e) {
71              _log.error(e, e);
72  
73              throw new TransactionSystemException(e.getMessage());
74          }
75      }
76  
77      public Object getRemoteTransactionStatus() {
78          return _remoteTransactionStatus;
79      }
80  
81      public boolean hasSavepoint() {
82          try {
83              Method method = _remoteMethods.get("hasSavepoint");
84  
85              return (Boolean)method.invoke(_remoteTransactionStatus);
86          }
87          catch (Exception e) {
88              _log.error(e, e);
89  
90              throw new RuntimeException(e.getMessage());
91          }
92      }
93  
94      public boolean isCompleted() {
95          try {
96              Method method = _remoteMethods.get("isCompleted");
97  
98              return (Boolean)method.invoke(_remoteTransactionStatus);
99          }
100         catch (Exception e) {
101             _log.error(e, e);
102 
103             throw new RuntimeException(e.getMessage());
104         }
105     }
106 
107     public boolean isNewTransaction() {
108         try {
109             Method method = _remoteMethods.get("isNewTransaction");
110 
111             return (Boolean)method.invoke(_remoteTransactionStatus);
112         }
113         catch (Exception e) {
114             _log.error(e, e);
115 
116             throw new RuntimeException(e.getMessage());
117         }
118     }
119 
120     public boolean isRollbackOnly() {
121         try {
122             Method method = _remoteMethods.get("isRollbackOnly");
123 
124             return (Boolean)method.invoke(_remoteTransactionStatus);
125         }
126         catch (Exception e) {
127             _log.error(e, e);
128 
129             throw new RuntimeException(e.getMessage());
130         }
131     }
132 
133     public void releaseSavepoint(Object savepoint) throws TransactionException {
134         try {
135             Method method = _remoteMethods.get("releaseSavepoint");
136 
137             method.invoke(_remoteTransactionStatus);
138         }
139         catch (Exception e) {
140             _log.error(e, e);
141 
142             throw new TransactionSystemException(e.getMessage());
143         }
144     }
145 
146     public void rollbackToSavepoint(Object savepoint)
147         throws TransactionException {
148 
149         try {
150             Method method = _remoteMethods.get("rollbackToSavepoint");
151 
152             method.invoke(_remoteTransactionStatus);
153         }
154         catch (Exception e) {
155             _log.error(e, e);
156 
157             throw new TransactionSystemException(e.getMessage());
158         }
159     }
160 
161     public void setRollbackOnly() {
162         try {
163             Method method = _remoteMethods.get("setRollbackOnly");
164 
165             method.invoke(_remoteTransactionStatus);
166         }
167         catch (Exception e) {
168             _log.error(e, e);
169 
170             throw new RuntimeException(e.getMessage());
171         }
172     }
173 
174     protected void initRemoteMethods(Object remoteTransactionStatus) {
175         _remoteMethods = new HashMap<String, Method>();
176 
177         Method[] methods = TransactionStatus.class.getMethods();
178 
179         for (Method method : methods) {
180             _remoteMethods.put(method.getName(), method);
181         }
182     }
183 
184     private static Log _log = LogFactoryUtil.getLog(TransactionStatusClp.class);
185 
186     private static Map<String, Method> _remoteMethods;
187 
188     private Object _remoteTransactionStatus;
189 
190 }