KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > storage > raf > log > synraf > action > LogAction


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc(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 JavaDoc) 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 JavaDoc 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 JavaDoc toString() {
131         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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