KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > recovery > PendingWriteRequest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.recovery;
23
24 import EDU.oswego.cs.dl.util.concurrent.Latch;
25
26 import java.nio.ByteBuffer JavaDoc;
27
28 /**
29  * Represents one pending "write record" operation to be performed on
30  * a <code>BatchLog</code>.
31  *
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
34  * @version $Revision: 37459 $
35  */

36 class PendingWriteRequest
37 {
38    /**
39     * Type of a tx committed or tx prepared record for a transaction that
40     * involves multiple TMs.
41     */

42    static final int TYPE_TX_MULTI_TM = 1;
43    
44    /**
45     * Type of a tx committed record for a transaction that involves a single TM.
46     */

47    static final int TYPE_TX_SINGLE_TM = 0;
48    
49    /**
50     * Type of an tx end record for a transaction that involves a multiple TMs.
51     * There is no tx end record for a transaction that involves a single TM.
52     * */

53    static final int TYPE_END = -1;
54    
55    /**
56     * A buffer containing the record to be written to a <code>BatchLog</code>.
57     */

58    private ByteBuffer JavaDoc buffer;
59    
60    /**
61     * The <code>BatchLog</code> to which the record should be written.
62     */

63    private BatchLog log;
64
65    /**
66     * A <code>Latch</code> that will be released after the pending write
67     * operation is performed. Whoever actually performs the write operation
68     * is responsible for releasing this latch.
69     */

70    private Latch latch;
71    
72    /**
73     * The <code>TxCompletionHandler</code> associated with a commit record or
74     * with a prepare record to be written to a log. The two-phase commit
75     * implementation should invoke the completion handler at the end of the
76     * second phase of the protocol for the transaction that has the pending
77     * commit or prepare record.
78     */

79    private TxCompletionHandler completionHandler;
80    
81    /**
82     * Either contains an exception thrown during an attempt to perform the
83     * pending write record operation, or null if no exception was thrown.
84     */

85    private Exception JavaDoc failure;
86    
87    /**
88     * Indicates the type of the record to be written to a <code>BatchLog</code>.
89     * Possible values are <code>TYPE_TX_MULTI_TM</code>,
90     * <code>TYPE_TX_SINGLE_TM</code>, and <code>TX_END</code>.
91     *
92     */

93    private int type;
94
95    /**
96     * Creates a <code>PendingWriteRequest</code> for a commit or prepare
97     * record.
98     *
99     * @param buffer a buffer containing the commit or prepare record
100     * @param latch a latch that will be released after the pending write
101     * operation is performed
102     * @param multiTmTransaction true if the record refers to a transaction
103     * that involves multiple TMs (and requires a tx end record),
104     * false if the record refers to a transaction that involves
105     * a single TM (and requires no tx end record).
106     */

107    PendingWriteRequest(ByteBuffer JavaDoc buffer,
108                        Latch latch,
109                        boolean multiTmTransaction)
110    {
111       this.buffer = buffer;
112       this.latch = latch;
113       this.log = null;
114       type = (multiTmTransaction) ? TYPE_TX_MULTI_TM : TYPE_TX_SINGLE_TM;
115    }
116
117    /**
118     * Creates a <code>PendingWriteRequest</code> for a tx end record to be
119     * written to a given <code>BatchLog</code>. This method takes an argument
120     * that speficies the <code>BatchLog</code> because the tx end record for a
121     * transaction must be written to the same <code>BatchLog</code> as the
122     * transaction's tx committed or tx prepared record.
123     *
124     * @param buffer a buffer containing the tx end record
125     * @param latch a latch that will be released after the pending write
126     * operation is performed
127     * @param log the <code>BatchLog</code> to which the tx end record should
128     * be written.
129     */

130    PendingWriteRequest(ByteBuffer JavaDoc buffer, Latch latch, BatchLog log)
131    {
132       this.buffer = buffer;
133       this.latch = latch;
134       this.log = log;
135       type = TYPE_END;
136    }
137
138    /**
139     * Waits until this pending write record is performed. If an exception
140     * was thrown while attempting to perform the write record, this method
141     * throws a <code>RuntimeException</code> that contains the original
142     * exception.
143     *
144     * @return the completion handler associated with a tx committed or with
145     * a tx prepared record.
146     */

147    TxCompletionHandler waitTilDone()
148    {
149       try
150       {
151          latch.acquire();
152          if (failure != null)
153             throw failure;
154          return completionHandler;
155       }
156       catch (Exception JavaDoc e)
157       {
158          throw new RuntimeException JavaDoc(e);
159       }
160    }
161
162    /**
163     * Gets the buffer containing the record to be written.
164     *
165     * @return a <code>ByteBuffer</code> instance containing the record.
166     */

167    ByteBuffer JavaDoc getBuffer()
168    {
169       return buffer;
170    }
171    
172    /**
173     * Gets the type of the record to be written.
174     *
175     * @return <code>TYPE_TX_MULTI_TM</code>, or
176     * <code>TYPE_TX_SINGLE_TM</code>, or <code>TX_END</code>.
177     */

178    int getType()
179    {
180       return type;
181    }
182
183    /**
184     * Gets the <code>BatchLog</code> to which the record should be written.
185     *
186     * @return the <code>BatchLog</code> to which the record should be written.
187     */

188    BatchLog getLogger()
189    {
190       return log;
191    }
192
193    /**
194     * Associates a completion handler with the pending tx committed or
195     * tx prepared record.
196     *
197     * @param completionHandler the new completion handler.
198     */

199    void setCompletionHandler(TxCompletionHandler completionHandler)
200    {
201       this.completionHandler = completionHandler;
202    }
203
204    /**
205     * Stores an exception thrown during an attempt to perform the pending
206     * write operation. The method <code>waitTilDone</code> will throw a
207     * a <code>RuntimeException</code> that contains the stored exception.
208     *
209     * @param failure an exception to be stored.
210     */

211    void setFailure(Exception JavaDoc failure)
212    {
213       this.failure = failure;
214    }
215
216 }
217
Popular Tags