1 24 package org.objectweb.jalisto.se.storage.raf.log.synraf.action; 25 26 import org.objectweb.jalisto.se.api.internal.JalistoObject; 27 import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess; 28 import org.objectweb.jalisto.se.exception.JalistoException; 29 import org.objectweb.jalisto.se.impl.InFileAddress; 30 import org.objectweb.jalisto.se.storage.raf.DbByteArrayInputStream; 31 import org.objectweb.jalisto.se.storage.raf.DbByteArrayOutputStream; 32 33 import java.io.*; 34 35 public class LogAction { 36 37 public LogAction() { 38 } 39 40 public LogAction(short actionType, InFileAddress ifa, JalistoObject object) { 41 this.actionType = actionType; 42 this.ifa = ifa; 43 this.object = object; 44 } 45 46 public LogAction(short actionType) { 47 if ((actionType == BEGIN_ACTION) || (actionType == COMMIT_ACTION)) { 48 this.actionType = actionType; 49 } else { 50 throw new JalistoException("not valid action type : " + actionType); 51 } 52 } 53 54 public short getActionType() { 55 return actionType; 56 } 57 58 public void setActionType(short actionType) { 59 this.actionType = actionType; 60 } 61 62 public InFileAddress getIfa() { 63 return ifa; 64 } 65 66 public void setIfa(InFileAddress ifa) { 67 this.ifa = ifa; 68 } 69 70 public JalistoObject getObject() { 71 return object; 72 } 73 74 public void setObject(JalistoObject object) { 75 this.object = object; 76 } 77 78 public void writeTo(DataOutput dest) throws IOException { 79 DbByteArrayOutputStream out = DbByteArrayOutputStream.getInstance(); 80 ObjectOutputStream objOut = new ObjectOutputStream(out); 81 objOut.writeObject(new Short (actionType)); 82 if ((actionType != BEGIN_ACTION) && (actionType != COMMIT_ACTION)) { 83 objOut.writeObject(ifa); 84 if (actionType != INSERT_ACTION) { 85 objOut.writeObject(object); 86 } 87 } 88 int size = out.size(); 89 dest.writeInt(size); 90 out.writeTo(dest); 91 out.reset(); 92 } 93 94 public void readFrom(DataInput source) throws IOException { 95 int size = source.readInt(); 96 byte[] data = new byte[size]; 97 source.readFully(data); 98 ByteArrayInputStream in = DbByteArrayInputStream.getInstance(data); 99 ObjectInputStream objIn = new ObjectInputStream(in); 100 try { 101 actionType = ((Short ) objIn.readObject()).shortValue(); 102 if ((actionType != BEGIN_ACTION) && (actionType != COMMIT_ACTION)) { 103 ifa = (InFileAddress) objIn.readObject(); 104 if (actionType != INSERT_ACTION) { 105 object = (JalistoObject) objIn.readObject(); 106 } 107 } 108 } catch (Exception e) { 109 throw new JalistoException(e); 110 } finally { 111 in.reset(); 112 } 113 } 114 115 public void undoOn(PluggablePhysicalFileAccess physicalAccesInternal) { 116 if ((actionType == BEGIN_ACTION) || (actionType == COMMIT_ACTION)) { 117 return; 118 } 119 if (actionType == INSERT_ACTION) { 120 physicalAccesInternal.deleteFileObject(ifa); 121 } else if (actionType == DELETE_ACTION) { 122 physicalAccesInternal.insertFileObject(ifa, object); 123 } else if (actionType == UPDATE_ACTION) { 124 physicalAccesInternal.updateFileObject(ifa, object); 125 } else { 126 throw new JalistoException(); 127 } 128 } 129 130 public String toString() { 131 StringBuffer sb = new StringBuffer (); 132 sb.append("LogAction("); 133 if (actionType == INSERT_ACTION) { 134 sb.append("insert"); 135 } else if (actionType == DELETE_ACTION) { 136 sb.append("delete"); 137 } else if (actionType == UPDATE_ACTION) { 138 sb.append("update"); 139 } else if (actionType == BEGIN_ACTION) { 140 sb.append("begin)"); 141 return sb.toString(); 142 } else if (actionType == COMMIT_ACTION) { 143 sb.append("commit)"); 144 return sb.toString(); 145 } 146 sb.append(",").append(ifa); 147 if (actionType != INSERT_ACTION) { 148 sb.append(",").append(object); 149 } 150 sb.append(")"); 151 return sb.toString(); 152 } 153 154 private short actionType; 155 private InFileAddress ifa; 156 private JalistoObject object; 157 158 public static final short INSERT_ACTION = 0; 159 public static final short DELETE_ACTION = 1; 160 public static final short UPDATE_ACTION = 2; 161 public static final short BEGIN_ACTION = 3; 162 public static final short COMMIT_ACTION = 4; 163 164 165 static final long serialVersionUID = -7699636669241162559L; 166 } 167 | Popular Tags |