1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.kandula.initiator;
18
19 import org.apache.axis2.addressing.EndpointReference;
20 import org.apache.kandula.Status;
21 import org.apache.kandula.context.AbstractContext;
22 import org.apache.kandula.context.ContextFactory;
23 import org.apache.kandula.context.impl.ATActivityContext;
24 import org.apache.kandula.faults.AbstractKandulaException;
25 import org.apache.kandula.faults.InvalidStateException;
26 import org.apache.kandula.storage.StorageFactory;
27 import org.apache.kandula.storage.Store;
28 import org.apache.kandula.utility.EndpointReferenceFactory;
29 import org.apache.kandula.wsat.completion.CompletionCoordinatorPortTypeRawXMLStub;
30 import org.apache.kandula.wscoor.ActivationCoordinatorPortTypeRawXMLStub;
31 import org.apache.kandula.wscoor.RegistrationCoordinatorPortTypeRawXMLStub;
32
33 /***
34 * @author Dasarath Weeratunge
35 * @author <a href="mailto:thilina@apache.org"> Thilina Gunarathne </a>
36 */
37
38 public class TransactionManager {
39
40 private static ThreadLocal threadInfo;
41
42 private String axis2Home, axis2Xml;
43
44 public TransactionManager(String coordinationType,
45 EndpointReference coordinatorEPR) throws AbstractKandulaException {
46
47 threadInfo = new ThreadLocal();
48 AbstractContext context = ContextFactory.getInstance().createActivity(
49 coordinationType, coordinatorEPR);
50 if (threadInfo.get() != null)
51 throw new IllegalStateException();
52 threadInfo.set(context.getProperty(ATActivityContext.REQUESTER_ID));
53 Store store = StorageFactory.getInstance().getInitiatorStore();
54 store.put(context.getProperty(ATActivityContext.REQUESTER_ID), context);
55 }
56
57 public void begin(String axis2Home, String axis2Xml) throws Exception {
58 begin(axis2Home,axis2Xml,false);
59 }
60 /***
61 * @throws Exception
62 */
63 public void begin(String axis2Home, String axis2Xml, boolean async) throws Exception {
64 this.axis2Home = axis2Home;
65 this.axis2Xml = axis2Xml;
66 AbstractContext context = getTransaction();
67 String id = (String) context
68 .getProperty(AbstractContext.REQUESTER_ID);
69 ActivationCoordinatorPortTypeRawXMLStub activationCoordinator = new ActivationCoordinatorPortTypeRawXMLStub(
70 axis2Home, axis2Xml, (EndpointReference) context
71 .getProperty(AbstractContext.ACTIVATION_EPR));
72 activationCoordinator.createCoordinationContextOperation(
73 context,async);
74 while (async & context.getCoordinationContext() == null) {
75
76 Thread.sleep(10);
77 }
78 RegistrationCoordinatorPortTypeRawXMLStub registrationCoordinator = new RegistrationCoordinatorPortTypeRawXMLStub(
79 axis2Home, axis2Xml, context.getCoordinationContext()
80 .getRegistrationService());
81
82 EndpointReference registrationRequeterPortEPR = EndpointReferenceFactory
83 .getInstance().getCompletionInitiatorEndpoint(id);
84 registrationCoordinator.registerOperation( context,registrationRequeterPortEPR,async);
85 while (async & context.getProperty(ATActivityContext.COORDINATION_EPR) == null) {
86 Thread.sleep(10);
87 }
88 }
89
90 public void commit() throws Exception {
91 AbstractContext context = getTransaction();
92 EndpointReference coordinationEPR = (EndpointReference) context
93 .getProperty(ATActivityContext.COORDINATION_EPR);
94 CompletionCoordinatorPortTypeRawXMLStub stub = new CompletionCoordinatorPortTypeRawXMLStub(
95 axis2Home, axis2Xml, coordinationEPR);
96 stub.commitOperation();
97 while ((context.getStatus() != Status.ParticipantStatus.STATUS_COMMITED)
98 & (context.getStatus() != Status.ParticipantStatus.STATUS_ABORTED)) {
99 Thread.sleep(10);
100 }
101 if ((context.getStatus() == Status.ParticipantStatus.STATUS_ABORTED)) {
102 throw new Exception("Aborted");
103 }
104 forgetTransaction();
105 }
106
107 public void rollback() throws Exception {
108 AbstractContext context = getTransaction();
109 EndpointReference coordinationEPR = (EndpointReference) context
110 .getProperty(ATActivityContext.COORDINATION_EPR);
111 CompletionCoordinatorPortTypeRawXMLStub stub = new CompletionCoordinatorPortTypeRawXMLStub(
112 axis2Home, axis2Xml, coordinationEPR);
113 stub.rollbackOperation();
114 while ((context.getStatus() != Status.ParticipantStatus.STATUS_COMMITED)
115 | (context.getStatus() != Status.ParticipantStatus.STATUS_ABORTED)) {
116 Thread.sleep(10);
117 }
118 forgetTransaction();
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public static AbstractContext getTransaction()
139 throws AbstractKandulaException {
140 Object key = threadInfo.get();
141 AbstractContext context = (AbstractContext) StorageFactory
142 .getInstance().getInitiatorStore().get(key);
143 if (context == null) {
144 throw new InvalidStateException("No Activity Found");
145 }
146 return context;
147 }
148 public static void forgetTransaction()
149 {
150 threadInfo.set(null);
151 }
152 }