View Javadoc
1   /**
2    * This file Copyright (c) 2011-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.groovy.remote;
35  
36  import groovyjarjarcommonscli.BasicParser;
37  import groovyjarjarcommonscli.CommandLine;
38  import groovyjarjarcommonscli.CommandLineParser;
39  import groovyjarjarcommonscli.HelpFormatter;
40  import groovyjarjarcommonscli.Options;
41  import groovyjarjarcommonscli.ParseException;
42  
43  import java.io.File;
44  import java.util.ArrayList;
45  
46  /**
47   * Main class Executing the RemoteClientConsole.
48   * This Main will call RemoteClientConsole in order to open an HTTP connection
49   * with the remote Magnolia server.
50   * Once the connection is open and authorized Groovy script files passed as argument
51   * are executed against the internal Magnolia server Groovy environment.
52   */
53  public class RemoteClientConsoleMain {
54  
55      /**
56       * Main class.
57       * 
58       * @throws ParseException: in case of CommandLine Parsing Exception.
59       */
60      public static void main(String[] args) throws ParseException {
61          // Init global
62          String user = null;
63          String pass = null;
64          String uri = null;
65          ArrayList<Object> inputArgs = new ArrayList<Object>();
66  
67          // Init the parser
68          CommandLineParser parser = new BasicParser();
69          // Init Options
70          Options options = initCommandLineOptions();
71          // Parse the program arguments
72          CommandLine commandLine = parser.parse(options, args);
73          // Set the appropriate variables based on supplied options
74          /* Check input arguments */
75          if (commandLine.hasOption("help")) {
76              printHelp(options);
77              System.exit(0);
78          }
79          if (commandLine.hasOption("user")) {
80              user = commandLine.getOptionValue("user");
81          } else {
82              printHelp(options);
83              System.exit(0);
84          }
85          if (commandLine.hasOption("pass")) {
86              pass = commandLine.getOptionValue("pass");
87          } else {
88              printHelp(options);
89              System.exit(0);
90          }
91          if (commandLine.hasOption("uri")) {
92              uri = commandLine.getOptionValue("uri");
93          }
94          else {
95              printHelp(options);
96              System.exit(0);
97          }
98          if (!commandLine.hasOption("file") && !commandLine.hasOption("cmd")) {
99              printHelp(options);
100             System.exit(0);
101         }
102         if (commandLine.hasOption("file")) {
103             String[] stringFiles = commandLine.getOptionValues("file");
104             for (String s : stringFiles) {
105                 File f = new File(s);
106                 if (!f.exists()) {
107                     System.out.println("File don't exist " + f.getAbsolutePath());
108                     continue;
109                 }
110                 inputArgs.add(f);
111             }
112         }
113         if (commandLine.hasOption("cmd")) {
114             String[] stringCmds = commandLine.getOptionValues("cmd");
115             for (String s : stringCmds) {
116                 inputArgs.add(s);
117             }
118         }
119         /* init groovyClient */
120         RemoteClientConsole groovyClient = new RemoteClientConsole(uri, user, pass);
121         if (groovyClient.isConnected()) {
122             for (Object o : inputArgs) {
123                 System.out.println("-----------------------------------------------------------");
124                 System.out.println("will run the following groovy script: " + ((o instanceof File) ? ((File) o).getAbsolutePath() : o));
125                 System.out.println("" + groovyClient.execute(o));
126             }
127 
128             groovyClient.disconnect();
129         } else {
130             System.out.println("Unable to connect");
131         }
132 
133     }
134 
135     /**
136      * Print usage.
137      */
138     private static void printHelp(Options options) {
139         HelpFormatter formatter = new HelpFormatter();
140         formatter.printHelp("RemoteClientConsoleMain", options, true);
141     }
142 
143     /**
144      * Initialize CommandLineOprions.
145      */
146     private static Options initCommandLineOptions() {
147         // init
148         Options options = new Options();
149         options.addOption("help", "help", false, "Print this usage information");
150         options.addOption("user", "user", true, "Enter user");
151         options.addOption("pass", "password", true, "Enter password");
152         options.addOption("uri", "uri", true, "Enter Magnolia Uri like http://localhost:8080/contextpath");
153         options.addOption("file", "file", true, "Groovy File to execute. Can have one to n file arguments");
154         options.addOption("cmd", "cmd", true, "Groovy command line to be executed. Can have one to n cmd arguments");
155         return options;
156     }
157 }