1 package org.smartcomps.twister.common.transaction;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.smartcomps.twister.common.lifecycle.LifecycleAwareResource;
6 import org.smartcomps.twister.common.lifecycle.LifecycleException;
7 import org.smartcomps.twister.common.configuration.CommonConfiguration;
8
9 import java.util.*;
10
11 /***
12 * The TransactionManager manages TransactionalResource implementations and tries to
13 * apply (if possible) an all or nothing policy with those resources.
14 * @see TransactionException
15 * @see TransactionalResource
16 */
17 public class TransactionManager implements LifecycleAwareResource {
18
19 private static Log log = LogFactory.getLog(TransactionManager.class);
20 private static ThreadLocal txStatus = new ThreadLocal();
21
22 public static Collection resources = null;
23
24 public void create() throws LifecycleException {
25 // Get the resource list
26 List resourcesList = CommonConfiguration.getTransactionResources();
27
28 if (resourcesList != null) {
29 // Build the resources collection
30 resources = new ArrayList();
31 try {
32 for (int m = 0; m < resourcesList.size(); m++) {
33 String resClassName = (String) resourcesList.get(m);
34 resources.add((TransactionalResource)Class.forName(resClassName).newInstance());
35 }
36 } catch (InstantiationException e) {
37 throw new LifecycleException("A transactional resource could not be instantiated, could not start.", e);
38 } catch (IllegalAccessException e) {
39 throw new LifecycleException("A transactional resource could not be accessed, could not start.", e);
40 } catch (ClassNotFoundException e) {
41 throw new LifecycleException("A transactional resource could not be found in classpath, could not start.", e);
42 } catch (ClassCastException e) {
43 throw new LifecycleException("A class declared as a transactional resource doesn't implement the right " +
44 "interface, could not start.", e);
45 }
46 }
47
48 // Initialize resources
49 try {
50 for (Iterator resourceIter = resources.iterator(); resourceIter.hasNext();) {
51 TransactionalResource transactionalResource = (TransactionalResource) resourceIter.next();
52 transactionalResource.initialize();
53 }
54 } catch (TransactionException e) {
55 throw new LifecycleException("A transactional resource could not be initialized, could not start.", e);
56 }
57
58 }
59
60 public void start() throws LifecycleException { }
61
62 public void destroy() throws LifecycleException {
63 resources = null;
64 }
65
66 public void stop() throws LifecycleException { }
67
68 public static void beginTransaction() throws TransactionException {
69 txStatus.set(Boolean.TRUE);
70 for (Iterator resourceIter = resources.iterator(); resourceIter.hasNext();) {
71 TransactionalResource transactionalResource = (TransactionalResource) resourceIter.next();
72 transactionalResource.beginTransaction();
73 }
74 }
75
76 public static void commitTransaction() throws TransactionException {
77 for (Iterator resourceIter = resources.iterator(); resourceIter.hasNext();) {
78 TransactionalResource transactionalResource = (TransactionalResource) resourceIter.next();
79 transactionalResource.commitTransaction();
80 }
81 txStatus.set(Boolean.FALSE);
82 }
83
84 public static void rollbackTransaction() throws TransactionException {
85 for (Iterator resourceIter = resources.iterator(); resourceIter.hasNext();) {
86 TransactionalResource transactionalResource = (TransactionalResource) resourceIter.next();
87 transactionalResource.rollbackTransaction();
88 }
89 txStatus.set(Boolean.FALSE);
90 }
91
92 public static boolean hasTransaction() {
93 return ((Boolean)txStatus.get()).booleanValue();
94 }
95 }
This page was automatically generated by Maven