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.command;
034:
035: import org.libresource.so6.core.WsConnection;
036: import org.libresource.so6.core.engine.DBType;
037: import org.libresource.so6.core.engine.util.Base64;
038: import org.libresource.so6.core.engine.util.ObjectCloner;
039:
040: import java.io.FileInputStream;
041: import java.io.IOException;
042: import java.io.Serializable;
043: import java.io.Writer;
044:
045: abstract public class Command implements Serializable, Comparable {
046: private static final long serialVersionUID = 3;
047: public static final String ATTACHEMENT = "attachement";
048: protected long ticket = -1;
049: protected String path;
050: protected boolean conflict = false;
051: protected long time = -1;
052: protected String wsName = "";
053: protected String wspath;
054: protected String attachement; // name of an attached file...
055:
056: public Command(String path, WsConnection ws) {
057: this (path, ws.getWsName(), ws.getPath());
058: }
059:
060: // Default constructor
061: public Command(long ticket, String path, String wsName, long time,
062: boolean conflict, String attachement) {
063: this .ticket = ticket;
064: this .path = path;
065: this .wsName = wsName;
066: this .time = time;
067: this .conflict = conflict;
068: this .attachement = attachement;
069: }
070:
071: protected Command(String path, String wsName, String wspath) {
072: this .wsName = wsName;
073: this .wspath = wspath;
074: this .time = System.currentTimeMillis();
075: this .path = path.replaceAll("\\\\", "/");
076: }
077:
078: // Comparable
079: public int compareTo(Object o) {
080: Command cmd = (Command) o;
081:
082: if (this .getTicket() < cmd.getTicket()) {
083: return -1;
084: }
085:
086: if (this .getTicket() == cmd.getTicket()) {
087: return 0;
088: }
089:
090: if (this .getTicket() > cmd.getTicket()) {
091: return 1;
092: }
093:
094: return 0;
095: }
096:
097: public Command max(Command c) {
098: if (this .getTicket() > c.getTicket()) {
099: return this ;
100: } else {
101: return c;
102: }
103: }
104:
105: public Command min(Command c) {
106: if (this .getTicket() <= c.getTicket()) {
107: return this ;
108: } else {
109: return c;
110: }
111: }
112:
113: abstract public void execute(String dir, DBType dbt)
114: throws Exception;
115:
116: abstract public boolean equals(Object obj);
117:
118: public Object clone() {
119: Object result = null;
120:
121: try {
122: result = ObjectCloner.deepCopy(this );
123: } catch (Exception e) {
124: e.printStackTrace();
125:
126: //System.exit(1);
127: }
128:
129: return result;
130: }
131:
132: public String toString() {
133: String cmdName = getClass().getName();
134: cmdName = cmdName.substring(cmdName.lastIndexOf(".") + 1);
135:
136: return "(" + this .getTicket() + ")." + cmdName;
137: }
138:
139: // Common data
140: public String getPath() {
141: return path;
142: }
143:
144: public void setPath(String path) {
145: this .path = path;
146: }
147:
148: public long getTicket() {
149: return ticket;
150: }
151:
152: public void setTicket(long ticket) {
153: this .ticket = ticket;
154: }
155:
156: public boolean isConflict() {
157: return conflict;
158: }
159:
160: public void setConflict(boolean conflict) {
161: this .conflict = conflict;
162: }
163:
164: public String getWsName() {
165: return wsName;
166: }
167:
168: public void setWsName(String string) {
169: wsName = string;
170: }
171:
172: public long getTime() {
173: return time;
174: }
175:
176: public void setTime(long l) {
177: time = l;
178: }
179:
180: // For attachement
181: public String getAttachement() {
182: return attachement;
183: }
184:
185: public void setAttachement(String string) {
186: attachement = string;
187: }
188:
189: /**
190: * @param osw
191: */
192: public void toXML(Writer osw) throws IOException {
193: osw.write("<ticket>" + ticket + "</ticket>");
194: osw.write("<from>"
195: + Base64.encodeBytes(wsName.getBytes("UTF-8"))
196: + "</from>");
197: osw.write("<time>" + time + "</time>");
198: osw.write("<path>" + Base64.encodeBytes(path.getBytes("UTF-8"))
199: + "</path>");
200:
201: if (attachement != null) {
202: osw.write("<" + ATTACHEMENT + ">");
203:
204: // with stream
205: FileInputStream fis = new FileInputStream(attachement);
206: byte[] b = new byte[3145728];
207: int bytecount = 0;
208:
209: while ((bytecount = fis.read(b)) > 0) {
210: osw.write(Base64.encodeBytes(b, 0, bytecount));
211: }
212:
213: fis.close();
214: osw.write("</" + ATTACHEMENT + ">");
215: osw.flush();
216: }
217: }
218: }
|