001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine;
034:
035: import org.libresource.so6.core.command.Command;
036:
037: import java.io.File;
038: import java.io.FileInputStream;
039: import java.io.FileNotFoundException;
040: import java.io.FileOutputStream;
041: import java.io.IOException;
042: import java.io.ObjectInputStream;
043: import java.io.ObjectOutputStream;
044: import java.io.Serializable;
045:
046: import java.util.ArrayList;
047: import java.util.ListIterator;
048:
049: /**
050: * @author molli
051: */
052: public class OpVectorFsImpl implements OpVector, Serializable {
053: private String root; // where to put commands...
054: private ArrayList paths = new ArrayList();
055: private long fromTicket;
056: private long toTicket;
057:
058: public OpVectorFsImpl(String root) {
059: this .root = root;
060: this .fromTicket = -1;
061: this .toTicket = -1;
062: }
063:
064: public void clear() throws Exception {
065: paths.clear();
066: this .fromTicket = -1;
067: this .toTicket = -1;
068: }
069:
070: public void load() {
071: int pos = 0;
072: Command cmd = getCommand(0);
073:
074: if (cmd == null) {
075: return;
076: }
077:
078: long ticket = cmd.getTicket();
079: fromTicket = ticket;
080:
081: while ((cmd = getCommand(pos++)) != null) {
082: if (cmd.getTicket() == ticket++) {
083: paths.add(cmd.getPath());
084: toTicket = ticket - 1;
085: } else {
086: return;
087: }
088: }
089: }
090:
091: public void add(Command cmd) throws Exception {
092: try {
093: FileOutputStream fo = new FileOutputStream(root
094: + File.separator + paths.size());
095: ObjectOutputStream so = new ObjectOutputStream(fo);
096: so.writeObject(cmd);
097: so.flush();
098: so.close();
099: paths.add(cmd.getPath());
100:
101: if (fromTicket == -1) {
102: fromTicket = cmd.getTicket();
103: }
104:
105: toTicket = cmd.getTicket();
106: } catch (FileNotFoundException e) {
107: e.printStackTrace();
108: } catch (IOException e) {
109: e.printStackTrace();
110: }
111: }
112:
113: public void update(Command cmd, int pos) {
114: try {
115: FileOutputStream fo = new FileOutputStream(root
116: + File.separator + pos);
117: ObjectOutputStream so = new ObjectOutputStream(fo);
118: so.writeObject(cmd);
119: so.flush();
120: so.close();
121:
122: //oids.add(pos, cmd.getPath());
123: } catch (FileNotFoundException e) {
124: e.printStackTrace();
125: } catch (IOException e) {
126: e.printStackTrace();
127: }
128: }
129:
130: public int size() throws Exception {
131: return paths.size();
132: }
133:
134: public Command getCommand(int pos) {
135: Command cmd = null;
136: File f = new File(root + File.separator + pos);
137:
138: if (!(f.exists())) {
139: return null;
140: }
141:
142: try {
143: FileInputStream fi = new FileInputStream(f);
144: ObjectInputStream si = new ObjectInputStream(fi);
145: cmd = (Command) si.readObject();
146: si.close();
147: } catch (FileNotFoundException e) {
148: return null;
149: } catch (IOException e) {
150: throw new RuntimeException(e);
151: } catch (ClassNotFoundException e) {
152: throw new RuntimeException(e);
153: }
154:
155: // if (cmd.getAttachement() != null) {
156: // String tmppath = File.createTempFile("grouss", "grouss").getPath();
157: // FileUtil.copy(cmd.getAttachement(), tmppath);
158: // cmd.setAttachement(tmppath);
159: // }
160: return cmd;
161: }
162:
163: public void close() throws Exception {
164: }
165:
166: public ListIterator getCommands() throws Exception {
167: return new CmdIterator();
168: }
169:
170: public long getFromTicket() {
171: return fromTicket;
172: }
173:
174: public long getToTicket() {
175: return toTicket;
176: }
177:
178: public ArrayList getPathList() throws Exception {
179: return paths;
180: }
181:
182: class CmdIterator implements ListIterator {
183: private int i = 0;
184:
185: CmdIterator() {
186: }
187:
188: public void close() {
189: }
190:
191: public Object next() {
192: Command cmd = null;
193:
194: if (i < paths.size()) {
195: cmd = getCommand(i);
196: i++;
197:
198: return cmd;
199: }
200:
201: return null;
202: }
203:
204: public void set(Object c) {
205: OpVectorFsImpl.this .update((Command) c, i - 1);
206: }
207:
208: public void remove() {
209: throw new RuntimeException("methode remove not implemented");
210: }
211:
212: public boolean hasNext() {
213: return (getCommand(i) != null);
214: }
215:
216: public int nextIndex() {
217: return i;
218: }
219:
220: public int previousIndex() {
221: return i - 1;
222: }
223:
224: public boolean hasPrevious() {
225: return i > 0;
226: }
227:
228: public Object previous() {
229: return getCommand(previousIndex());
230: }
231:
232: public void add(Object o) {
233: throw new RuntimeException("methode remove not implemented");
234: }
235: }
236: }
|