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.exec;
034:
035: import org.libresource.so6.core.StateMonitoring;
036: import org.libresource.so6.core.WsConnection;
037: import org.libresource.so6.core.command.Command;
038: import org.libresource.so6.core.command.fs.AddBinaryFile;
039: import org.libresource.so6.core.command.fs.Remove;
040: import org.libresource.so6.core.command.text.AddTxtFile;
041: import org.libresource.so6.core.command.xml.AddXmlFile;
042: import org.libresource.so6.core.engine.log.LogUtils;
043: import org.libresource.so6.core.engine.util.Base64;
044: import org.libresource.so6.core.engine.util.FileUtils;
045:
046: import java.io.BufferedOutputStream;
047: import java.io.File;
048: import java.io.FileOutputStream;
049: import java.io.OutputStreamWriter;
050:
051: import java.util.logging.Logger;
052:
053: /**
054: * The <code>ChangeType</code> class is used to change the type of a
055: * previously syncronized file.
056: * <p>
057: * For exemple, an XML file has been detected as a text file but as it's an
058: * important file, you prefere to synchronize it as a binary file to prevent
059: * inside merge. Then you call this class to do so.
060: * <p>
061: * Some Exemples :
062: * <ul>
063: * <li>java -cp so6.jar ChangeType .so6/1/so6.properties build.xml BIN</li>
064: * <li>java -cp so6.jar ChangeType .so6/1/so6.properties build.xml XML</li>
065: * <li>java -cp so6.jar ChangeType .so6/1/so6.properties build.xml TXT</li>
066: * </ul>
067: *
068: * @author Smack
069: * @version 1.0, 26/05/04
070: * @see org.libresource.so6.core.exec.Main
071: * @since JDK1.4
072: */
073: public class ChangeType {
074: public final static String SUPPORTED_TYPE = "BIN TXT XML";
075:
076: public ChangeType(String wsPath, String filePath, String fileType)
077: throws Exception {
078: LogUtils.removeAllHandlers(Logger.getLogger("ui.log"));
079: LogUtils.removeAllHandlers(StateMonitoring.getInstance()
080: .getXMLMonitoringLogger());
081:
082: if (SUPPORTED_TYPE.indexOf(fileType.toUpperCase()) == -1) {
083: throw new Exception("Invalide type declaration: "
084: + fileType + " is not a valide type. ("
085: + SUPPORTED_TYPE + ")");
086: }
087:
088: WsConnection ws = new WsConnection(wsPath);
089: File file = new File(ws.getRefCopyPath() + File.separator
090: + filePath);
091:
092: if (!file.exists()) {
093: throw new Exception("File does not exist ! (" + filePath
094: + ")");
095: }
096:
097: Command cmd1;
098: Command cmd2 = null;
099:
100: //
101: long ticket = ws.getNs() + 1;
102: cmd1 = new Remove(filePath, ws);
103: cmd1.setTicket(ticket);
104: ticket++;
105:
106: if (fileType.toUpperCase().equals("BIN")) {
107: cmd2 = new AddBinaryFile(filePath, ws);
108: cmd2.setTicket(ticket);
109: }
110:
111: if (fileType.toUpperCase().equals("TXT")) {
112: cmd2 = new AddTxtFile(filePath, ws);
113: cmd2.setTicket(ticket);
114: }
115:
116: if (fileType.toUpperCase().equals("XML")) {
117: cmd2 = new AddXmlFile(filePath, ws);
118: cmd2.setTicket(ticket);
119: }
120:
121: // send patch
122: File dir = FileUtils.createTmpDir();
123: File f = File.createTempFile("changeTypePatch", null, dir);
124: OutputStreamWriter osw = new OutputStreamWriter(
125: new BufferedOutputStream(new FileOutputStream(f
126: .getPath())));
127: osw.write("<?xml version=\"1.0\"?>\n");
128: osw.write("<patch>");
129: osw.write("<name>"
130: + Base64.encodeBytes(ws.getWsName().getBytes("UTF-8"))
131: + "</name>");
132: osw.write("<begin>" + (ticket - 1) + "</begin>");
133: osw.write("<end>" + ticket + "</end>");
134: osw.write("<comment>"
135: + Base64.encodeBytes("Change Type".getBytes("UTF-8"))
136: + "</comment>");
137: osw.write("<command>");
138: osw.write("<class>" + cmd1.getClass().getName() + "</class>");
139: cmd1.toXML(osw);
140: osw.write("</command>");
141: osw.write("<command>");
142: osw.write("<class>" + cmd2.getClass().getName() + "</class>");
143: cmd2.toXML(osw);
144: osw.write("</command>");
145: osw.write("</patch>");
146: osw.flush();
147: osw.close();
148:
149: //
150: // FileReader fr = new FileReader(f);
151: // LineNumberReader lnr = new LineNumberReader(fr);
152: // String line = null;
153: // System.out.println();
154: // while ((line = lnr.readLine()) != null)
155: // System.out.println(line);
156: // System.out.println();
157: // fr.close();
158: //
159: ws.getClient().sendPatch(ticket - 1, ticket, f.getPath(), true);
160: System.out.println("Patch sent");
161:
162: //
163: ws.update();
164: System.out.println("Patch applied localy");
165: }
166:
167: /**
168: * The <code>ChangeType</code> class is used to change the type of a
169: * previously syncronized file.
170: * <p>
171: * For exemple, an XML file has been detected as a text file but as it's an
172: * important file, you prefere to synchronize it as a binary file to prevent
173: * inside merge. Then you call this class to do so.
174: * <p>
175: * Some Exemples :
176: * <ul>
177: * <li>java -cp so6.jar ChangeType .so6/1/so6.properties build.xml BIN
178: * </li>
179: * <li>java -cp so6.jar ChangeType .so6/1/so6.properties build.xml XML
180: * </li>
181: * <li>java -cp so6.jar ChangeType .so6/1/so6.properties build.xml TXT
182: * </li>
183: * </ul>
184: *
185: * @param args
186: * <ul>
187: * <li>Path of the WsConnection property file
188: * (.so6/1/so6.properties)</li>
189: * <li>Relative file path that we want to change the type
190: * (src/fr/ecoo/so6/Workspace.java )</li>
191: * <li>The file type (TXT-BIN-XML)</li>
192: * </ul>
193: *
194: */
195: public static void main(String[] args) throws Exception {
196: if (args.length != 3) {
197: System.err.println("Usage: wscPath filePath type");
198: System.err
199: .println(" (1) wscPath: Workspace connection property file path");
200: System.err.println(" (2) filePath: relative file path");
201: System.err.println(" (3) type: BIN TXT XML");
202: } else if (args.length == 3) {
203: ChangeType ct = new ChangeType(args[0], args[1], args[2]);
204: System.exit(0);
205: }
206: }
207: }
|