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 2002-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 *
44 * $Id: BaseTransactionLogEntry.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 is the base entry that is streamed into the transaction log file.
59 * All other transaction log entry classes must extend this class.
60 * <p>
61 * Each entry has the associated XID and the created time stamp, along with
62 * the identity of the resource that created the entry
63 */
64 public abstract class BaseTransactionLogEntry implements Externalizable {
65
66 /***
67 * This is the unique id used to identify the version of the class
68 * for the purpose of Serialization
69 */
70 static final long serialVersionUID = 2;
71
72 /***
73 * This is the transaction identity that this entry belongs too
74 */
75 private ExternalXid _externalXid = null;
76
77 /***
78 * This is the resource identity that this entry belongs too
79 */
80 private String _resourceId;
81
82 /***
83 * This is a time stamp when the entry was created
84 */
85 private long _created = -1;
86
87 /***
88 * This is the time that the transaction expires in ms. It is initialized
89 * to zero, which means that it never expired
90 */
91 private long _expiryTime = 0;
92
93
94 /***
95 * Default constructor for serialization
96 */
97 BaseTransactionLogEntry() {
98 }
99
100 /***
101 * Instantiate an instance of this class with a transaction identifier
102 * resource identifier and created date
103 *
104 * @param txid - the transaction identifier
105 * @param rid - the resource identifier
106 * @param created - timestamp for this entry
107 */
108 BaseTransactionLogEntry(ExternalXid txid, String rid, long created) {
109 _externalXid = txid;
110 _resourceId = rid;
111 _created = created;
112 }
113
114 /***
115 * Create a new instance populating it with the state of the specified
116 * object
117 *
118 * @param copy - object to copy
119 */
120 BaseTransactionLogEntry(BaseTransactionLogEntry copy) {
121 _externalXid = new ExternalXid(copy._externalXid);
122 _resourceId = copy._resourceId;
123 _created = copy._created;
124 _expiryTime = copy._expiryTime;
125 }
126
127 /***
128 * Set the transaction identifier
129 *
130 * @param txid - the transaction identifier
131 */
132 public void setExternalXid(ExternalXid txid) {
133 _externalXid = txid;
134 }
135
136 /***
137 * Get the transaction identifier
138 *
139 * @return ExternalXid
140 */
141 public ExternalXid getExternalXid() {
142 return _externalXid;
143 }
144
145 /***
146 * Set the resource identity for the entry
147 *
148 * @param rid - the resource identity
149 */
150 public void setResourceId(String rid) {
151 _resourceId = rid;
152 }
153
154 /***
155 * Get the resource identifier
156 *
157 * @return the resource identifier
158 */
159 public String getResourceId() {
160 return _resourceId;
161 }
162
163 /***
164 * Set the time in ms that this record was created
165 *
166 * @param time - time in ms
167 */
168 public void setCreated(long time) {
169 _created = time;
170 }
171
172 /***
173 * Set the created time of this entry to now
174 */
175 public void setCreated() {
176 _created = System.currentTimeMillis();
177 }
178
179 /***
180 * Return the time that this entry was created
181 *
182 * @return long
183 */
184 public long getCreated() {
185 return _created;
186 }
187
188
189 /***
190 * Set the expiry time for this transaction, which is an absolute time in
191 * milliseconds.
192 *
193 * @param long - absolute expiry time
194 */
195 public void setExpiryTime(long time) {
196 _expiryTime = time;
197 }
198
199 /***
200 * Retrieve the expiry time of this transaction
201 *
202 * @return long
203 */
204 public long getExpiryTime() {
205 return _expiryTime;
206 }
207
208 /***
209 * Check whether the trnasaction has expired
210 *
211 * @return boolean - true if it has expired; false otherwise
212 */
213 public boolean transactionExpired() {
214 return System.currentTimeMillis() > _expiryTime;
215 }
216
217
218 public void writeExternal(ObjectOutput stream)
219 throws IOException {
220 if (isValid()) {
221 stream.writeLong(serialVersionUID);
222 stream.writeObject(_externalXid);
223 stream.writeUTF(_resourceId);
224 stream.writeLong(_created);
225 } else {
226 throw new IOException("writeExternal : entry has invalid state");
227 }
228 }
229
230
231 public void readExternal(ObjectInput stream)
232 throws IOException, ClassNotFoundException {
233 long version = stream.readLong();
234 if (version == serialVersionUID) {
235 _externalXid = (ExternalXid) stream.readObject();
236 _resourceId = stream.readUTF();
237 _created = stream.readLong();
238 if (!isValid()) {
239 throw new IOException("readExternal : entry has invalid state");
240 }
241 } else {
242 throw new IOException("No support for BaseTransactionLogEntry " +
243 "with version " + version);
244 }
245 }
246
247 /***
248 * Verify that this record has a valid state, which is denoted by
249 * _externalXid , resourceId and _created being not equal to -1.
250 *
251 * @return boolean - true if the entry is valid
252 */
253 boolean isValid() {
254 return ((_externalXid != null) &&
255 (_resourceId != null) &&
256 (_created != -1));
257 }
258
259 }
260