1 /*
   2  * Copyright (c) 2015, 2016, 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 package com.oracle.tools.packager;
  26 
  27 import java.io.File;
  28 import java.io.FileOutputStream;
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.nio.file.Files;
  32 import java.text.MessageFormat;
  33 import java.util.Collections;
  34 import java.util.List;
  35 import java.util.Map;
  36 import java.util.ResourceBundle;
  37 import java.util.regex.Matcher;
  38 import java.util.regex.Pattern;
  39 
  40 import jdk.packager.internal.legacy.JLinkBundlerHelper;
  41 
  42 
  43 import static com.oracle.tools.packager.StandardBundlerParam.*;
  44 
  45 /**
  46  * Common utility methods used by app image bundlers.
  47  */
  48 public abstract class AbstractImageBundler extends AbstractBundler {
  49 
  50     private static final ResourceBundle I18N =
  51             ResourceBundle.getBundle(AbstractImageBundler.class.getName());
  52 
  53     public void imageBundleValidation(Map<String, ? super Object> p) throws ConfigException {
  54         StandardBundlerParam.validateMainClassInfoFromAppResources(p);
  55 
  56         Map<String, String> userJvmOptions = USER_JVM_OPTIONS.fetchFrom(p);
  57         if (userJvmOptions != null) {
  58             for (Map.Entry<String, String> entry : userJvmOptions.entrySet()) {
  59                 if (entry.getValue() == null || entry.getValue().isEmpty()) {
  60                     throw new ConfigException(
  61                             MessageFormat.format(I18N.getString("error.empty-user-jvm-option-value"), entry.getKey()),
  62                             I18N.getString("error.empty-user-jvm-option-value.advice"));
  63                 }
  64             }
  65         }
  66 
  67         boolean hasMainJar = MAIN_JAR.fetchFrom(p) != null;
  68         boolean hasMainModule = StandardBundlerParam.MODULE.fetchFrom(p) != null;
  69 
  70         if (!hasMainJar && !hasMainModule) {
  71             if (!hasMainJar) {
  72                 throw new ConfigException(
  73                         I18N.getString("error.no-application-jar"),
  74                         I18N.getString("error.no-application-jar.advice"));
  75             }
  76             else {
  77                 throw new ConfigException(
  78                         I18N.getString("error.no-main-module"),
  79                         I18N.getString("error.no-main-module.advice"));
  80             }
  81         }
  82 
  83         if (ENABLE_APP_CDS.fetchFrom(p)) {
  84             if (!UNLOCK_COMMERCIAL_FEATURES.fetchFrom(p)) {
  85                 throw new ConfigException(
  86                         I18N.getString("error.app-cds-no-commercial-unlock"),
  87                         I18N.getString("error.app-cds-no-commercial-unlock.advice"));
  88             }
  89         }
  90     }
  91 
  92     public static void extractFlagsFromVersion(Map<String, ? super Object> params, String versionOutput) {
  93         Pattern bitArchPattern = Pattern.compile("(\\d*)[- ]?[bB]it");
  94         Matcher matcher = bitArchPattern.matcher(versionOutput);
  95         if (matcher.find()) {
  96             params.put(".runtime.bit-arch", matcher.group(1));
  97         } else {
  98             // presume 32 bit on no match
  99             params.put(".runtime.bit-arch", "32");
 100         }
 101 
 102         Pattern oldVersionMatcher = Pattern.compile("java version \"((\\d+.(\\d+).\\d+)(_(\\d+)))?(-(.*))?\"");
 103         matcher = oldVersionMatcher.matcher(versionOutput);
 104         if (matcher.find()) {
 105             params.put(".runtime.version", matcher.group(1));
 106             params.put(".runtime.version.release", matcher.group(2));
 107             params.put(".runtime.version.major", matcher.group(3));
 108             params.put(".runtime.version.update", matcher.group(5));
 109             params.put(".runtime.version.minor", matcher.group(5));
 110             params.put(".runtime.version.security", matcher.group(5));
 111             params.put(".runtime.version.patch", "0");
 112             params.put(".runtime.version.modifiers", matcher.group(7));
 113         } else {
 114             Pattern newVersionMatcher = Pattern.compile("java version \"((\\d+).(\\d+).(\\d+).(\\d+))(-(.*))?(\\+[^\"]*)?\"");
 115             matcher = newVersionMatcher.matcher(versionOutput);
 116             if (matcher.find()) {
 117                 params.put(".runtime.version", matcher.group(1));
 118                 params.put(".runtime.version.release", matcher.group(1));
 119                 params.put(".runtime.version.major", matcher.group(2));
 120                 params.put(".runtime.version.update", matcher.group(3));
 121                 params.put(".runtime.version.minor", matcher.group(3));
 122                 params.put(".runtime.version.security", matcher.group(4));
 123                 params.put(".runtime.version.patch", matcher.group(5));
 124                 params.put(".runtime.version.modifiers", matcher.group(7));
 125             } else {
 126                 params.put(".runtime.version", "");
 127                 params.put(".runtime.version.release", "");
 128                 params.put(".runtime.version.major", "");
 129                 params.put(".runtime.version.update", "");
 130                 params.put(".runtime.version.minor", "");
 131                 params.put(".runtime.version.security", "");
 132                 params.put(".runtime.version.patch", "");
 133                 params.put(".runtime.version.modifiers", "");
 134             }
 135         }
 136     }
 137 }