View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2001 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   *
44   * $Id: TransactionState.java,v 1.1 2004/11/26 01:51:01 tanderson Exp $
45   *
46   * Date			Author  Changes
47   * 20/11/2001   jima    Created
48   */
49  package org.exolab.jms.tranlog;
50  
51  import java.io.Externalizable;
52  import java.io.IOException;
53  import java.io.ObjectInput;
54  import java.io.ObjectOutput;
55  
56  
57  /***
58   * This class defines the various states of a transaction that a
59   * {@link org.exolab.jms.messagemgr.ResourceManager} participates in..
60   *
61   * Opened       transaction is in an open state
62   * Prepared     transaction is in a prepared state
63   * Closed       transaction is in a closed state
64   *
65   * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:51:01 $
66   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
67   **/
68  public final class TransactionState
69      implements Externalizable {
70  
71      /***
72       * This is the unique id used to identify the version of the class
73       * for the purpose of Serialization
74       */
75      static final long serialVersionUID = 1;
76  
77      /***
78       * The state of the transaction as a string.
79       */
80      private String _state;
81  
82      /***
83       * The state of the transaction as an integer
84       */
85      private int _ord;
86  
87  
88      /***
89       * Default constructor for Serialization
90       */
91      public TransactionState() {
92      }
93  
94      /***
95       * Limit the creation scope of this object. All instances of this
96       * class are created at class initialisation time.
97       *
98       * @param state - the state as a string
99       * @param ord - the ordinal value of the state
100      */
101     private TransactionState(String state, int ord) {
102         _state = state;
103         _ord = ord;
104     }
105 
106     /***
107      * Returns the ordinal value for this state
108      *
109      * @return      int
110      */
111     public int getOrd() {
112         return _ord;
113     }
114 
115     /***
116      * Returns the name of this state
117      *
118      * @return       String
119      */
120     public String toString() {
121         return _state;
122     }
123 
124     /***
125      * Return true iff the two specified object are of the same type
126      * and their ordinal values are identical.
127      *
128      * @param       obj         object to compare against
129      * @return      boolean     true if objects are equivalent
130      */
131     public boolean equals(Object obj) {
132         boolean result = false;
133 
134         if ((obj instanceof TransactionState) &&
135             ((TransactionState) obj).getOrd() == getOrd()) {
136             result = true;
137         }
138 
139         return result;
140     }
141 
142     /***
143      * Check if the transaction state is set to opened
144      *
145      * @return boolean - ture if it is
146      */
147     public boolean isOpened() {
148         return _ord == OPENED.getOrd();
149     }
150 
151     /***
152      * Check if the transaction state is set to prepared
153      *
154      * @return boolean - ture if it is
155      */
156     public boolean isPrepared() {
157         return _ord == PREPARED.getOrd();
158     }
159 
160     /***
161      * Check if the transaction state is set to closed
162      *
163      * @return boolean - ture if it is
164      */
165     public boolean isClosed() {
166         return _ord == CLOSED.getOrd();
167     }
168 
169 
170     // implementation of Externalizable.writeExternal
171     public void writeExternal(ObjectOutput stream)
172         throws IOException {
173         stream.writeLong(serialVersionUID);
174         stream.writeObject(_state);
175         stream.writeInt(_ord);
176     }
177 
178     // implementation of Externalizable.writeExternal
179     public void readExternal(ObjectInput stream)
180         throws IOException, ClassNotFoundException {
181         long version = stream.readLong();
182         if (version == serialVersionUID) {
183             _state = (String) stream.readObject();
184             _ord = stream.readInt();
185         } else {
186             throw new IOException("No support for TransactionState " +
187                 "with version " + version);
188         }
189     }
190 
191     /***
192      * The public ordinal values for each of the enumerates states
193      */
194     public static final int OPENED_ORD = 1;
195     public static final int PREPARED_ORD = 2;
196     public static final int CLOSED_ORD = 3;
197 
198     /***
199      * Instatiate instances related to each of the enumerations. New enumerations
200      * must be added to the end
201      */
202     public static final TransactionState OPENED = new TransactionState("opened", OPENED_ORD);
203     public static final TransactionState PREPARED = new TransactionState("prepared", PREPARED_ORD);
204     public static final TransactionState CLOSED = new TransactionState("closed", CLOSED_ORD);
205 }