KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.nio.ByteBuffer JavaDoc;
28 import java.nio.channels.FileChannel JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Simple implementation of <code>HeuristicStatusLogReader</code> used at
33  * recovery time. The <code>BatchRecoveryLogger</code>'s implementation of
34  * method <code>getHeuristicStatusLogs()</code> instantiates
35  * <code>SimpleHeuristicStatusLogReader</code>s for the existing heuristic
36  * status log files. It returns an array containing those readers, which the
37  * recovery manager uses to get information on heuristically completed
38  * transactions.
39  *
40  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
41  * @version $Revision: 37459 $
42  */

43 public class SimpleHeuristicStatusLogReader
44    implements HeuristicStatusLogReader
45 {
46    /** The underlying heuristic status log file. */
47    private File JavaDoc logFile;
48    
49    /**
50     * Constructs a <code>SimpleHeuristicStatusLogReader</code>.
51     *
52     * @param logFile the heuristic status log file to read.
53     */

54    public SimpleHeuristicStatusLogReader(File JavaDoc logFile)
55    {
56       this.logFile = logFile;
57    }
58
59    /**
60     * Gets the name of the heuristic status log file.
61     *
62     * @return the name of the heuristic status log file.
63     */

64    public String JavaDoc getLogFileName()
65    {
66       return logFile.toString();
67    }
68
69    /**
70     * Recovers information on heuristically completed transactions from
71     * the heuristic status log file.
72     *
73     * @param heuristicallyCompletedTransactions a <code>Map</code> to which
74     * this method will one entry per heuristically completed
75     * transaction. The map keys are <code>Long</code> values
76     * containing local transaction ids. The map values are
77     * <code>LogRecord.HeurData</code> objects with information
78     * on heuristically completed transactions.
79     */

80    public void recover(Map JavaDoc heuristicallyCompletedTransactions)
81    {
82       FileInputStream JavaDoc fis;
83
84       try
85       {
86          fis = new FileInputStream JavaDoc(logFile);
87
88       }
89       catch (IOException JavaDoc e)
90       {
91          throw new RuntimeException JavaDoc(e);
92       }
93
94       try
95       {
96          if (fis.available() < LogRecord.FULL_HEADER_LEN)
97             return; // TODO: perhaps thrown an exception in this case?
98

99          FileChannel JavaDoc channel = fis.getChannel();
100          ByteBuffer JavaDoc buf = ByteBuffer.allocate(LogRecord.FULL_HEADER_LEN);
101          channel.read(buf);
102          
103          int len = LogRecord.getNextRecordLength(buf, 0);
104          LogRecord.HeurData data = new LogRecord.HeurData();
105          
106          while (len > 0)
107          {
108             buf = ByteBuffer.allocate(len + LogRecord.FULL_HEADER_LEN);
109             if (channel.read(buf) < len)
110                break; // TODO: throw an exception or log something
111
buf.flip();
112             LogRecord.getHeurData(buf, len, data);
113             switch (data.recordType)
114             {
115             case LogRecord.HEUR_STATUS:
116                heuristicallyCompletedTransactions.put(
117                                              new Long JavaDoc(data.localTransactionId),
118                                              data);
119                break;
120                
121             case LogRecord.HEUR_FORGOTTEN:
122                heuristicallyCompletedTransactions.remove(
123                                              new Long JavaDoc(data.localTransactionId));
124                break;
125                
126             default:
127                // TODO: log something
128
break;
129             }
130             len = LogRecord.getNextRecordLength(buf, len);
131          }
132       }
133       catch (IOException JavaDoc ignore)
134       {
135       }
136       try
137       {
138          fis.close();
139       }
140       catch (IOException JavaDoc e)
141       {
142          throw new RuntimeException JavaDoc(e);
143       }
144
145    }
146
147    /**
148     * Removes the heuristic status log file.
149     */

150    public void finishRecovery()
151    {
152       logFile.delete();
153    }
154
155 }
156
Popular Tags