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