1 22 package org.jboss.tm.recovery; 23 24 import java.io.DataInputStream ; 25 import java.io.DataOutputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.nio.ByteBuffer ; 32 import java.util.zip.Adler32 ; 33 import java.util.zip.Checksum ; 34 35 import javax.management.ObjectName ; 36 37 import org.jboss.system.ServiceMBeanSupport; 38 import org.jboss.tm.LocalId; 39 import org.jboss.tm.XidFactoryMBean; 40 41 53 public class XidFactoryInitializationService 54 extends ServiceMBeanSupport 55 implements XidFactoryInitializationServiceMBean 56 { 57 private ObjectName xidFactory; 58 private String filename; 59 private int nextTxGenerationNumber = 0; 60 61 63 66 protected void startService() 67 throws Exception 68 { 69 super.startService(); 70 XidFactoryMBean xidFactoryObj = 71 (XidFactoryMBean) getServer().getAttribute(xidFactory, "Instance"); 72 73 File nextTxGenerationFile = new File (filename); 74 nextTxGenerationFile = nextTxGenerationFile.getAbsoluteFile(); 75 if (!nextTxGenerationFile.createNewFile()) 76 { 77 InputStream in = new FileInputStream (nextTxGenerationFile); 79 DataInputStream dataIn = new DataInputStream (in); 80 int txGenNumberFromFile = dataIn.readInt(); 81 int checksumFromFile = dataIn.readInt(); 82 dataIn.close(); 83 84 ByteBuffer buffer = ByteBuffer.allocate(4); 86 buffer.putInt(txGenNumberFromFile); 87 Checksum checksum = new Adler32 (); 88 checksum.update(buffer.array(), 0, 4); 89 if ((int) checksum.getValue() != checksumFromFile) 90 throw new RuntimeException ("Incorrect checksum in file " + 91 nextTxGenerationFile + ". Could not " + 92 "obtain the next transaction " + 93 "generation number."); 94 95 File backupFile = new File (filename + ".bak"); 97 backupFile.delete(); 98 nextTxGenerationFile.renameTo(backupFile); 99 nextTxGenerationNumber = txGenNumberFromFile; 100 } 101 102 xidFactoryObj.setGlobalIdNumber(LocalId.assemble(nextTxGenerationNumber, 104 0 )); 105 106 nextTxGenerationNumber++; 108 ByteBuffer buffer = ByteBuffer.allocate(4); 109 buffer.putInt(nextTxGenerationNumber); 110 Checksum checksum = new Adler32 (); 111 checksum.update(buffer.array(), 0, 4); 112 113 FileOutputStream out= new FileOutputStream (nextTxGenerationFile); 114 DataOutputStream dataOut = new DataOutputStream (out); 115 dataOut.writeInt(nextTxGenerationNumber); 116 dataOut.writeInt((int) checksum.getValue()); 117 dataOut.flush(); 118 out.getFD().sync(); 119 dataOut.close(); 120 } 121 122 124 127 public ObjectName getXidFactory() 128 { 129 return xidFactory; 130 } 131 132 136 public void setXidFactory(ObjectName xidFactory) 137 { 138 this.xidFactory = xidFactory; 139 } 140 141 144 public String getNextTxGenerationFile() 145 { 146 return filename; 147 } 148 149 153 public void setNextTxGenerationFile(String filename) 154 { 155 this.filename = filename; 156 } 157 158 161 public int getNextTxGenerationNumber() 162 { 163 return nextTxGenerationNumber; 164 } 165 166 } 167 | Popular Tags |