Rev 1782 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | /* |
2 | BERSERK - a BusinEss Runtime and SEcurity Resources Kit |
||
3 | Copyright (C) 2003 Goncalo Luiz |
||
4 | |||
5 | This library is free software; you can redistribute it and/or |
||
6 | modify it under the terms of the GNU Lesser General Public |
||
7 | License as published by the Free Software Foundation; either |
||
8 | version 2.1 of the License, or (at your option) any later version. |
||
9 | |||
10 | This library is distributed in the hope that it will be useful, |
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
13 | Lesser General Public License for more details. |
||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public |
||
16 | License along with this library; if not, write to the Free Software |
||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
18 | |||
19 | To contact Goncalo Luiz use the following e-mail address (use an @ instead of the _): |
||
20 | gedl_mega.ist.utl.pt |
||
21 | */ |
||
22 | package pt.utl.ist.berserk.logic.serviceManager; |
||
23 | /** |
||
24 | * @author Jorge Martins |
||
25 | * @version revision by Goncalo Luiz (gedl \AT/ rnl \DOT/ ist \DOT/ utl \DOT/ pt) at June the 15th, 2004 |
||
26 | **/ |
||
1782 | jmachado | 27 | |
28 | |||
1 | fvelez | 29 | import pt.utl.ist.berserk.BerserkConfiguration; |
30 | import pt.utl.ist.berserk.ServiceRequest; |
||
31 | import pt.utl.ist.berserk.ServiceResponse; |
||
32 | import pt.utl.ist.berserk.logic.filterManager.FilteringResult; |
||
33 | import pt.utl.ist.berserk.logic.filterManager.IFilterBroker; |
||
34 | import pt.utl.ist.berserk.logic.serviceManager.exceptions.ExecutedFilterException; |
||
35 | import pt.utl.ist.berserk.logic.serviceManager.exceptions.FilterChainFailedException; |
||
36 | import pt.utl.ist.berserk.logic.serviceManager.exceptions.ServiceManagerException; |
||
37 | import pt.utl.ist.berserk.storage.ITransactionBroker; |
||
38 | import pt.utl.ist.berserk.storage.exceptions.StorageException; |
||
1312 | jmachado | 39 | |
40 | import java.util.ArrayList; |
||
41 | |||
1 | fvelez | 42 | public class TransactionalServiceInvoker extends ServiceInvoker |
43 | { |
||
1781 | jmachado | 44 | |
1782 | jmachado | 45 | public static boolean debug = false; |
1781 | jmachado | 46 | |
47 | //TODO HIPOTESE DE TER AQUI UMA VERIFICACAO DE TRANSACAO CONTIDA |
||
48 | //PROBLEMA E QUE TEMOS DE USAR A THREAD SE POR ALGUMA RAZAO MUDAMOS DE TRANSACTION BROKER |
||
49 | //TUDO ESTRAGADO |
||
50 | //NAO DA ISTO É UM SINGLETON boolean alreadyActive = false; |
||
51 | //TIVEMOS DE TER COMO VAR LOCAL UMA VEZ QUE O InVOCADOR E NUCLEAR |
||
52 | |||
1 | fvelez | 53 | public final static TransactionalServiceInvoker invocador = new TransactionalServiceInvoker(); |
1781 | jmachado | 54 | |
1782 | jmachado | 55 | |
1781 | jmachado | 56 | public final 5+0%2Fdocs%2Fapi+Object">Object invoke( |
1 | fvelez | 57 | 5+0%2Fdocs%2Fapi+Object">Object requester, |
58 | IService service, |
||
59 | 1.5.0/docs/api/java/lang/String.html">String methodName, |
||
60 | 5+0%2Fdocs%2Fapi+Object">Object arguments[], |
||
61 | 1.5.0/docs/api/java/lang/String.html">String names[], |
||
62 | IFilterBroker broker) |
||
63 | throws ServiceManagerException, 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
||
64 | { |
||
65 | boolean passedPre = false; |
||
66 | boolean passedPost = false; |
||
67 | ServiceRequest request = new ServiceRequest(requester, arguments,names,service,methodName); |
||
68 | ServiceResponse response = new ServiceResponse(); |
||
69 | FilteringResult filteringResult = null; |
||
70 | ITransactionBroker tb = BerserkConfiguration.getApplicationTransactionBroker(); |
||
1781 | jmachado | 71 | boolean alreadyActive = tb.isTransactionActive(); |
1782 | jmachado | 72 | |
1 | fvelez | 73 | try |
74 | { |
||
1781 | jmachado | 75 | |
76 | if(debug) |
||
77 | 1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId() + "Starting transaction for service: " + service.getClass().getName() + " method:" + methodName); |
||
78 | if(!alreadyActive) |
||
79 | { |
||
80 | tb.beginTransaction(); |
||
81 | } |
||
82 | else |
||
83 | { |
||
84 | 1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId() + "AVISO : ESTA A CHAMAR UM SERVICO TRANSACIONAL DENTRO DE UMA TRANSACAO, VAMOS MANTER A MESMA TRASACAO"); |
||
85 | 1.5.0/docs/api/java/lang/System.html">System.out.println(">>>>>>TRANSACTION WARNING BUT NO PROBLEM: AVISO: Já existia uma transação activa e foi iniciada outra neste momento sem fechar a anterior "); |
||
1823 | jmachado | 86 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Servico chamado: " + service.getClass().getName()); |
87 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Metodo chamado: " + methodName); |
||
88 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Requester: " + requester); |
||
89 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Arguments: " + arguments); |
||
90 | 1.5.0/docs/api/java/lang/System.html">System.out.println("ArgNames: " + names); |
||
91 | |||
92 | |||
93 | |||
1781 | jmachado | 94 | for (1.5.0/docs/api/java/lang/StackTraceElement.html">StackTraceElement ste : 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getStackTrace()) { |
95 | 1.5.0/docs/api/java/lang/System.html">System.out.println("\t" + ste); |
||
96 | } |
||
97 | |||
98 | } |
||
1 | fvelez | 99 | try |
100 | { |
||
101 | filteringResult = broker.executePreFiltering(request, response); |
||
102 | passedPre = filteringResult.isSuccess(); |
||
103 | } |
||
104 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
105 | { |
||
106 | throw new ExecutedFilterException( |
||
107 | "a not 'FilterException' exception occured while executing pre filter chain", |
||
108 | e); |
||
109 | } |
||
110 | if (passedPre) |
||
111 | { |
||
112 | response.setReturnObject(doInvocation(request.getService(), methodName, request.getArguments())); |
||
113 | } |
||
114 | else |
||
115 | { |
||
116 | throw new FilterChainFailedException( |
||
117 | "failed to execute postFiltering on service" + service.getClass(),filteringResult); |
||
118 | } |
||
119 | try |
||
120 | { |
||
121 | filteringResult = broker.executePostFiltering(request, response); |
||
122 | passedPost = filteringResult.isSuccess(); |
||
123 | } |
||
124 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
125 | { |
||
126 | throw new ExecutedFilterException( |
||
127 | "a not 'FilterException' exception occured while executing pre filter chain", |
||
128 | e); |
||
129 | } |
||
130 | if (passedPost) |
||
131 | { |
||
132 | //the sucessfull case |
||
1781 | jmachado | 133 | if(!alreadyActive) |
1782 | jmachado | 134 | { |
135 | if(debug) |
||
136 | 1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId() + "COMMITING transaction for service: " + service.getClass().getName() + " method:" + methodName); |
||
1781 | jmachado | 137 | tb.commitTransaction(); |
1782 | jmachado | 138 | } |
1 | fvelez | 139 | return response.getReturnObject(); |
140 | } |
||
141 | else |
||
142 | { |
||
143 | throw new FilterChainFailedException( |
||
144 | "failed to execute postFiltering on service" + service.getClass(),filteringResult); |
||
145 | } |
||
146 | } |
||
147 | catch (FilterChainFailedException ex) |
||
148 | { |
||
149 | //ok let this exception go up |
||
150 | try |
||
151 | { |
||
152 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 153 | if(!alreadyActive) |
154 | tb.abortTransaction(); |
||
1 | fvelez | 155 | } |
156 | catch (StorageException e) |
||
157 | { |
||
158 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
159 | } |
||
160 | throw ex; |
||
161 | } |
||
162 | catch (ExecutedFilterException ex) |
||
163 | { |
||
164 | //ok let this exception go up |
||
165 | try |
||
166 | { |
||
167 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 168 | if(!alreadyActive) |
169 | tb.abortTransaction(); |
||
1 | fvelez | 170 | } |
171 | catch (StorageException e) |
||
172 | { |
||
173 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
174 | } |
||
175 | throw ex; |
||
176 | } |
||
177 | catch (StorageException ex) |
||
178 | { |
||
179 | try |
||
180 | { |
||
181 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 182 | if(!alreadyActive) |
183 | tb.abortTransaction(); |
||
1 | fvelez | 184 | } |
185 | catch (StorageException e) |
||
186 | { |
||
187 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
188 | } |
||
189 | throw new ServiceManagerException("Storage exception while executing transactional service",ex); |
||
190 | } |
||
191 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable ex) |
||
192 | { |
||
193 | //ok let this exception go up |
||
194 | try |
||
195 | { |
||
196 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 197 | if(!alreadyActive) |
198 | tb.abortTransaction(); |
||
1 | fvelez | 199 | } |
200 | catch (StorageException e) |
||
201 | { |
||
202 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
203 | } |
||
204 | throw ex; |
||
205 | } |
||
206 | |||
207 | } |
||
1312 | jmachado | 208 | |
209 | |||
210 | |||
211 | |||
212 | |||
213 | public final 5+0%2Fdocs%2Fapi+Object">Object checkFilters( |
||
214 | 5+0%2Fdocs%2Fapi+Object">Object requester, |
||
215 | IService service, |
||
216 | 1.5.0/docs/api/java/lang/String.html">String methodName, |
||
217 | 5+0%2Fdocs%2Fapi+Object">Object arguments[], |
||
218 | 1.5.0/docs/api/java/lang/String.html">String names[], |
||
219 | IFilterBroker broker) |
||
220 | throws ServiceManagerException, 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
||
221 | { |
||
222 | boolean passedPre = false; |
||
223 | |||
224 | ServiceRequest request = new ServiceRequest(requester, arguments,names,service,methodName); |
||
225 | request.setExcludeChains(new ArrayList<String>()); |
||
226 | request.getExcludeChains().add("Logger"); |
||
227 | ServiceResponse response = new ServiceResponse(); |
||
228 | FilteringResult filteringResult = null; |
||
229 | ITransactionBroker tb = BerserkConfiguration.getApplicationTransactionBroker(); |
||
1781 | jmachado | 230 | boolean alreadyActive = tb.isTransactionActive(); |
1312 | jmachado | 231 | try |
232 | { |
||
1781 | jmachado | 233 | if(debug) |
234 | 1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId() + ": Starting transaction for service: " + service.getClass().getName() + " method:" + methodName); |
||
235 | if(!alreadyActive) |
||
236 | { |
||
237 | tb.beginTransaction(); |
||
238 | } |
||
239 | else |
||
240 | { |
||
241 | 1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId() +" AVISO : ESTA A CHAMAR UM SERVICO TRANSACIONAL DENTRO DE UMA TRANSACAO, VAMOS MANTER A MESMA TRASACAO"); |
||
242 | 1.5.0/docs/api/java/lang/System.html">System.out.println(">>>>>>TRANSACTION WARNING BUT NO PROBLEM: AVISO: Já existia uma transação activa e foi iniciada outra neste momento sem fechar a anterior "); |
||
243 | for (1.5.0/docs/api/java/lang/StackTraceElement.html">StackTraceElement ste : 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getStackTrace()) { |
||
244 | 1.5.0/docs/api/java/lang/System.html">System.out.println("\t" + ste); |
||
245 | } |
||
246 | } |
||
247 | |||
1312 | jmachado | 248 | try |
249 | { |
||
250 | filteringResult = broker.executePreFiltering(request, response); |
||
251 | passedPre = filteringResult.isSuccess(); |
||
252 | } |
||
253 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
254 | { |
||
255 | throw new ExecutedFilterException( |
||
256 | "a not 'FilterException' exception occured while executing pre filter chain", |
||
257 | e); |
||
258 | } |
||
259 | if (passedPre) |
||
260 | { |
||
1781 | jmachado | 261 | if(!alreadyActive) |
1782 | jmachado | 262 | { |
263 | if(debug) |
||
264 | 1.5.0/docs/api/java/lang/System.html">System.out.println("THREAD:" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId() + "Commiting transaction for service: " + service.getClass().getName() + " method:" + methodName); |
||
1781 | jmachado | 265 | tb.commitTransaction(); |
1782 | jmachado | 266 | } |
1312 | jmachado | 267 | return true; |
268 | } |
||
269 | else |
||
270 | { |
||
271 | throw new FilterChainFailedException( |
||
272 | "failed to execute postFiltering on service" + service.getClass(),filteringResult); |
||
273 | } |
||
274 | } |
||
275 | catch (FilterChainFailedException ex) |
||
276 | { |
||
277 | //ok let this exception go up |
||
278 | try |
||
279 | { |
||
280 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 281 | if(!alreadyActive) |
282 | tb.abortTransaction(); |
||
1312 | jmachado | 283 | } |
284 | catch (StorageException e) |
||
285 | { |
||
286 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
287 | } |
||
288 | throw ex; |
||
289 | } |
||
290 | catch (ExecutedFilterException ex) |
||
291 | { |
||
292 | //ok let this exception go up |
||
293 | try |
||
294 | { |
||
295 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 296 | if(!alreadyActive) |
297 | tb.abortTransaction(); |
||
1312 | jmachado | 298 | } |
299 | catch (StorageException e) |
||
300 | { |
||
301 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
302 | } |
||
303 | throw ex; |
||
304 | } |
||
305 | catch (StorageException ex) |
||
306 | { |
||
307 | try |
||
308 | { |
||
309 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 310 | if(!alreadyActive) |
311 | tb.abortTransaction(); |
||
1312 | jmachado | 312 | } |
313 | catch (StorageException e) |
||
314 | { |
||
315 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
316 | } |
||
317 | throw new ServiceManagerException("Storage exception while executing transactional service",ex); |
||
318 | } |
||
319 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable ex) |
||
320 | { |
||
321 | //ok let this exception go up |
||
322 | try |
||
323 | { |
||
324 | //if we get here, something went wrong with the executing service. we should roll back all changes |
||
1781 | jmachado | 325 | if(!alreadyActive) |
326 | tb.abortTransaction(); |
||
1312 | jmachado | 327 | } |
328 | catch (StorageException e) |
||
329 | { |
||
330 | throw new ServiceManagerException("Storage Exception while aborting transaction",e); |
||
331 | } |
||
332 | throw ex; |
||
333 | } |
||
334 | |||
335 | } |
||
1 | fvelez | 336 | } |