KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > AbstractSimpleService


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/AbstractSimpleService.java,v 1.9 2004/07/28 09:38:22 ib Exp $
3  * $Revision: 1.9 $
4  * $Date: 2004/07/28 09:38:22 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.common;
25
26 import java.util.Vector JavaDoc;
27
28 import javax.transaction.xa.XAException JavaDoc;
29 import javax.transaction.xa.XAResource JavaDoc;
30 import javax.transaction.xa.Xid JavaDoc;
31
32 import org.apache.slide.transaction.SlideTransactionManager;
33
34 /**
35  * Slide Service simple implementation. Will allow only one simultaneous
36  * transaction context.
37  *
38  * @deprecated do not use as it only allows for a single concurrent transaction context
39  * @version $Revision: 1.9 $
40  */

41 public abstract class AbstractSimpleService extends AbstractServiceBase
42     implements Service {
43     
44     
45     // -------------------------------------------------------------- Constants
46

47     
48     public static final int TX_IDLE = 0;
49     public static final int TX_PREPARED = 1;
50     public static final int TX_SUSPENDED = 2;
51     
52     
53     // ----------------------------------------------------- Instance Variables
54

55     
56     /**
57      * Current transaction context.
58      */

59     protected Xid JavaDoc currentContext = null;
60     
61     
62     /**
63      * Status reagrding the current job.
64      */

65     protected int status = TX_IDLE;
66     
67     
68     /**
69      * Transaction timeout.
70      */

71     protected int transactionTimeout =
72         SlideTransactionManager.DEFAULT_TRANSACTION_TIMEOUT;
73     
74     
75     /**
76      * Rollback only.
77      */

78     protected boolean rollbackOnly = false;
79     
80     
81     // ----------------------------------------------------- XAResource Mathods
82

83     
84     /**
85      * Commit the global transaction specified by xid.
86      *
87      * @param xid A global transaction identifier
88      * @param onePhase If true, the resource manager should use a one-phase
89      * commit protocol to commit the work done on behalf of xid.
90      * @exception XAException An error has occurred. Possible XAExceptions
91      * are XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
92      * XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO. If the resource
93      * manager did not commit the transaction and the paramether onePhase is
94      * set to true, the resource manager may throw one of the XA_RB*
95      * exceptions. Upon return, the resource manager has rolled back the
96      * branch's work and has released all held resources.
97      */

98     public void commit(Xid JavaDoc xid, boolean onePhase)
99         throws XAException JavaDoc {
100         
101         if (currentContext == null)
102             throw new XAException JavaDoc(XAException.XAER_NOTA);
103     if (xid == null)
104         throw new XAException JavaDoc(XAException.XAER_INVAL);
105         if (currentContext.getGlobalTransactionId()
106             != xid.getGlobalTransactionId())
107             throw new XAException JavaDoc(XAException.XAER_PROTO);
108         
109         if (!onePhase && status != TX_PREPARED)
110             throw new XAException JavaDoc(XAException.XAER_PROTO);
111         if (onePhase && (!((status == TX_IDLE) || (status == TX_SUSPENDED))))
112             throw new XAException JavaDoc(XAException.XAER_PROTO);
113         
114         if (rollbackOnly) {
115             rollback(xid);
116             throw new XAException JavaDoc(XAException.XA_RBROLLBACK);
117         }
118         
119         currentContext = null;
120         status = TX_IDLE;
121         rollbackOnly = false;
122         
123     }
124     
125     
126     /**
127      * Ends the work performed on behalf of a transaction branch. The
128      * resource manager disassociates the XA resource from the transaction
129      * branch specified and let the transaction be completed.
130      * If TMSUSPEND is specified in flags, the transaction branch is
131      * temporarily suspended in incomplete state. The transaction context is
132      * in suspened state and must be resumed via start with TMRESUME specified.
133      * If TMFAIL is specified, the portion of work has failed. The resource
134      * manager may mark the transaction as rollback-only.
135      * If TMSUCCESS is specified, the portion of work has completed
136      * successfully.
137      *
138      * @param xid A global transaction identifier that is the same as what
139      * was used previously in the start method.
140      * @param flags One of TMSUCCESS, TMFAIL, or TMSUSPEND
141      * @exception XAException An error has occurred. Possible XAException
142      * values are XAER_RMERR, XAER_RMFAILED, XAER_NOTA, XAER_INVAL,
143      * XAER_PROTO, or XA_RB*.
144      */

145     public void end(Xid JavaDoc xid, int flags)
146         throws XAException JavaDoc {
147         
148         if (currentContext == null)
149             throw new XAException JavaDoc(XAException.XAER_NOTA);
150     if (xid == null)
151         throw new XAException JavaDoc(XAException.XAER_INVAL);
152         if (currentContext.getGlobalTransactionId()
153             != xid.getGlobalTransactionId())
154             throw new XAException JavaDoc(XAException.XAER_PROTO);
155         
156         if (flags == XAResource.TMSUSPEND)
157             status = TX_SUSPENDED;
158         
159         if (flags == XAResource.TMFAIL)
160             rollbackOnly = true;
161         
162     }
163     
164     
165     /**
166      * Tell the resource manager to forget about a heuristically completed
167      * transaction branch.
168      *
169      * @param xid A global transaction identifier
170      * @exception XAException An error has occurred. Possible exception values
171      * are XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
172      */

173     public void forget(Xid JavaDoc xid)
174         throws XAException JavaDoc {
175         
176         if (currentContext == null)
177             throw new XAException JavaDoc(XAException.XAER_NOTA);
178     if (xid == null)
179         throw new XAException JavaDoc(XAException.XAER_INVAL);
180         if (currentContext.getGlobalTransactionId()
181             != xid.getGlobalTransactionId())
182             throw new XAException JavaDoc(XAException.XAER_PROTO);
183         
184         currentContext = null;
185         status = TX_IDLE;
186         rollbackOnly = false;
187         
188     }
189     
190     
191     /**
192      * Obtain the current transaction timeout value set for this XAResource
193      * instance. If XAResource.setTransactionTimeout was not use prior to
194      * invoking this method, the return value is the default timeout set for
195      * the resource manager; otherwise, the value used in the previous
196      * setTransactionTimeout call is returned.
197      *
198      * @return the transaction timeout value in seconds.
199      * @exception XAException An error has occurred. Possible exception
200      * values are XAER_RMERR, XAER_RMFAIL.
201      */

202     public int getTransactionTimeout()
203         throws XAException JavaDoc {
204         
205         return transactionTimeout;
206         
207     }
208     
209     
210     /**
211      * This method is called to determine if the resource manager instance
212      * represented by the target object is the same as the resouce manager
213      * instance represented by the parameter xares.
214      *
215      * @param xares An XAResource object whose resource manager instance is
216      * to be compared with the resource manager instance of the target object.
217      * @return true if it's the same RM instance; otherwise false.
218      * @exception XAException An error has occurred. Possible exception values
219      * are XAER_RMERR, XAER_RMFAIL.
220      */

221     public boolean isSameRM(XAResource JavaDoc xares)
222         throws XAException JavaDoc {
223         
224         if (xares == null)
225             return false;
226         
227         return (this == xares);
228         
229     }
230     
231     
232     /**
233      * Ask the resource manager to prepare for a transaction commit of the
234      * transaction specified in xid.
235      *
236      * @param xid A global transaction identifier
237      * @return A value indicating the resource manager's vote on the outcome
238      * of the transaction. The possible values are: XA_RDONLY or XA_OK. If
239      * the resource manager wants to roll back the transaction, it should do
240      * so by raising an appropriate XAException in the prepare method.
241      * @exception XAException An error has occurred. Possible exception
242      * values are: XA_RB*, XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL,
243      * or XAER_PROTO.
244      */

245     public int prepare(Xid JavaDoc xid)
246         throws XAException JavaDoc {
247         
248         if (currentContext == null)
249             throw new XAException JavaDoc(XAException.XAER_NOTA);
250     if (xid == null)
251         throw new XAException JavaDoc(XAException.XAER_INVAL);
252         if (currentContext.getGlobalTransactionId()
253             != xid.getGlobalTransactionId())
254             throw new XAException JavaDoc(XAException.XAER_PROTO);
255         
256         if (!((status == TX_IDLE) || (status == TX_SUSPENDED)))
257             throw new XAException JavaDoc(XAException.XAER_PROTO);
258         
259         if (rollbackOnly) {
260             // FIXME: Don't know if should be automatically rollbacked in that
261
// case
262
throw new XAException JavaDoc(XAException.XA_RBROLLBACK);
263         }
264         
265         status = TX_PREPARED;
266         
267         return XAResource.XA_OK;
268         
269     }
270     
271     
272     /**
273      * Obtain a list of prepared transaction branches from a resource manager.
274      * The transaction manager calls this method during recovery to obtain the
275      * list of transaction branches that are currently in prepared or
276      * heuristically completed states.
277      *
278      * @param flag One of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS. TMNOFLAGS must
279      * be used when no other flags are set in flags.
280      * @return The resource manager returns zero or more XIDs for the
281      * transaction branches that are currently in a prepared or heuristically
282      * completed state. If an error occurs during the operation, the resource
283      * manager should throw the appropriate XAException.
284      * @exception XAException An error has occurred. Possible values are
285      * XAER_RMERR, XAER_RMFAIL, XAER_INVAL, and XAER_PROTO.
286      */

287     public Xid JavaDoc[] recover(int flag)
288         throws XAException JavaDoc {
289         
290         Vector JavaDoc list = new Vector JavaDoc();
291         
292         if ((status == TX_PREPARED) && (currentContext != null))
293            list.addElement(currentContext);
294         
295         return (Xid JavaDoc[]) list.toArray(new Xid JavaDoc[list.size()]);
296         
297     }
298     
299     
300     /**
301      * Inform the resource manager to roll back work done on behalf of a
302      * transaction branch.
303      *
304      * @param xid A global transaction identifier
305      * @exception XAException An error has occurred
306      */

307     public void rollback(Xid JavaDoc xid)
308         throws XAException JavaDoc {
309         
310         if (currentContext == null)
311             throw new XAException JavaDoc(XAException.XAER_NOTA);
312     if (xid == null)
313         throw new XAException JavaDoc(XAException.XAER_INVAL);
314         if (currentContext.getGlobalTransactionId()
315             != xid.getGlobalTransactionId())
316             throw new XAException JavaDoc(XAException.XAER_PROTO);
317         
318         status = TX_IDLE;
319         currentContext = null;
320         rollbackOnly = false;
321         
322     }
323     
324     
325     /**
326      * Set the current transaction timeout value for this XAResource instance.
327      *
328      * @param seconds the transaction timeout value in seconds.
329      * @return true if transaction timeout value is set successfully;
330      * otherwise false.
331      * @exception XAException An error has occurred. Possible exception
332      * values are XAER_RMERR, XAER_RMFAIL, or XAER_INVAL.
333      */

334     public boolean setTransactionTimeout(int seconds)
335         throws XAException JavaDoc {
336         transactionTimeout = seconds;
337         return true;
338     }
339     
340     
341     /**
342      * Start work on behalf of a transaction branch specified in xid If
343      * TMJOIN is specified, the start is for joining a transaction previously
344      * seen by the resource manager. If TMRESUME is specified, the start is
345      * to resume a suspended transaction specified in the parameter xid. If
346      * neither TMJOIN nor TMRESUME is specified and the transaction specified
347      * by xid has previously been seen by the resource manager, the resource
348      * manager throws the XAException exception with XAER_DUPID error code.
349      *
350      * @param xid A global transaction identifier to be associated with the
351      * resource
352      * @param flags One of TMNOFLAGS, TMJOIN, or TMRESUME
353      * @exception XAException An error has occurred. Possible exceptions are
354      * XA_RB*, XAER_RMERR, XAER_RMFAIL, XAER_DUPID, XAER_OUTSIDE, XAER_NOTA,
355      * XAER_INVAL, or XAER_PROTO.
356      */

357     public void start(Xid JavaDoc xid, int flags)
358         throws XAException JavaDoc {
359         
360     if (xid == null)
361         throw new XAException JavaDoc(XAException.XAER_INVAL);
362         if ( (currentContext != null) &&
363              (currentContext.getGlobalTransactionId()
364               != xid.getGlobalTransactionId()) )
365             throw new XAException JavaDoc(XAException.XAER_OUTSIDE);
366         
367         switch (flags) {
368         case XAResource.TMNOFLAGS:
369             if (currentContext != null)
370                 throw new XAException JavaDoc(XAException.XAER_INVAL);
371             currentContext = xid;
372             break;
373         case XAResource.TMJOIN:
374             if (currentContext == null)
375                 throw new XAException JavaDoc(XAException.XAER_NOTA);
376             if (currentContext.getGlobalTransactionId()
377                 != xid.getGlobalTransactionId())
378                 throw new XAException JavaDoc(XAException.XAER_INVAL);
379             break;
380         case XAResource.TMRESUME:
381             if (currentContext == null)
382                 throw new XAException JavaDoc(XAException.XAER_NOTA);
383             if (status != TX_SUSPENDED)
384                 throw new XAException JavaDoc(XAException.XAER_INVAL);
385             status = TX_IDLE;
386             break;
387         }
388         
389     }
390     
391     
392 }
393
394
Popular Tags