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.Workspace;
036: import org.libresource.so6.core.WsConnection;
037: import org.libresource.so6.core.client.DummyClient;
038: import org.libresource.so6.core.engine.util.FileUtils;
039: import org.libresource.so6.core.exec.ui.ConflictEditor;
040: import org.libresource.so6.core.exec.ui.PatchInspector;
041:
042: import java.io.File;
043:
044: import java.util.ArrayList;
045: import java.util.Properties;
046:
047: /**
048: *
049: * Allow the launch of most so6 applications.
050: * <ul>
051: * <li>Update local workspace</li>
052: * <li>Commit local workspace</li>
053: * <li>CreateWorkspace : create a dummy workspace</li>
054: * <li>CreateQueue : create a dummy queue</li>
055: * <li>Rename a file</li>
056: * <li>FindConflict</li>
057: * <li>ChangeFileType</li>
058: * <li>ComputeLocalTypes</li>
059: * <li>Restore</li>
060: * <li>PartialCommit</li>
061: * <li>AddWsConnection</li>
062: * <li>ConflictEditor : Launch the conflict editor</li>
063: * <li>HistoryViewer : Launch the history viewer</li>
064: * <li>ApplyPatch :</li>
065: * <li>PatchInspector :</li>
066: * <li>AnonymousAccess :</li>
067: * <li>Clean</li>
068: * </ul>
069: *
070: * @author smack
071: */
072: public class Main {
073: private static String connection = "1";
074: private static String wsBasePath = null;
075: private static String wsPath = null;
076: private static String noOp = null;
077: private static final String[] COMMANDS = { "COMMIT", "UPDATE",
078: "CREATEWORKSPACE", "CREATEQUEUE", "RENAME", "FINDCONFLICT",
079: "CHANGETYPE", "COMPUTELOCALTYPES", "RESTORE",
080: "PARTIALCOMMIT", "ADDWSCONNECTION", "CONFLICTEDITOR",
081: "APPLYPATCH", "PATCHINSPECTOR", "SYNCHRONIZE",
082: "ANONYMOUSACCESS", "CLEAN" };
083: private static final int COMMIT = 0;
084: private static final int UPDATE = 1;
085: private static final int CREATE_WORKSPACE = 2;
086: private static final int CREATE_SYNCHRONISER = 3;
087: private static final int RENAME = 4;
088: private static final int FIND_CONFLICT = 5;
089: private static final int CHANGE_TYPE = 6;
090: private static final int COMPUTE_LOCAL_TYPES = 7;
091: private static final int RESTORE = 8;
092: private static final int PARTIAL_COMMIT = 9;
093: private static final int ADD_WS_CONNECTION = 10;
094: private static final int CONFLICT_EDITOR = 11;
095: private static final int HISTORY_VIEWER = 18; //12;
096: private static final int APPLY_PATCH = 12; //13;
097: private static final int PATCH_INSPECTOR = 13; //14;
098: private static final int SYNCHRONIZE = 14; //15;
099: private static final int CHECKOUT = 15; //16;
100: private static final int CLEAN = 16; //17;
101: private static final String HELP = "so6 command line help : \n"
102: + " -h or --help : Get a general help on the so6 command lines\n"
103: + " -w or --wsPath : The workspace base path\n"
104: + " -n or --noOp : Simulate an action\n"
105: + " -c or --connection : The connection name usually (If not specified, the default value will be 1)\n";
106: private static final String HELP_COMMIT = "commit -w workspacepath [-c connection] \"comment\" \n"
107: + " Exemple:\n\n"
108: + " so6 commit -w . \"A little comment for the commit...\"\n"
109: + " so6 commit -w . -c 1 \"A little comment for the commit...\"\n";
110: private static final String HELP_PARTIAL_COMMIT = "partialCommit -w workspacepath [-c connection] \"comment\" filterfile"
111: + " Exemple:\n\n"
112: + " so6 partialCommit -w . -c 1 \"A little comment for the commit...\" /myFilter.txt\n"
113: + " so6 partialCommit -w . \"A little comment for the commit...\" /myFilter.txt\n";
114: private static final String HELP_UPDATE = "update -w workspacepath [-c connection]\n"
115: + " Exemple:\n\n" + " so6 update -w . -c 1\n";
116: private static final String HELP_CREATE_WORKSPACE = "createWorkspace workspacepath queuepath workspacename\n"
117: + " Exemple:\n\n"
118: + " so6 createWorkspace ~/foo ~/queue1 \"seb loria\"\n";
119: private static final String HELP_ADD_WS_CONNECTION = "addWsConnection -w workspacepath queuepath workspacename\n"
120: + " Exemple:\n\n"
121: + " so6 addWsConnection -w ~/foo ~/queue1 \"seb loria\"\n";
122: private static final String HELP_CREATE_SYNCHRONISER = "createQueue queuepath\n"
123: + " Exemple:\n\n" + " so6 createQueue ~/queue1 \n";
124: private static final String HELP_RENAME = "rename -w workspacepath [-c connection] file1 file2\n"
125: + " Exemple:\n\n"
126: + " so6 rename -w . dir1/dir2/a.txt dir1/dir2/b.txt\n"
127: + " so6 rename -w . -c 1 dir1/dir2/a.txt dir1/dir2/b.txt\n";
128: private static final String HELP_FIND_CONFLICT = "findConflict -w workspacepath [-c connection]\n"
129: + " Exemple:\n\n" + " so6 findConflict -w . -c 1\n";
130: private static final String HELP_CHANGE_TYPE = "changeType -w workspacepath [-c connection] filepath <BIN|TXT|XML>\n"
131: + " Exemple:\n\n"
132: + " so6 changeType -w . -c 1 dir1/dir2/b.txt BIN\n";
133: private static final String HELP_COMPUTE_LOCAL_TYPES = "computeLocalTypes -w workspacepath [-c connection]\n"
134: + " Exemple:\n\n"
135: + " so6 computeLocalTypes -w . -c 1\n";
136: private static final String HELP_RESTORE = "restore -w workspacepath [-c connection]\n"
137: + " Exemple:\n\n" + " so6 restore -w . -c 1";
138: private static final String HELP_CONFLICT_EDITOR = "conflictEditor filepath\n"
139: + " Exemple:\n\n"
140: + " so6 conflictEditor /a/b/build.txt"
141: + WsConnection.SO6_WSC_FILE;
142: private static final String HELP_HISTORY_VIEWER = "historyViewer -w workspacepath [-c connection]\n"
143: + " Exemple:\n\n"
144: + "\n"
145: + " so6 historyViewer -w . /a/b/build.txt";
146: private static final String HELP_STUDIO = "studio\n"
147: + " Exemple:\n\n" + "\n" + " so6 studio";
148: private static final String HELP_APPLY_PATCH = "applyPatch dirToPatch patch1 patch2 ...\n"
149: + " Exemple:\n\n"
150: + "\n"
151: + " so6 applyPatch dirToPatch /tmp/patch.xml.1 /tmp/patch.xml.2";
152: private static final String HELP_PATCH_INSPECTOR = "pacthInspector patchPath\n"
153: + " Exemple:\n\n"
154: + "\n"
155: + " so6 patchInspector .so6/1/APPLIED/1.200";
156: private static final String HELP_SYNCHRONIZE = "synchronize queuePath wsPath1 wsPath2 ...\n"
157: + " Exemple:\n\n"
158: + "\n"
159: + " so6 synchronize /usr/dummyQueue1 /home/seb/so6 /home/seb/so6.tmp";
160: private static final String HELP_CHECK_OUT = "anonymousAccess checkoutFilePath (lastTicket)\n"
161: + " Exemple:\n\n"
162: + "\n"
163: + " so6 anonymousAccess /tmp/so6.checkout ";
164: private static final String HELP_CLEAN = "clean (-w . -c 2) \n"
165: + " Exemple:\n\n" + "\n" + " so6 clean (-w .) ";
166:
167: public static boolean askForHelp(String[] param) {
168: for (int i = 0; i < param.length; i++) {
169: if (param[i].equals("-h") || param[i].equals("--help")) {
170: return true;
171: }
172: }
173:
174: return false;
175: }
176:
177: private static Object[] setOption(String[] param) {
178: // System.out.println("INPUT\n");
179: // for (int i = 0; i < param.length; i++) {
180: // System.out.println(param[i]);
181: // }
182: // System.out.println("\nOUTPUT\n");
183: ArrayList result = new ArrayList();
184:
185: for (int i = 1; i < param.length; i++) {
186: if (param[i].equals("-n") || param[i].equals("--noOp")) {
187: if (param.length < (i + 2)) {
188: System.err
189: .println("Option -n / -noOp require a directory path behind");
190: System.exit(-1);
191: }
192:
193: noOp = param[i + 1];
194: i++;
195:
196: continue;
197: }
198:
199: if (param[i].equals("-w") || param[i].equals("--wsPath")) {
200: if (param.length < (i + 2)) {
201: System.err
202: .println("Option -w / -wsPath require a workspace base path behind");
203: System.exit(-1);
204: }
205:
206: wsBasePath = param[i + 1];
207: i++;
208:
209: continue;
210: }
211:
212: if (param[i].equals("-c")
213: || param[i].equals("--connection")) {
214: if (param.length < (i + 2)) {
215: System.err
216: .println("Option -c / -connection require a number behind (by default it's 1)");
217: System.exit(-1);
218: }
219:
220: connection = param[i + 1];
221: i++;
222:
223: continue;
224: }
225:
226: // Standard param
227: result.add(param[i]);
228: }
229:
230: // System.out.println("wsPath: " + wsPath);
231: // System.out.println("noOp: " + noOp);
232: // System.out.println("Params: ");
233: // for (int i = 0; i < result.size(); i++) {
234: // System.out.print(result.get(i) + " ");
235: // }
236: // System.out.println();
237: if (wsBasePath != null) {
238: wsPath = wsBasePath + File.separator + Workspace.SO6PREFIX
239: + File.separator + connection + File.separator
240: + WsConnection.SO6_WSC_FILE;
241: }
242:
243: return result.toArray();
244: }
245:
246: private static int getAction(String[] param) {
247: if (param.length == 0) {
248: return -1;
249: }
250:
251: for (int i = 0; i < COMMANDS.length; i++) {
252: if (COMMANDS[i].equals(param[0].toUpperCase())) {
253: return i;
254: }
255: }
256:
257: return -1;
258: }
259:
260: /**
261: * Allow the launch of most so6 applications.
262: * <ul>
263: * <li>Update local workspace</li>
264: * <li>Commit local workspace</li>
265: * <li>CreateWorkspace : create a dummy workspace</li>
266: * <li>CreateQueue : create a dummy queue</li>
267: * <li>Rename a file</li>
268: * <li>FindConflict</li>
269: * <li>ChangeFileType</li>
270: * <li>ComputeLocalTypes</li>
271: * <li>Restore</li>
272: * <li>PartialCommit</li>
273: * <li>AddWsConnection</li>
274: * <li>ConflictEditor : Launch the conflict editor</li>
275: * <li>HistoryViewer : Launch the history viewer</li>
276: * <li>ApplyPatch :</li>
277: * <li>PatchInspector</li>
278: * <li>AnonymousAccess</li>
279: * <li>Clean</li>
280: * </ul>
281: *
282: * @param args
283: * @throws Exception
284: */
285: public static void main(String[] args) throws Exception {
286: int action = getAction(args);
287:
288: if (askForHelp(args) || (action == -1)) {
289: if (action == -1) {
290: // Global help
291: System.out.println(HELP);
292: System.out
293: .println("Type so6 <ACTION> --help for detailed help");
294:
295: for (int i = 0; i < COMMANDS.length; i++) {
296: System.out.println(" * "
297: + COMMANDS[i].toLowerCase());
298: }
299: } else {
300: // Local help
301: System.err.println(HELP);
302:
303: switch (action) {
304: case COMMIT:
305: System.err.println(HELP_COMMIT);
306:
307: break;
308:
309: case PARTIAL_COMMIT:
310: System.err.println(PARTIAL_COMMIT);
311:
312: break;
313:
314: case CHANGE_TYPE:
315: System.err.println(HELP_CHANGE_TYPE);
316:
317: break;
318:
319: case COMPUTE_LOCAL_TYPES:
320: System.err.println(HELP_COMPUTE_LOCAL_TYPES);
321:
322: break;
323:
324: case CREATE_WORKSPACE:
325: System.err.println(HELP_CREATE_WORKSPACE);
326:
327: break;
328:
329: case ADD_WS_CONNECTION:
330: System.err.println(HELP_ADD_WS_CONNECTION);
331:
332: break;
333:
334: case CREATE_SYNCHRONISER:
335: System.err.println(HELP_CREATE_SYNCHRONISER);
336:
337: break;
338:
339: case FIND_CONFLICT:
340: System.err.println(HELP_FIND_CONFLICT);
341:
342: break;
343:
344: case RENAME:
345: System.err.println(HELP_RENAME);
346:
347: break;
348:
349: case UPDATE:
350: System.err.println(HELP_UPDATE);
351:
352: break;
353:
354: case RESTORE:
355: System.err.println(HELP_RESTORE);
356:
357: break;
358:
359: case CONFLICT_EDITOR:
360: System.err.println(HELP_CONFLICT_EDITOR);
361:
362: break;
363:
364: case HISTORY_VIEWER:
365: System.err.println(HELP_HISTORY_VIEWER);
366:
367: break;
368:
369: case APPLY_PATCH:
370: System.err.println(HELP_APPLY_PATCH);
371:
372: break;
373:
374: case PATCH_INSPECTOR:
375: System.err.println(HELP_PATCH_INSPECTOR);
376:
377: break;
378:
379: case SYNCHRONIZE:
380: System.err.println(HELP_SYNCHRONIZE);
381:
382: break;
383:
384: case CHECKOUT:
385: System.err.println(HELP_CHECK_OUT);
386:
387: break;
388:
389: case CLEAN:
390: System.err.println(HELP_CLEAN);
391:
392: break;
393: }
394: }
395: } else {
396: // init global options
397: Object[] generalParam = setOption(args);
398:
399: // Do the job...
400: switch (action) {
401: case COMMIT:
402:
403: if ((generalParam.length == 1) && (wsPath != null)) {
404: Commit commit = new Commit(wsPath,
405: (String) generalParam[0]);
406:
407: if (noOp != null) {
408: commit.simulate(noOp);
409: } else {
410: commit.execute();
411: }
412: } else {
413: System.err.println(HELP);
414: System.err.println(HELP_COMMIT);
415: }
416:
417: break;
418:
419: case PARTIAL_COMMIT:
420:
421: if ((generalParam.length == 2) && (wsPath != null)) {
422: PartialCommit pcommit = new PartialCommit(wsPath,
423: (String) generalParam[0],
424: (String) generalParam[1]);
425:
426: if (noOp != null) {
427: pcommit.simulate(noOp);
428: } else {
429: pcommit.execute();
430: }
431: } else {
432: System.err.println(HELP);
433: System.err.println(HELP_PARTIAL_COMMIT);
434: }
435:
436: break;
437:
438: case UPDATE:
439:
440: if ((generalParam.length == 0) && (wsPath != null)) {
441: Update update = new Update(wsPath);
442:
443: if (noOp != null) {
444: update.simulate(noOp);
445: } else {
446: update.execute();
447: }
448: } else {
449: System.err.println(HELP);
450: System.err.println(HELP_UPDATE);
451: }
452:
453: break;
454:
455: case CHANGE_TYPE:
456:
457: if ((generalParam.length == 2) && (wsPath != null)) {
458: ChangeType.main(new String[] { wsPath,
459: (String) generalParam[0],
460: (String) generalParam[1] });
461: } else {
462: System.err.println(HELP);
463: System.err.println(HELP_CHANGE_TYPE);
464: }
465:
466: break;
467:
468: case COMPUTE_LOCAL_TYPES:
469:
470: if ((generalParam.length == 0) && (wsPath != null)) {
471: UpdateLocalDBType.main(new String[] { wsPath });
472: } else {
473: System.err.println(HELP);
474: System.err.println(HELP_COMPUTE_LOCAL_TYPES);
475: }
476:
477: break;
478:
479: case CREATE_WORKSPACE:
480:
481: if (generalParam.length == 3) {
482: new CreateDummyWorkspace((String) generalParam[0],
483: (String) generalParam[1],
484: (String) generalParam[2]).execute();
485: } else {
486: System.err.println(HELP);
487: System.err.println(HELP_CREATE_WORKSPACE);
488: }
489:
490: break;
491:
492: case ADD_WS_CONNECTION:
493:
494: if (generalParam.length == 3) {
495: Workspace ws = new Workspace(
496: (String) generalParam[0]);
497: Properties props = new Properties();
498: props.setProperty(DummyClient.SO6_QUEUE_ID,
499: (String) generalParam[1]);
500: ws.createConnection(props, DummyClient.class
501: .getName(), (String) generalParam[2]);
502: } else {
503: System.err.println(HELP);
504: System.err.println(HELP_ADD_WS_CONNECTION);
505: }
506:
507: break;
508:
509: case CREATE_SYNCHRONISER:
510:
511: if (generalParam.length == 1) {
512: CreateDummySynchroniser
513: .main(new String[] { (String) generalParam[0] });
514: } else {
515: System.err.println(HELP);
516: System.err.println(HELP_CREATE_SYNCHRONISER);
517: }
518:
519: break;
520:
521: case FIND_CONFLICT:
522:
523: if ((generalParam.length == 0) && (wsPath != null)) {
524: FindConflict.main(new String[] { wsPath });
525: } else {
526: System.err.println(HELP);
527: System.err.println(HELP_FIND_CONFLICT);
528: }
529:
530: break;
531:
532: case RENAME:
533:
534: if ((generalParam.length == 2) && (wsPath != null)) {
535: Rename.main(new String[] { wsPath,
536: (String) generalParam[0],
537: (String) generalParam[1] });
538: } else {
539: System.err.println(HELP);
540: System.err.println(HELP_RENAME);
541: }
542:
543: break;
544:
545: case RESTORE:
546:
547: if ((generalParam.length == 0) && (wsPath != null)) {
548: Restore.main(new String[] { wsPath });
549: } else {
550: System.err.println(HELP);
551: System.err.println(HELP_RESTORE);
552: }
553:
554: break;
555:
556: case CONFLICT_EDITOR:
557:
558: if (generalParam.length == 1) {
559: ConflictEditor
560: .main(new String[] { (String) generalParam[0] });
561: } else {
562: System.err.println(HELP);
563: System.err.println(HELP_CONFLICT_EDITOR);
564: }
565:
566: break;
567:
568: case APPLY_PATCH:
569:
570: if ((generalParam.length > 1) && (wsPath == null)) {
571: String[] param = new String[generalParam.length];
572:
573: for (int i = 0; i < param.length; i++) {
574: param[i] = (String) generalParam[i];
575: }
576:
577: ApplyPatch.main(param);
578: } else {
579: System.err.println(HELP);
580: System.err.println(HELP_APPLY_PATCH);
581: }
582:
583: break;
584:
585: case PATCH_INSPECTOR:
586:
587: if ((generalParam.length == 1) && (wsPath == null)) {
588: PatchInspector
589: .main(new String[] { (String) generalParam[0] });
590: } else {
591: System.err.println(HELP);
592: System.err.println(HELP_PATCH_INSPECTOR);
593: }
594:
595: break;
596:
597: case SYNCHRONIZE:
598:
599: if ((generalParam.length > 1) && (wsPath == null)) {
600: String[] param = new String[generalParam.length];
601:
602: for (int i = 0; i < param.length; i++) {
603: param[i] = (String) generalParam[i];
604: }
605:
606: org.libresource.so6.core.tool.Main.main(param);
607: } else {
608: System.err.println(HELP);
609: System.err.println(HELP_SYNCHRONIZE);
610: }
611:
612: break;
613:
614: case CHECKOUT:
615:
616: if ((generalParam.length > 1) && (wsPath == null)) {
617: String[] param = new String[generalParam.length];
618:
619: for (int i = 0; i < param.length; i++) {
620: param[i] = (String) generalParam[i];
621: }
622:
623: AnonymousAccess.main(param);
624: } else {
625: System.err.println(HELP);
626: System.err.println(HELP_CHECK_OUT);
627: }
628:
629: break;
630:
631: case CLEAN:
632: FileUtils.cleanSo6TmpFiles();
633:
634: if (wsPath != null) {
635: WsConnection wsc = new WsConnection(wsPath);
636: wsc.removedOldMergedOp();
637: }
638:
639: break;
640: }
641: }
642: }
643: }
|