1 45 46 47 package org.openejb.core.stateful; 48 49 50 import java.io.RandomAccessFile ; 51 import java.io.File ; 52 import java.util.Enumeration ; 53 import java.util.Hashtable ; 54 import java.util.Properties ; 55 56 import org.openejb.spi.Serializer; 57 58 59 public class RAFPassivater implements PassivationStrategy{ 61 62 int fileID = 0; 63 Hashtable masterTable = new Hashtable (); 64 65 static class Pointer { 66 int fileid; 67 long filepointer; 68 int bytesize; 69 public Pointer(int file, long pointer, int bytecount){ 70 fileid = file; 71 filepointer = pointer; 72 bytesize = bytecount; 73 } 74 } 75 76 public void init(Properties props) throws org.openejb.SystemException {} 77 78 79 public synchronized void passivate(Hashtable stateTable) 80 throws org.openejb.SystemException{ 81 try{ 82 fileID++; 83 84 RandomAccessFile ras = new RandomAccessFile (System.getProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "passivation"+fileID+".ser","rw"); 85 Enumeration enumeration = stateTable.keys(); 86 Pointer lastPointer = null; 87 while(enumeration.hasMoreElements()){ 88 Object id = enumeration.nextElement(); 89 Object obj = stateTable.get(id); 90 byte [] bytes = Serializer.serialize(obj); 91 long filepointer = ras.getFilePointer(); 92 93 if( lastPointer == null ) lastPointer = new Pointer(fileID, filepointer, (int)(filepointer)); 94 else lastPointer = new Pointer(fileID, filepointer, (int)(filepointer-lastPointer.filepointer)); 95 96 masterTable.put(id,lastPointer); 97 ras.write(bytes); 98 } 99 ras.close( ); 100 }catch(Exception e){ 101 throw new org.openejb.SystemException(e); 102 } 103 } 104 105 public synchronized Object activate(Object primaryKey) 106 throws org.openejb.SystemException{ 107 108 Pointer pointer = (Pointer)masterTable.get(primaryKey); 109 if(pointer == null) 110 return null; 111 112 try{ 113 RandomAccessFile ras = new RandomAccessFile (System.getProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "passivation"+pointer.fileid+".ser","r"); 114 byte [] bytes = new byte[(int)pointer.bytesize]; 115 ras.seek(pointer.filepointer); 116 ras.readFully(bytes); 117 ras.close(); 118 return Serializer.deserialize(bytes); 119 }catch(Exception e){ 120 throw new org.openejb.SystemException(e); 121 } 122 123 } 124 125 } | Popular Tags |