1 /*
   2  * Copyright (c) 2015, 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.packager.internal;
  27 
  28 import java.text.MessageFormat;
  29 import java.util.Map;
  30 import java.util.ResourceBundle;
  31 import java.util.regex.Matcher;
  32 import java.util.regex.Pattern;
  33 
  34 import static jdk.packager.internal.StandardBundlerParam.*;
  35 
  36 /**
  37  * AbstractImageBundler
  38  *
  39  * This is the base class for each of the Application Image Bundlers.
  40  *
  41  * It contains methods and parameters common to all Image Bundlers.
  42  *
  43  * Application Image Bundlers are created in "create-image" mode,
  44  * or as an intermeadiate step in "create-installer" mode.
  45  *
  46  * The concrete implementations are in the platform specific Bundlers.
  47  */
  48 public abstract class AbstractImageBundler extends AbstractBundler {
  49 
  50     private final static String JAVA_VERSION_SPEC =
  51         "java version \"((\\d+).(\\d+).(\\d+).(\\d+))(-(.*))?(\\+[^\"]*)?\"";
  52 
  53     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  54             "jdk.packager.internal.resources.AbstractImageBundler");
  55 
  56     public void imageBundleValidation(Map<String, ? super Object> p)
  57              throws ConfigException {
  58         StandardBundlerParam.validateMainClassInfoFromAppResources(p);
  59 
  60         boolean hasMainJar = MAIN_JAR.fetchFrom(p) != null;
  61         boolean hasMainModule =
  62                 StandardBundlerParam.MODULE.fetchFrom(p) != null;
  63         boolean hasMainClass = MAIN_CLASS.fetchFrom(p) != null;
  64         boolean jreInstaller = Arguments.CREATE_JRE_INSTALLER.fetchFrom(p);
  65 
  66         if (!hasMainJar && !hasMainModule && !hasMainClass && !jreInstaller) {
  67             throw new ConfigException(
  68                     I18N.getString("error.no-application-class"),
  69                     I18N.getString("error.no-application-class.advice"));
  70         }
  71     }
  72 
  73     public static void extractFlagsFromVersion(
  74             Map<String, ? super Object> params, String versionOutput) {
  75         Pattern bitArchPattern = Pattern.compile("(\\d*)[- ]?[bB]it");
  76         Matcher matcher = bitArchPattern.matcher(versionOutput);
  77         if (matcher.find()) {
  78             params.put(".runtime.bit-arch", matcher.group(1));
  79         } else {
  80             // presume 32 bit on no match
  81             params.put(".runtime.bit-arch", "32");
  82         }
  83 
  84         Pattern oldVersionMatcher = Pattern.compile(
  85                 "java version \"((\\d+.(\\d+).\\d+)(_(\\d+)))?(-(.*))?\"");
  86         matcher = oldVersionMatcher.matcher(versionOutput);
  87         if (matcher.find()) {
  88             params.put(".runtime.version", matcher.group(1));
  89             params.put(".runtime.version.release", matcher.group(2));
  90             params.put(".runtime.version.major", matcher.group(3));
  91             params.put(".runtime.version.update", matcher.group(5));
  92             params.put(".runtime.version.minor", matcher.group(5));
  93             params.put(".runtime.version.security", matcher.group(5));
  94             params.put(".runtime.version.patch", "0");
  95             params.put(".runtime.version.modifiers", matcher.group(7));
  96         } else {
  97             Pattern newVersionMatcher = Pattern.compile(JAVA_VERSION_SPEC);
  98             matcher = newVersionMatcher.matcher(versionOutput);
  99             if (matcher.find()) {
 100                 params.put(".runtime.version", matcher.group(1));
 101                 params.put(".runtime.version.release", matcher.group(1));
 102                 params.put(".runtime.version.major", matcher.group(2));
 103                 params.put(".runtime.version.update", matcher.group(3));
 104                 params.put(".runtime.version.minor", matcher.group(3));
 105                 params.put(".runtime.version.security", matcher.group(4));
 106                 params.put(".runtime.version.patch", matcher.group(5));
 107                 params.put(".runtime.version.modifiers", matcher.group(7));
 108             } else {
 109                 params.put(".runtime.version", "");
 110                 params.put(".runtime.version.release", "");
 111                 params.put(".runtime.version.major", "");
 112                 params.put(".runtime.version.update", "");
 113                 params.put(".runtime.version.minor", "");
 114                 params.put(".runtime.version.security", "");
 115                 params.put(".runtime.version.patch", "");
 116                 params.put(".runtime.version.modifiers", "");
 117             }
 118         }
 119     }
 120 }