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         Log.info(I18N.getString("warning.experimental"));
  74         try {
  75             String[] newArgs;
  76             try {
  77                 newArgs = CommandLine.parse(args);
  78             } catch (FileNotFoundException fnfe) {
  79                 Log.error(MessageFormat.format(I18N.getString(
  80                         "ERR_CannotParseOptions"), fnfe.getMessage()));
  81                 return 1;
  82             } catch (IOException ioe) {
  83                 Log.error(ioe.getMessage());
  84                 return 1;
  85             }
  86 
  87             if (newArgs.length == 0) {
  88                 CLIHelp.showHelp(true);
  89             } else if (hasHelp(newArgs)){
  90                 if (hasVersion(newArgs)) {
  91                     Log.info(System.getProperty("java.version") + "\n");
  92                 }
  93                 CLIHelp.showHelp(false);
  94             } else if (hasVersion(newArgs)) {
  95                 Log.info(System.getProperty("java.version"));
  96             } else {
  97                 Arguments arguments = new Arguments(newArgs);
  98                 if (!arguments.processArguments()) {
  99                     // processArguments() will log error message if failed.
 100                     return 1;
 101                 }
 102             }
 103             return 0;
 104         } finally {
 105             Log.flush();
 106         }
 107     }
 108 
 109     private boolean hasHelp(String[] args) {
 110         for (String a : args) {
 111             if ("--help".equals(a) || "-h".equals(a)) {
 112                 return true;
 113             }
 114         }
 115         return false;
 116     }
 117 
 118     private boolean hasVersion(String[] args) {
 119         for (String a : args) {
 120             if ("--version".equals(a)) {
 121                 return true;
 122             }
 123         }
 124         return false;
 125     }
 126 
 127 }