1 /*
   2  * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 package jdk.jpackage.main;
  26 
  27 import jdk.jpackage.internal.Arguments;
  28 import jdk.jpackage.internal.Log;
  29 import jdk.jpackage.internal.CLIHelp;
  30 import java.io.PrintWriter;
  31 import java.util.ResourceBundle;
  32 import java.io.FileNotFoundException;
  33 import java.io.IOException;
  34 import java.text.MessageFormat;
  35 
  36 public class Main {
  37 
  38     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  39             "jdk.jpackage.internal.resources.MainResources");
  40 
  41     /**
  42      * main(String... args)
  43      * This is the entry point for the jpackage tool.
  44      *
  45      * @param args command line arguments
  46      */
  47     public static void main(String... args) throws Exception {
  48         // Create logger with default system.out and system.err
  49         Log.setLogger(null);
  50 
  51         int status = new jdk.jpackage.main.Main().execute(args);
  52         System.exit(status);
  53     }
  54 
  55     /**
  56      * execute() - this is the entry point for the ToolProvider API.
  57      *
  58      * @param out output stream
  59      * @param err error output stream
  60      * @param args command line arguments
  61      * @return an exit code. 0 means success, non-zero means an error occurred.
  62      */
  63     public int execute(PrintWriter out, PrintWriter err, String... args) {
  64         // Create logger with provided streams
  65         Log.Logger logger = new Log.Logger();
  66         logger.setPrintWriter(out, err);
  67         Log.setLogger(logger);
  68 
  69         return execute(args);
  70     }
  71 
  72     private int execute(String... args) {
  73         try {
  74             String[] newArgs;
  75             try {
  76                 newArgs = CommandLine.parse(args);
  77             } catch (FileNotFoundException fnfe) {
  78                 Log.error(MessageFormat.format(I18N.getString(
  79                         "ERR_CannotParseOptions"), fnfe.getMessage()));
  80                 return 1;
  81             } catch (IOException ioe) {
  82                 Log.error(ioe.getMessage());
  83                 return 1;
  84             }
  85 
  86             if (newArgs.length == 0) {
  87                 CLIHelp.showHelp(true);
  88             } else if (hasHelp(newArgs)){
  89                 if (hasVersion(newArgs)) {
  90                     Log.info(System.getProperty("java.version") + "\n");
  91                 }
  92                 CLIHelp.showHelp(false);
  93             } else if (hasVersion(newArgs)) {
  94                 Log.info(System.getProperty("java.version"));
  95             } else {
  96                 Arguments arguments = new Arguments(newArgs);
  97                 if (!arguments.processArguments()) {
  98                     // processArguments() will log error message if failed.
  99                     return 1;
 100                 }
 101             }
 102             return 0;
 103         } finally {
 104             Log.flush();
 105         }
 106     }
 107 
 108     private boolean hasHelp(String[] args) {
 109         for (String a : args) {
 110             if ("--help".equals(a) || "-h".equals(a)) {
 111                 return true;
 112             }
 113         }
 114         return false;
 115     }
 116 
 117     private boolean hasVersion(String[] args) {
 118         for (String a : args) {
 119             if ("--version".equals(a)) {
 120                 return true;
 121             }
 122         }
 123         return false;
 124     }
 125 
 126 }