1 package org.apache.maven.scm.provider.synergy.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmVersion;
24 import org.codehaus.plexus.util.StringUtils;
25 import org.codehaus.plexus.util.cli.CommandLineUtils;
26 import org.codehaus.plexus.util.cli.Commandline;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.Calendar;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Properties;
34
35
36
37
38
39
40
41 public class SynergyCCM
42 {
43
44 private static final String CCM = "ccm";
45
46 private static final String BASELINE = "baseline";
47
48 private static final String CI = "ci";
49
50 private static final String CO = "co";
51
52 private static final String CREATE = "create";
53
54 private static final String DELETE = "delete";
55
56 private static final String DELIMITER = "delimiter";
57
58 private static final String DIR = "dir";
59
60 private static final String QUERY = "query";
61
62 private static final String RECONCILE = "rwa";
63
64 private static final String RECONFIGURE = "reconfigure";
65
66 private static final String RECONFIGURE_PROPERTIES = "reconfigure_properties";
67
68 private static final String START = "start";
69
70 private static final String STOP = "stop";
71
72 private static final String SYNC = "sync";
73
74 private static final String TASK = "task";
75
76 private static final String WA = "wa";
77
78
79
80
81
82
83
84
85
86
87 public static Commandline showTaskObjects( int taskNumber, String format, String ccmAddr )
88 throws ScmException
89 {
90
91 Commandline cl = new Commandline();
92
93 configureEnvironment( cl, ccmAddr );
94
95 cl.setExecutable( CCM );
96
97 cl.createArg().setValue( TASK );
98 cl.createArg().setValue( "-show" );
99 cl.createArg().setValue( "objects" );
100
101
102 if ( format != null && !format.equals( "" ) )
103 {
104 cl.createArg().setValue( "-f" );
105 cl.createArg().setValue( format );
106 }
107
108 cl.createArg().setValue( Integer.toString( taskNumber ) );
109
110 return cl;
111 }
112
113
114
115
116
117
118
119
120
121
122 public static Commandline query( String query, String format, String ccmAddr )
123 throws ScmException
124 {
125
126
127 Commandline cl = new Commandline();
128
129 configureEnvironment( cl, ccmAddr );
130
131 cl.setExecutable( CCM );
132 cl.createArg().setValue( QUERY );
133
134 cl.createArg().setValue( "-u" );
135
136
137 if ( format != null && !format.equals( "" ) )
138 {
139 cl.createArg().setValue( "-f" );
140 cl.createArg().setValue( format );
141 }
142
143 cl.createArg().setValue( query );
144
145 return cl;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159 public static Commandline createBaseline( String projectSpec, String name, String release, String purpose,
160 String ccmAddr )
161 throws ScmException
162 {
163 Commandline cl = new Commandline();
164
165 configureEnvironment( cl, ccmAddr );
166
167 cl.setExecutable( CCM );
168 cl.createArg().setValue( BASELINE );
169
170 cl.createArg().setValue( "-create" );
171 cl.createArg().setValue( name );
172
173 cl.createArg().setValue( "-p" );
174 cl.createArg().setValue( projectSpec );
175
176 cl.createArg().setValue( "-release" );
177 cl.createArg().setValue( release );
178
179 cl.createArg().setValue( "-purpose" );
180 cl.createArg().setValue( purpose );
181
182 return cl;
183
184 }
185
186
187
188
189
190
191
192
193
194
195 public static Commandline create( List<File> files, String message, String ccmAddr )
196 throws ScmException
197 {
198 Commandline cl = new Commandline();
199
200 configureEnvironment( cl, ccmAddr );
201
202 cl.setExecutable( CCM );
203 cl.createArg().setValue( CREATE );
204
205 if ( message != null && !message.equals( "" ) )
206 {
207
208 cl.createArg().setValue( "-c" );
209
210 cl.createArg().setValue( message );
211
212 }
213
214 for ( File f : files )
215 {
216 try
217 {
218 cl.createArg().setValue( f.getCanonicalPath() );
219 }
220 catch ( IOException e )
221 {
222 throw new ScmException( "Invalid file path " + f.toString(), e );
223 }
224 }
225
226 return cl;
227
228 }
229
230
231
232
233
234
235
236
237
238
239
240 public static Commandline createTask( String synopsis, String release, boolean defaultTask, String ccmAddr )
241 throws ScmException
242 {
243 Commandline cl = new Commandline();
244
245 configureEnvironment( cl, ccmAddr );
246
247 cl.setExecutable( CCM );
248 cl.createArg().setValue( TASK );
249
250 cl.createArg().setValue( "-create" );
251
252 cl.createArg().setValue( "-synopsis" );
253 cl.createArg().setValue( synopsis );
254
255 if ( release != null && !release.equals( "" ) )
256 {
257 cl.createArg().setValue( "-release" );
258 cl.createArg().setValue( release );
259 }
260
261 if ( defaultTask )
262 {
263 cl.createArg().setValue( "-default" );
264 }
265
266 cl.createArg().setValue( "-description" );
267 cl.createArg().setValue(
268 "This task was created by Maven SCM Synergy provider on " + Calendar.getInstance().getTime() );
269
270 return cl;
271
272 }
273
274
275
276
277
278
279
280
281
282
283 public static Commandline checkinTask( String taskSpecs, String comment, String ccmAddr )
284 throws ScmException
285 {
286 Commandline cl = new Commandline();
287
288 configureEnvironment( cl, ccmAddr );
289
290 cl.setExecutable( CCM );
291 cl.createArg().setValue( TASK );
292
293 cl.createArg().setValue( "-checkin" );
294
295 cl.createArg().setValue( taskSpecs );
296
297 cl.createArg().setValue( "-comment" );
298 cl.createArg().setValue( comment );
299
300 return cl;
301
302 }
303
304
305
306
307
308
309
310
311
312
313 public static Commandline delete( List<File> files, String ccmAddr, boolean replace )
314 throws ScmException
315 {
316 Commandline cl = new Commandline();
317
318 configureEnvironment( cl, ccmAddr );
319
320 cl.setExecutable( CCM );
321 cl.createArg().setValue( DELETE );
322
323 if ( replace )
324 {
325 cl.createArg().setValue( "-replace" );
326 }
327
328 for ( File f : files )
329 {
330 try
331 {
332 cl.createArg().setValue( f.getCanonicalPath() );
333 }
334 catch ( IOException e )
335 {
336 throw new ScmException( "Invalid file path " + f.toString(), e );
337 }
338 }
339
340 return cl;
341
342 }
343
344
345
346
347
348
349
350
351
352 public static Commandline reconfigure( String projectSpec, String ccmAddr )
353 throws ScmException
354 {
355 Commandline cl = new Commandline();
356
357 configureEnvironment( cl, ccmAddr );
358
359 cl.setExecutable( CCM );
360 cl.createArg().setValue( RECONFIGURE );
361
362 cl.createArg().setValue( "-recurse" );
363
364 if ( projectSpec != null )
365 {
366 cl.createArg().setValue( "-p" );
367 cl.createArg().setValue( projectSpec );
368 }
369
370 return cl;
371
372 }
373
374
375
376
377
378
379
380
381
382 public static Commandline reconfigureProperties( String projectSpec, String ccmAddr )
383 throws ScmException
384 {
385 Commandline cl = new Commandline();
386
387 configureEnvironment( cl, ccmAddr );
388
389 cl.setExecutable( CCM );
390 cl.createArg().setValue( RECONFIGURE_PROPERTIES );
391
392 cl.createArg().setValue( "-refresh" );
393 cl.createArg().setValue( projectSpec );
394
395 return cl;
396
397 }
398
399
400
401
402
403
404
405
406
407 public static Commandline reconcileUwa( String projectSpec, String ccmAddr )
408 throws ScmException
409 {
410 Commandline cl = new Commandline();
411
412 configureEnvironment( cl, ccmAddr );
413
414 cl.setExecutable( CCM );
415 cl.createArg().setValue( RECONCILE );
416
417 cl.createArg().setValue( "-r" );
418 cl.createArg().setValue( "-uwa" );
419
420 if ( projectSpec != null )
421 {
422 cl.createArg().setValue( "-p" );
423 cl.createArg().setValue( projectSpec );
424 }
425
426 return cl;
427
428 }
429
430
431
432
433
434
435
436
437
438 public static Commandline reconcileUdb( String projectSpec, String ccmAddr )
439 throws ScmException
440 {
441 Commandline cl = new Commandline();
442
443 configureEnvironment( cl, ccmAddr );
444
445 cl.setExecutable( CCM );
446 cl.createArg().setValue( RECONCILE );
447
448 cl.createArg().setValue( "-r" );
449 cl.createArg().setValue( "-udb" );
450
451 if ( projectSpec != null )
452 {
453 cl.createArg().setValue( "-p" );
454 cl.createArg().setValue( projectSpec );
455 }
456
457 return cl;
458
459 }
460
461
462
463
464
465
466
467
468
469
470 public static Commandline dir( File directory, String format, String ccmAddr )
471 throws ScmException
472 {
473 Commandline cl = new Commandline();
474
475 configureEnvironment( cl, ccmAddr );
476
477 try
478 {
479 cl.setWorkingDirectory( directory.getCanonicalPath() );
480 }
481 catch ( IOException e )
482 {
483 throw new ScmException( "Invalid directory", e );
484 }
485
486 cl.setExecutable( CCM );
487 cl.createArg().setValue( DIR );
488 cl.createArg().setValue( "-m" );
489
490
491 if ( format != null && !format.equals( "" ) )
492 {
493 cl.createArg().setValue( "-f" );
494 cl.createArg().setValue( format );
495 }
496
497 return cl;
498
499 }
500
501
502
503
504
505
506
507
508
509 public static Commandline checkoutFiles( List<File> files, String ccmAddr )
510 throws ScmException
511 {
512 Commandline cl = new Commandline();
513
514 configureEnvironment( cl, ccmAddr );
515
516 cl.setExecutable( CCM );
517 cl.createArg().setValue( CO );
518
519 for ( File f : files )
520 {
521 try
522 {
523 cl.createArg().setValue( f.getCanonicalPath() );
524 }
525 catch ( IOException e )
526 {
527 throw new ScmException( "Invalid file path " + f.toString(), e );
528 }
529 }
530
531 return cl;
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545 public static Commandline checkoutProject( File directory, String projectSpec, ScmVersion version, String purpose,
546 String release, String ccmAddr )
547 throws ScmException
548 {
549 Commandline cl = new Commandline();
550
551 configureEnvironment( cl, ccmAddr );
552
553 cl.setExecutable( CCM );
554 cl.createArg().setValue( CO );
555 cl.createArg().setValue( "-subprojects" );
556 cl.createArg().setValue( "-rel" );
557
558 if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
559 {
560 cl.createArg().setValue( "-t" );
561 cl.createArg().setValue( version.getName() );
562 }
563
564 if ( purpose != null && !purpose.equals( "" ) )
565 {
566 cl.createArg().setValue( "-purpose" );
567 cl.createArg().setValue( purpose );
568 }
569
570 if ( release != null && !release.equals( "" ) )
571 {
572 cl.createArg().setValue( "-release" );
573 cl.createArg().setValue( release );
574 }
575
576 if ( directory != null )
577 {
578 cl.createArg().setValue( "-path" );
579 try
580 {
581 cl.createArg().setValue( directory.getCanonicalPath() );
582 }
583 catch ( IOException e )
584 {
585 throw new ScmException( "Invalid directory", e );
586 }
587 }
588 cl.createArg().setValue( "-p" );
589 cl.createArg().setValue( projectSpec );
590
591 return cl;
592 }
593
594
595
596
597
598
599
600
601
602
603 public static Commandline checkinProject( String projectSpec, String comment, String ccmAddr )
604 throws ScmException
605 {
606 Commandline cl = new Commandline();
607
608 configureEnvironment( cl, ccmAddr );
609
610 cl.setExecutable( CCM );
611 cl.createArg().setValue( CI );
612 if ( comment != null && !comment.equals( "" ) )
613 {
614 cl.createArg().setValue( "-c" );
615 cl.createArg().setValue( comment );
616 }
617 cl.createArg().setValue( "-p" );
618 cl.createArg().setValue( projectSpec );
619
620 return cl;
621 }
622
623
624
625
626
627
628
629
630
631
632 public static Commandline checkinFiles( List<File> files, String comment, String ccmAddr )
633 throws ScmException
634 {
635 Commandline cl = new Commandline();
636
637 configureEnvironment( cl, ccmAddr );
638
639 cl.setExecutable( CCM );
640 cl.createArg().setValue( CI );
641 if ( comment != null && !comment.equals( "" ) )
642 {
643 cl.createArg().setValue( "-c" );
644 cl.createArg().setValue( comment );
645 }
646
647 if ( files.size() > 0 )
648 {
649 for ( File f : files )
650 {
651 try
652 {
653 cl.createArg().setValue( f.getCanonicalPath() );
654 }
655 catch ( IOException e )
656 {
657 throw new ScmException( "Invalid file path " + f.toString(), e );
658 }
659 }
660 }
661 return cl;
662 }
663
664
665
666
667
668
669
670
671
672 public static Commandline synchronize( String projectSpec, String ccmAddr )
673 throws ScmException
674 {
675 Commandline cl = new Commandline();
676
677 configureEnvironment( cl, ccmAddr );
678
679 cl.setExecutable( CCM );
680 cl.createArg().setValue( SYNC );
681 cl.createArg().setValue( "-r" );
682 cl.createArg().setValue( "-p" );
683 cl.createArg().setValue( projectSpec );
684
685 return cl;
686 }
687
688
689
690
691
692
693
694
695
696 public static Commandline showWorkArea( String projectSpec, String ccmAddr )
697 throws ScmException
698 {
699 Commandline cl = new Commandline();
700
701 configureEnvironment( cl, ccmAddr );
702
703 cl.setExecutable( CCM );
704 cl.createArg().setValue( WA );
705 cl.createArg().setValue( "-show" );
706 cl.createArg().setValue( projectSpec );
707
708 return cl;
709 }
710
711
712
713
714
715
716
717
718 public static Commandline stop( String ccmAddr )
719 throws ScmException
720 {
721 Commandline cl = new Commandline();
722
723 configureEnvironment( cl, ccmAddr );
724
725 cl.setExecutable( CCM );
726 cl.createArg().setValue( STOP );
727
728 return cl;
729 }
730
731
732
733
734
735
736
737
738 private static void configureEnvironment( Commandline cl, String ccmAddr )
739 throws ScmException
740 {
741
742 try
743 {
744 Properties envVars = CommandLineUtils.getSystemEnvVars();
745
746 for ( @SuppressWarnings( "rawtypes" )
747 Iterator i = envVars.keySet().iterator(); i.hasNext(); )
748 {
749 String key = (String) i.next();
750
751 if ( !key.equalsIgnoreCase( "CCM_ADDR" ) )
752 {
753
754 cl.addEnvironment( key, envVars.getProperty( key ) );
755
756 }
757 }
758 }
759 catch ( Exception e1 )
760 {
761 throw new ScmException( "Fail to add PATH environment variable.", e1 );
762
763 }
764 cl.addEnvironment( "CCM_ADDR", ccmAddr );
765
766 }
767
768
769
770
771
772
773
774
775
776
777 public static Commandline start( String username, String password, SynergyRole role )
778 throws ScmException
779 {
780 Commandline cl = new Commandline();
781
782 cl.setExecutable( CCM );
783 cl.createArg().setValue( START );
784 cl.createArg().setValue( "-nogui" );
785 cl.createArg().setValue( "-m" );
786 cl.createArg().setValue( "-q" );
787 cl.createArg().setValue( "-n" );
788 cl.createArg().setValue( username );
789 cl.createArg().setValue( "-pw" );
790 cl.createArg().setValue( password );
791 if ( role != null )
792 {
793 cl.createArg().setValue( "-r" );
794 cl.createArg().setValue( role.toString() );
795 }
796
797 return cl;
798 }
799
800
801
802
803
804
805
806
807
808
809 public static Commandline startRemote( String username, String password, SynergyRole role )
810 throws ScmException
811 {
812 Commandline cl = new Commandline();
813
814 cl.setExecutable( CCM );
815 cl.createArg().setValue( START );
816 cl.createArg().setValue( "-nogui" );
817 cl.createArg().setValue( "-m" );
818 cl.createArg().setValue( "-q" );
819 cl.createArg().setValue( "-rc" );
820 cl.createArg().setValue( "-n" );
821 cl.createArg().setValue( username );
822 cl.createArg().setValue( "-pw" );
823 cl.createArg().setValue( password );
824 if ( role != null )
825 {
826 cl.createArg().setValue( "-r" );
827 cl.createArg().setValue( role.toString() );
828 }
829
830 return cl;
831 }
832
833
834
835
836
837
838
839 public static Commandline delimiter( String ccmAddr )
840 throws ScmException
841 {
842 Commandline cl = new Commandline();
843
844 configureEnvironment( cl, ccmAddr );
845
846 cl.setExecutable( CCM );
847 cl.createArg().setValue( DELIMITER );
848
849 return cl;
850 }
851
852
853
854
855
856
857
858
859 public static Commandline showDefaultTask( String ccmAddr )
860 throws ScmException
861 {
862 Commandline cl = new Commandline();
863
864 configureEnvironment( cl, ccmAddr );
865 cl.setExecutable( CCM );
866 cl.createArg().setValue( TASK );
867 cl.createArg().setValue( "-default" );
868
869 return cl;
870 }
871
872
873
874
875
876
877
878
879
880 public static Commandline setDefaultTask( int task, String ccmAddr )
881 throws ScmException
882 {
883 Commandline cl = new Commandline();
884
885 configureEnvironment( cl, ccmAddr );
886 cl.setExecutable( CCM );
887 cl.createArg().setValue( TASK );
888 cl.createArg().setValue( "-default" );
889 cl.createArg().setValue( String.valueOf( task ) );
890 return cl;
891 }
892 }