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.engine.log.LogUtils;
038: import org.libresource.so6.core.engine.util.Base64;
039: import org.libresource.so6.core.engine.util.FileUtils;
040:
041: import java.io.BufferedOutputStream;
042: import java.io.File;
043: import java.io.FileOutputStream;
044: import java.io.OutputStreamWriter;
045:
046: import java.util.logging.Logger;
047:
048: /**
049: * The <code>Rename</code> class is used to rename a previously syncronized
050: * file.
051: * <p>
052: * Exemples :<br>
053: * java -cp so6.jar Rename .so6/1/so6.properties src/Workspace.java
054: * src/org/libresource/so6/Workspace.java
055: *
056: * @author Smack
057: * @version 1.0, 26/05/04
058: * @see org.libresource.so6.core.exec.Main
059: * @since JDK1.4
060: */
061: public class Rename {
062: /**
063: * Make the rename:
064: * <ul>
065: * <li>wscPath : Workspace connection property file path
066: * (.so6/1/so6.properties)</li>
067: * <li>srcRelativePath : path of the file to move</li>
068: * <li>destRelativePath : destination path of the file to move</li>
069: * </ul>
070: *
071: * @param wscPath :
072: * Workspace connection property file path
073: * (.so6/1/so6.properties)
074: * @param from :
075: * path of the file to move
076: * @param to :
077: * destination path of the file to move
078: * @throws Exception
079: */
080: public Rename(String wscPath, String from, String to)
081: throws Exception {
082: LogUtils.removeAllHandlers(Logger.getLogger("ui.log"));
083: LogUtils.removeAllHandlers(StateMonitoring.getInstance()
084: .getXMLMonitoringLogger());
085:
086: WsConnection ws = new WsConnection(wscPath);
087: File src = new File(ws.getRefCopyPath() + File.separator + from);
088: File dst = new File(ws.getRefCopyPath() + File.separator + to);
089:
090: if (!src.exists()) {
091: throw new Exception("Source file does not exsit ! (" + from
092: + ")");
093: }
094:
095: if (dst.exists()) {
096: throw new Exception("Destination file already exsit ! ("
097: + to + ")");
098: }
099:
100: //
101: long ticket = ws.getNs() + 1;
102: org.libresource.so6.core.command.fs.Rename cmd = new org.libresource.so6.core.command.fs.Rename(
103: from, ws, to);
104: cmd.setTicket(ticket);
105:
106: // send patch
107: File dir = FileUtils.createTmpDir();
108: File f = File.createTempFile("renamePatch", null, dir);
109: OutputStreamWriter osw = new OutputStreamWriter(
110: new BufferedOutputStream(new FileOutputStream(f
111: .getPath())));
112: osw.write("<?xml version=\"1.0\"?>\n");
113: osw.write("<patch>");
114: osw.write("<name>"
115: + Base64.encodeBytes(ws.getWsName().getBytes("UTF-8"))
116: + "</name>");
117: osw.write("<begin>" + ticket + "</begin>");
118: osw.write("<end>" + ticket + "</end>");
119: osw.write("<comment>"
120: + Base64.encodeBytes("Rename".getBytes("UTF-8"))
121: + "</comment>");
122: osw.write("<command>");
123: osw.write("<class>" + cmd.getClass().getName() + "</class>");
124: cmd.toXML(osw);
125: osw.write("</command>");
126: osw.write("</patch>");
127: osw.flush();
128: osw.close();
129:
130: //
131: /*
132: * FileReader fr = new FileReader(f); LineNumberReader lnr = new
133: * LineNumberReader(fr); String line = null; System.out.println(); while
134: * ((line = lnr.readLine()) != null) System.out.println(line);
135: * System.out.println(); fr.close();
136: */
137: //
138: ws.getClient().sendPatch(ticket, ticket, f.getPath(), true);
139: System.out.println("Patch sent");
140:
141: //
142: ws.update();
143: System.out.println("Patch applied locally");
144: }
145:
146: /**
147: * Make the rename and exit !
148: *
149: * @param args
150: * <ul>
151: * <li>wscPath : Workspace connection property file path
152: * (.so6/1/so6.properties)</li>
153: * <li>srcRelativePath : path of the file to move</li>
154: * <li>destRelativePath : destination path of the file to move
155: * </li>
156: * </ul>
157: * @throws Exception
158: */
159: public static void main(String[] args) throws Exception {
160: if (args.length != 3) {
161: System.err
162: .println("Usage: wscPath srcRelativePath destRelativePath");
163: System.err.println(" (1) wscPath: Workspace path");
164: System.err
165: .println(" (2) srcRelativePath: path of the file to move");
166: System.err
167: .println(" (3) destRelativePath: destination path of the file to move");
168: } else if (args.length == 3) {
169: Rename mv = new Rename(args[0], args[1], args[2]);
170: System.exit(0);
171: }
172: }
173: }
|