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.server.ls;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.core.FileDataImpl;
038:
039: import org.libresource.so6.server.ls.ejb.LibresourceSynchronizerServiceBean;
040: import org.libresource.so6.server.ls.interfaces.LibresourceSynchronizerService;
041:
042: import org.libresource.xml.ImportExportLogger;
043: import org.libresource.xml.LibresourceImportHandler;
044:
045: import org.xml.sax.Attributes;
046:
047: import sun.misc.BASE64Decoder;
048:
049: import java.io.ByteArrayInputStream;
050: import java.io.File;
051: import java.io.FileOutputStream;
052: import java.io.OutputStream;
053:
054: import java.net.URI;
055:
056: import java.text.SimpleDateFormat;
057:
058: import java.util.Date;
059:
060: /**
061: * Synchronizer Import handler
062: * @author <a href="mailto:jourdain@loria.fr">Sebastien Jourdain</a> - <a href="http://www.inria.fr">INRIA Lorraine</a>
063: */
064: public class SynchronizerImportHandler extends LibresourceImportHandler {
065: private URI node;
066: private String name;
067: private String description;
068: private String encoding;
069: private String extensions;
070: private StringBuffer tmpContent;
071:
072: // patch data
073: private long fromTicket = 0;
074: private long toTicket = 0;
075: private long size = 0;
076: private String wsName;
077: private String comment;
078: Date date = null;
079: private boolean contentMode = false;
080: private File tmpContentFile;
081: private OutputStream tmpContentOS;
082:
083: // Sync data
084: private long lastTicket = 0;
085:
086: // service
087: private LibresourceSynchronizerService synchronizerService;
088: private String tmpDecodeBuffer;
089: private BASE64Decoder decoder;
090:
091: public void handleBeginElement(String name, Attributes attributes,
092: ImportExportLogger logger) throws Exception {
093: tmpContent = new StringBuffer();
094: contentMode = false;
095:
096: if (name.equals("patch:file")) {
097: logger.log(ImportExportLogger.MESSAGE,
098: " - Adding patch file " + comment + " (" + size
099: + " o.)");
100: tmpContentFile = File.createTempFile("importPatch", ".bin",
101: new File(LibresourceSynchronizerServiceBean
102: .getDistributedFileSystem()));
103: tmpContentOS = new FileOutputStream(tmpContentFile);
104: contentMode = true;
105: tmpDecodeBuffer = "";
106: }
107: }
108:
109: public boolean handleEndElement(String name,
110: ImportExportLogger logger) throws Exception {
111: //System.out.println("> " + name + "\n" + tmpContent.toString().trim());
112: if (name.equals("so6:synchronizer")) {
113: //System.out.println("createSynchronizer");
114: synchronizerService.createSynchronizer(node, this .name,
115: this .description, this .encoding, this .extensions);
116:
117: //System.out.println("createSynchronizer ok");
118: return false;
119: }
120:
121: if (name.equals("so6:queue")) {
122: return true;
123: }
124:
125: if (name.equals("synchronizer:name")) {
126: this .name = tmpContent.toString().trim();
127:
128: return false;
129: }
130:
131: if (name.equals("synchronizer:description")) {
132: this .description = tmpContent.toString().trim();
133:
134: return false;
135: }
136:
137: if (name.equals("synchronizer:extensions")) {
138: this .extensions = tmpContent.toString().trim();
139:
140: return false;
141: }
142:
143: if (name.equals("synchronizer:encoding")) {
144: this .encoding = tmpContent.toString().trim();
145:
146: return false;
147: }
148:
149: if (name.equals("patch:wsName")) {
150: this .wsName = tmpContent.toString().trim();
151:
152: return false;
153: }
154:
155: if (name.equals("patch:comment")) {
156: this .comment = tmpContent.toString().trim();
157:
158: return false;
159: }
160:
161: if (name.equals("patch:date")) {
162: //this.time = Long.parseLong(tmpContent.toString().trim());
163: this .date = Libresource.parseDate(tmpContent.toString()
164: .trim());
165:
166: return false;
167: }
168:
169: if (name.equals("patch:size")) {
170: this .size = Long.parseLong(tmpContent.toString().trim());
171:
172: return false;
173: }
174:
175: if (name.equals("patch:toTicket")) {
176: this .toTicket = Long
177: .parseLong(tmpContent.toString().trim());
178: }
179:
180: if (name.equals("patch:fromTicket")) {
181: this .fromTicket = Long.parseLong(tmpContent.toString()
182: .trim());
183: }
184:
185: if (name.equals("patch:file")) {
186: contentMode = false;
187: decoder.decodeBuffer(new ByteArrayInputStream(
188: tmpDecodeBuffer.getBytes()), tmpContentOS);
189: tmpContentOS.close();
190:
191: return false;
192: }
193:
194: if (name.equals("so6:patch")) {
195: synchronizerService.addPatchSystem(node, fromTicket,
196: toTicket, new FileDataImpl(tmpContentFile), wsName,
197: comment, date.getTime(), false, false, "");
198:
199: return false;
200: }
201:
202: return false;
203: }
204:
205: public void init(URI node, ImportExportLogger logger)
206: throws Exception {
207: this .node = node;
208: name = null;
209: description = null;
210: tmpContent = null;
211: name = null;
212: description = null;
213: extensions = null;
214: encoding = null;
215: date = null;
216: synchronizerService = (LibresourceSynchronizerService) Libresource
217: .getService("LibresourceSynchronizer");
218: contentMode = false;
219: decoder = new BASE64Decoder();
220: }
221:
222: public void handleContent(String content, ImportExportLogger logger)
223: throws Exception {
224: if (tmpContent != null) {
225: if (contentMode) {
226: tmpDecodeBuffer = tmpDecodeBuffer + content.trim();
227:
228: int decodable = (tmpDecodeBuffer.length() / 4) * 4;
229: decoder.decodeBuffer(new ByteArrayInputStream(
230: tmpDecodeBuffer.substring(0, decodable)
231: .getBytes()), tmpContentOS);
232: tmpDecodeBuffer = tmpDecodeBuffer.substring(decodable);
233: } else {
234: tmpContent.append(content);
235: }
236: }
237: }
238: }
|