KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > LogFile


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28 //----------------------------------------------------------------------------
29
//
30
// Module: LogFile.java
31
//
32
// Description: Log File interface.
33
//
34
// Product: com.sun.jts.CosTransactions
35
//
36
// Author: Simon Holdsworth
37
//
38
// Date: March, 1997
39
//
40
// Copyright (c): 1995-1997 IBM Corp.
41
//
42
// The source code for this program is not published or otherwise divested
43
// of its trade secrets, irrespective of what has been deposited with the
44
// U.S. Copyright Office.
45
//
46
// This software contains confidential and proprietary information of
47
// IBM Corp.
48
//----------------------------------------------------------------------------
49

50 package com.sun.jts.CosTransactions;
51
52 // Import required classes.
53

54 import java.util.*;
55 import java.util.logging.Logger JavaDoc;
56 import java.util.logging.Level JavaDoc;
57 import com.sun.logging.LogDomains;
58 import com.sun.jts.utils.LogFormatter;
59
60 /**The LogFile interface provides operations that control the
61  * individual log entries that make up the physical log. It allows writing to
62  * the log and reading from the log, along with the capability to close a
63  * portion of the log. Different physical logs can be placed on the system
64  * with only minor changes to the methods contained in this class.
65  *
66  * @version 0.01
67  *
68  * @author Simon Holdsworth, IBM Corporation
69  *
70  * @see
71 */

72 //----------------------------------------------------------------------------
73
// CHANGE HISTORY
74
//
75
// Version By Change Description
76
// 0.01 SAJH Initial implementation.
77
//-----------------------------------------------------------------------------
78

