1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.groovy.remote;
35
36
37 import java.io.File;
38 import java.util.ArrayList;
39
40 import groovyjarjarcommonscli.BasicParser;
41 import groovyjarjarcommonscli.CommandLine;
42 import groovyjarjarcommonscli.CommandLineParser;
43 import groovyjarjarcommonscli.HelpFormatter;
44 import groovyjarjarcommonscli.Options;
45 import groovyjarjarcommonscli.ParseException;
46
47
48
49
50
51
52
53
54
55
56
57 public class RemoteClientConsoleMain {
58
59
60
61
62
63
64 public static void main(String[] args) throws ParseException {
65
66 String user = null;
67 String pass = null;
68 String uri = null;
69 ArrayList<Object> inputArgs = new ArrayList<Object>();
70
71
72 CommandLineParser parser = new BasicParser( );
73
74 Options options = initCommandLineOptions();
75
76 CommandLine commandLine = parser.parse( options, args );
77
78
79 if( commandLine.hasOption("help") ) {
80 printHelp(options);
81 System.exit(0);
82 }
83 if( commandLine.hasOption("user") ) {
84 user = commandLine.getOptionValue("user");
85 }else{
86 printHelp(options);
87 System.exit(0);
88 }
89 if( commandLine.hasOption("pass") ) {
90 pass = commandLine.getOptionValue("pass");
91 }else{
92 printHelp(options);
93 System.exit(0);
94 }
95 if( commandLine.hasOption("uri") ) {
96 uri = commandLine.getOptionValue("uri");
97 }
98 else{
99 printHelp(options);
100 System.exit(0);
101 }
102 if( !commandLine.hasOption("file") && !commandLine.hasOption("cmd") ) {
103 printHelp(options);
104 System.exit(0);
105 }
106 if( commandLine.hasOption("file") ) {
107 String[] stringFiles = commandLine.getOptionValues("file");
108 for(String s:stringFiles) {
109 File f = new File(s);
110 if (!f.exists()) {
111 System.out.println("File don't exist " + f.getAbsolutePath());
112 continue;
113 }
114 inputArgs.add(f);
115 }
116 }
117 if( commandLine.hasOption("cmd") ) {
118 String[] stringCmds = commandLine.getOptionValues("cmd");
119 for (String s:stringCmds) {
120 inputArgs.add(s);
121 }
122 }
123
124 RemoteClientConsole groovyClient = new RemoteClientConsole(uri, user, pass);
125 if(groovyClient.isConnected()) {
126 for(Object o:inputArgs) {
127 System.out.println("-----------------------------------------------------------");
128 System.out.println("will run the following groovy script: " + ((o instanceof File) ? ((File) o).getAbsolutePath() : o));
129 System.out.println(""+groovyClient.execute(o));
130 }
131
132 groovyClient.disconnect();
133 }else{
134 System.out.println("Unable to connect");
135 }
136
137 }
138
139
140
141
142 private static void printHelp( Options options){
143 HelpFormatter formatter = new HelpFormatter();
144 formatter.printHelp( "RemoteClientConsoleMain", options ,true);
145 }
146
147
148
149
150 private static Options initCommandLineOptions(){
151
152 Options options = new Options( );
153 options.addOption("help", "help", false, "Print this usage information");
154 options.addOption("user", "user", true, "Enter user" );
155 options.addOption("pass", "password", true, "Enter password" );
156 options.addOption("uri", "uri", true, "Enter Magnolia Uri like http://localhost:8080/contextpath" );
157 options.addOption("file", "file", true, "Groovy File to execute. Can have one to n file arguments");
158 options.addOption("cmd", "cmd", true, "Groovy command line to be executed. Can have one to n cmd arguments");
159 return options;
160 }
161 }