KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.RandomAccessFile JavaDoc;
27 import java.nio.ByteBuffer JavaDoc;
28 import java.nio.channels.FileChannel JavaDoc;
29
30 import org.jboss.logging.Logger;
31
32 /**
33  * Simple class that appends <code>ByteBuffer</code>s (each of which contains
34  * a heuristic status log record) to a log file. Information on heuristically
35  * completed transactions does not go to the recovery logs -- it goes to a
36  * separate log file. Since heuristic decisions are very rare, there is no need
37  * to batch heuristic log writes together.
38  *
39  * TODO: In this implementation the heuristic status log file grows whenever
40  * a record is written to it. This is bad for robustness and should be replaced
41  * by an implementation in which the log file has constant lenght (such as the
42  * recovery log implementation).
43  *
44  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
45  * @version $Revision: 37459 $
46  */

47 public class HeuristicStatusLog
48 {
49    /**
50     * Class <code>Logger</code>, for trace messages.
51     */

52    private static Logger errorLog =
53       Logger.getLogger(HeuristicStatusLog.class);
54
55    /**
56     * True if trace messages should be logged.
57     */

58    private static boolean traceEnabled = errorLog.isTraceEnabled();
59
60    /**
61     * The log file.
62     */

63    private File JavaDoc logFile;
64    
65    /**
66     * A <code>RandomAccessFile</code> view of the log file.
67     */

68    RandomAccessFile JavaDoc os;
69    
70    /**
71     * The <code>RandomAccessFile</code>'s underlying <code>FileChannel</code>.
72     */

73    private FileChannel JavaDoc channel;
74    
75    /**
76     * Constructs a new <code>HeuristicStatusLog</code>.
77     *
78     * @param dir the directory in which the log file will be created.
79     * @throws IOException
80     */

81    HeuristicStatusLog(File JavaDoc dir)
82       throws IOException JavaDoc
83    {
84       logFile = File.createTempFile("HEURISTIC_STATUS_LOG", ".log", dir);
85       os = new RandomAccessFile JavaDoc(logFile, "rw");
86       channel = os.getChannel();
87       channel.force(true);
88    }
89
90    /**
91     * Writes one record at the current position of this
92     * <code>HeuristicStatusLog</code>.
93     *
94     * @param record a buffer with the record to be written
95     */

96    void write(ByteBuffer JavaDoc record)
97    {
98       if (traceEnabled)
99       {
100          errorLog.trace("Heuristic status log record:" +
101                         HexDump.fromBuffer(record.array()));
102          errorLog.trace(LogRecord.toString(record));
103       }
104       
105       try
106       {
107          channel.write(record);
108          channel.force(true);
109       }
110       catch (IOException JavaDoc e)
111       {
112          errorLog.error("Error writing heuristic status log " +
113                         logFile.getName(), e);
114       }
115    }
116    
117    /**
118     * Closes this <code>HeuristicStatusLog</code>.
119     */

120    void close()
121    {
122       try
123       {
124          os.close();
125       }
126       catch (IOException JavaDoc e)
127       {
128          errorLog.error("Error closing heuristic status log " +
129                         logFile.getName(), e);
130       }
131    }
132
133 }
134
Popular Tags