79 class LogFile {
80
81     /**Constants for write types.
82      */

83     final static int UNFORCED = 0;
84     final static int FORCED = 1;
85
86     /**Constants for log record types.
87      */

88     final static int NORMAL = 0;
89     final static int KEYPOINT_START = 1;
90     final static int KEYPOINT_END = 2;
91     final static int REWRITE = 3;
92     
93     /*
94         Logger to log transaction messages
95     */

96     
97     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
98
99     /**The handle of the log file.
100      */

101     LogHandle handle = null;
102
103     /**LogFile constructor.
104      *
105      * @param LogHandle
106      *
107      * @return
108      *
109      * @see
110      */

111     LogFile( LogHandle handle ) {
112
113         // Set up the instance variables to those values passed in.
114

115         this.handle = handle;
116
117     }
118
119     /**Writes a log record to the physical log.
120      * <p>
121      * Supports either a force or unforced option with force requiring an immediate
122      * write to the log and unforced keeping the data until a force is done somewhere
123      * else in the log service.
124      * <p>
125      * The LSN of the written log record is an output parameter.
126      * <p>
127      * Returns true if the write completed successfully and false if the write
128      * did not complete.
129      *
130      * @param writeType Forced/non-forced write indicator.
131      * @param record Log record data.
132      * @param recordType Log record type.
133      * @param recordLSN LSN of the written record.
134      *
135      * @return
136      *
137      * @see
138      */

139     synchronized boolean write( int writeType,
140                                 byte[] record,
141                                 int recordType,
142                                 LogLSN recordLSN ) {
143
144         boolean result = true;
145
146         // Write the record.
147
// Set the result based on return code from log write.
148

149         try {
150             LogLSN resultLSN = handle.writeRecord(record,recordType,
151                                                   (writeType==LogFile.FORCED ? LogHandle.FORCE : LogHandle.BUFFER));
152             if( recordLSN != null )
153                 recordLSN.copy(resultLSN);
154         } catch( LogException le ) {
155             _logger.log(Level.SEVERE,"jts.log_error",le.toString());
156              String JavaDoc msg = LogFormatter.getLocalizedMessage(_logger,"jts.log_error",
157                                         new java.lang.Object JavaDoc[] {le.toString()});
158             throw new org.omg.CORBA.INTERNAL JavaDoc(msg);
159             //if( recordLSN != null )
160
//recordLSN.copy(LogLSN.NULL_LSN);
161
//result = false;
162
}
163
164         return result;
165     }
166
167     /**Informs the log that all log records older than the one with the given LSN
168      * are no longer required.
169      * <p>
170      * The checkpoint marks the point where log processing will begin in the event
171      * of recovery processing. This will generally correspond to the last record
172      * before a successful keypoint.
173      *
174      * @param firstLSN
175      *
176      * @return
177      *
178      * @see
179      */

180     synchronized boolean checkpoint( LogLSN firstLSN ) {
181
182         boolean result = true;
183         LogLSN checkLSN;
184
185         // If the LSN passed in is NULL, assume it means the head.
186

187         if( firstLSN.isNULL() )
188             checkLSN = new LogLSN(LogLSN.HEAD_LSN);
189         else
190             checkLSN = new LogLSN(firstLSN);
191
192         // Checkpoint the log.
193

194         try {
195             handle.checkLSN(checkLSN);
196             handle.truncate(checkLSN,LogHandle.TAIL_NOT_INCLUSIVE);
197         } catch( LogException le ) {
198             result = false;
199         }
200
201         return result;
202     }
203
204     /**Writes the given information in the restart record for the log.
205      * <p>
206      *
207      * @param record The information to be written.
208      *
209      * @return Indicates success of the operation.
210      *
211      * @see
212      */

213     synchronized boolean writeRestart( byte[] record ) {
214
215         boolean result = false;
216
217         // Write the restart information.
218

219         try {
220             handle.writeRestart(record);
221             result = true;
222         } catch( LogException le ) {
223             result = false;
224         }
225
226         return result;
227     }
228
229     /**Reads the restart record from the log.
230      * <p>
231      *
232      * @param
233      *
234      * @return The restart record.
235      *
236      * @see
237      */

238     synchronized byte[] readRestart() {
239         byte[] result = null;
240
241         // Write the restart information.
242

243         try {
244             result = handle.readRestart();
245         } catch( LogException le ) {
246         }
247
248         return result;
249     }
250
251     /**Closes the portion of the log defined by the LogFile object reference.
252      * <p>
253      * Deletes the associated logfile if requested.
254      *
255      * @param deleteFile
256      *
257      * @return
258      *
259      * @see
260      */

261     synchronized boolean close( boolean deleteFile ) {
262
263         boolean result = true;
264
265         // Call to close the physical log.
266

267         try {
268             handle.closeFile(deleteFile);
269         } catch( LogException le ) {
270             result = false;
271         }
272
273         return result;
274     }
275
276     /**Returns all of the log records written to the log since the last checkpoint.
277      * <p>
278      * The caller is responsible for freeing the sequence storage.
279      * <p>
280      * If the log is empty, an empty sequence is returned.
281      * <p>
282      * The result is returned in a Vector as we do not know ahead of time how
283      * many log records there are.
284      *
285      * @param
286      *
287      * @return The log records.
288      *
289      * @see
290      */

291     synchronized Vector getLogRecords() {
292         Vector logRecords = new Vector();
293         boolean keypointEndFound = false;
294         LogCursor logCursor;
295
296         // Open a cursor for use with the log.
297

298         try {
299             logCursor = handle.openCursor(LogLSN.HEAD_LSN,LogLSN.TAIL_LSN);
300         } catch( LogException le ) {
301
302             return new Vector();
303         }
304
305         // Read each log record from the physical log and place in temporary queue.
306

307         try {
308             LogLSN lsn = new LogLSN();
309             int[] recordType = new int[1];
310
311             for(;;) {
312                 byte[] logRecord = logCursor.readCursor(recordType,lsn);
313
314                 // Process the log record depending on its type.
315

316                 switch( recordType[0] ) {
317
318                     // If the record is a keypoint start, and we have found the end of the
319
// keypoint, then we can stop processing the log. If the end has not been
320
// found then a failure must have occurred during the keypoint operation,
321
// so we must continue to process the log.
322
// We do not do anything with the contents of the keypoint start record.
323

324                 case LogFile.KEYPOINT_START :
325                     if( keypointEndFound )
326                         throw new LogException(null,LogException.LOG_END_OF_CURSOR,2);
327                     break;
328
329                     // If the record is a keypoint end, remember this so that we can stop when
330
// we find the start of the keypoint.
331
// We do not do anything with the contents of the keypoint end record.
332

333                 case LogFile.KEYPOINT_END :
334                     keypointEndFound = true;
335                     break;
336
337                     // For a normal log record, add the records to the list.
338
// For a rewritten record, only add the record to the list if the
339
// keypoint end record has been found.
340

341                 case LogFile.NORMAL :
342                 case LogFile.REWRITE :
343                     if( (recordType[0] == LogFile.NORMAL) || keypointEndFound )
344                         logRecords.addElement(logRecord);
345                     break;
346
347                     // Any other type of log record is ignored.
348

349                 default :
350                     break;
351                 }
352             }
353         } catch( LogException le ) {
354
355             // If any exception other that END_OF_CURSOR was thrown, then return an empty
356
// list.
357

358             if( le.errorCode != LogException.LOG_END_OF_CURSOR ) {
359                 return new Vector();
360             }
361         }
362
363         // Close the cursor.
364

365         try {
366             handle.closeCursor(logCursor);
367         } catch( LogException le ) {
368         }
369
370         return logRecords;
371     }
372
373     /**Dumps the state of the object.
374      *
375      * @param
376      *
377      * @return
378      *
379      * @see
380      */

381     void dump() {
382         //! somtrDump_OBJECT_HEADER;
383

384         // Dump all of the instance variables in the LogFile object, without going
385
// any further down object references.
386

387     }
388 }
389
Popular Tags