KickJava   Java API By Example, From Geeks To Geeks.

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


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.DataInputStream JavaDoc;
25 import java.io.DataOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.nio.ByteBuffer JavaDoc;
32 import java.util.zip.Adler32 JavaDoc;
33 import java.util.zip.Checksum JavaDoc;
34
35 import javax.management.ObjectName JavaDoc;
36
37 import org.jboss.system.ServiceMBeanSupport;
38 import org.jboss.tm.LocalId;
39 import org.jboss.tm.XidFactoryMBean;
40
41 /**
42  * MBean service that distinguishes "transaction generations", which correspond
43  * to different executions of a JBoss server, by stuffing a "transaction
44  * generation number" into the high part of all the local transaction ids
45  * created by an execution of the server. This service keeps the next
46  * transaction generation number (the value that the next server run will stuff
47  * into the high part of its local transaction ids) in a file, which it reads
48  * and updates at server startup time.
49  *
50  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
51  * @version $Revision: 37459 $
52  */

53 public class XidFactoryInitializationService
54       extends ServiceMBeanSupport
55       implements XidFactoryInitializationServiceMBean
56 {
57    private ObjectName JavaDoc xidFactory;
58    private String JavaDoc filename;
59    private int nextTxGenerationNumber = 0;
60
61    // ServiceMBeanSupport override ----------------------------------
62

63    /**
64     * @see org.jboss.system.ServiceMBeanSupport#startService()
65     */

66    protected void startService()
67       throws Exception JavaDoc
68    {
69       super.startService();
70       XidFactoryMBean xidFactoryObj =
71          (XidFactoryMBean) getServer().getAttribute(xidFactory, "Instance");
72       
73       File JavaDoc nextTxGenerationFile = new File JavaDoc(filename);
74       nextTxGenerationFile = nextTxGenerationFile.getAbsoluteFile();
75       if (!nextTxGenerationFile.createNewFile())
76       {
77          // Read existing file.
78
InputStream JavaDoc in = new FileInputStream JavaDoc(nextTxGenerationFile);
79          DataInputStream JavaDoc dataIn = new DataInputStream JavaDoc(in);
80          int txGenNumberFromFile = dataIn.readInt();
81          int checksumFromFile = dataIn.readInt();
82          dataIn.close();
83
84          // Verify the checksum.
85
ByteBuffer JavaDoc buffer = ByteBuffer.allocate(4);
86          buffer.putInt(txGenNumberFromFile);
87          Checksum JavaDoc checksum = new Adler32 JavaDoc();
88          checksum.update(buffer.array(), 0, 4);
89          if ((int) checksum.getValue() != checksumFromFile)
90             throw new RuntimeException JavaDoc("Incorrect checksum in file " +
91                                        nextTxGenerationFile + ". Could not " +
92                                        "obtain the next transaction " +
93                                        "generation number.");
94
95          // Rename existing file
96
File JavaDoc backupFile = new File JavaDoc(filename + ".bak");
97          backupFile.delete();
98          nextTxGenerationFile.renameTo(backupFile);
99          nextTxGenerationNumber = txGenNumberFromFile;
100       }
101       
102       // Set the transaction generation in the Xid factory.
103
xidFactoryObj.setGlobalIdNumber(LocalId.assemble(nextTxGenerationNumber,
104                                                        0 /* tx number */));
105       
106       // Increment and save the next transaction generation number.
107
nextTxGenerationNumber++;
108       ByteBuffer JavaDoc buffer = ByteBuffer.allocate(4);
109       buffer.putInt(nextTxGenerationNumber);
110       Checksum JavaDoc checksum = new Adler32 JavaDoc();
111       checksum.update(buffer.array(), 0, 4);
112
113       FileOutputStream JavaDoc out= new FileOutputStream JavaDoc(nextTxGenerationFile);
114       DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(out);
115       dataOut.writeInt(nextTxGenerationNumber);
116       dataOut.writeInt((int) checksum.getValue());
117       dataOut.flush();
118       out.getFD().sync();
119       dataOut.close();
120    }
121    
122    // XidFactoryInitializationServiceMBean implementation -----------
123

124    /**
125     * @see org.jboss.tm.recovery.XidFactoryInitializationServiceMBean#getXidFactory()
126     */

127    public ObjectName JavaDoc getXidFactory()
128    {
129       return xidFactory;
130    }
131
132    /**
133     * @see org.jboss.tm.recovery.XidFactoryInitializationServiceMBean#setXidFactory(
134     * javax.management.ObjectName)
135     */

136    public void setXidFactory(ObjectName JavaDoc xidFactory)
137    {
138       this.xidFactory = xidFactory;
139    }
140
141    /**
142     * @see org.jboss.tm.recovery.XidFactoryInitializationServiceMBean#getNextTxGenerationFile()
143     */

144    public String JavaDoc getNextTxGenerationFile()
145    {
146       return filename;
147    }
148
149    /**
150     * @see org.jboss.tm.recovery.XidFactoryInitializationServiceMBean#setNextTxGenerationFile(
151     * java.lang.String)
152     */

153    public void setNextTxGenerationFile(String JavaDoc filename)
154    {
155       this.filename = filename;
156    }
157
158    /**
159     * @see org.jboss.tm.recovery.XidFactoryInitializationServiceMBean#getNextTxGenerationNumber()
160     */

161    public int getNextTxGenerationNumber()
162    {
163       return nextTxGenerationNumber;
164    }
165
166 }
167
Popular Tags