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  * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
  49  */
  50 @Deprecated(since="10", forRemoval=true)
  51 public abstract class AbstractImageBundler extends AbstractBundler {
  52 
  53     private static final ResourceBundle I18N =
  54             ResourceBundle.getBundle(AbstractImageBundler.class.getName());
  55 
  56     public void imageBundleValidation(Map<String, ? super Object> p) throws ConfigException {
  57         StandardBundlerParam.validateMainClassInfoFromAppResources(p);
  58 
  59         Map<String, String> userJvmOptions = USER_JVM_OPTIONS.fetchFrom(p);
  60         if (userJvmOptions != null) {
  61             for (Map.Entry<String, String> entry : userJvmOptions.entrySet()) {
  62                 if (entry.getValue() == null || entry.getValue().isEmpty()) {
  63                     throw new ConfigException(
  64                             MessageFormat.format(I18N.getString("error.empty-user-jvm-option-value"), entry.getKey()),
  65                             I18N.getString("error.empty-user-jvm-option-value.advice"));
  66                 }
  67             }
  68         }
  69 
  70         boolean hasMainJar = MAIN_JAR.fetchFrom(p) != null;
  71         boolean hasMainModule = StandardBundlerParam.MODULE.fetchFrom(p) != null;
  72 
  73         if (!hasMainJar && !hasMainModule) {
  74             if (!hasMainJar) {
  75                 throw new ConfigException(
  76                         I18N.getString("error.no-application-jar"),
  77                         I18N.getString("error.no-application-jar.advice"));
  78             }
  79             else {
  80                 throw new ConfigException(
  81                         I18N.getString("error.no-main-module"),
  82                         I18N.getString("error.no-main-module.advice"));
  83             }
  84         }
  85 
  86         if (ENABLE_APP_CDS.fetchFrom(p)) {
  87             if (!UNLOCK_COMMERCIAL_FEATURES.fetchFrom(p)) {
  88                 throw new ConfigException(
  89                         I18N.getString("error.app-cds-no-commercial-unlock"),
  90                         I18N.getString("error.app-cds-no-commercial-unlock.advice"));
  91             }
  92         }
  93     }
  94 
  95     public static void extractFlagsFromVersion(Map<String, ? super Object> params, String versionOutput) {
  96         Pattern bitArchPattern = Pattern.compile("(\\d*)[- ]?[bB]it");
  97         Matcher matcher = bitArchPattern.matcher(versionOutput);
  98         if (matcher.find()) {
  99             params.put(".runtime.bit-arch", matcher.group(1));
 100         } else {
 101             // presume 32 bit on no match
 102             params.put(".runtime.bit-arch", "32");
 103         }
 104 
 105         Pattern oldVersionMatcher = Pattern.compile("java version \"((\\d+.(\\d+).\\d+)(_(\\d+)))?(-(.*))?\"");
 106         matcher = oldVersionMatcher.matcher(versionOutput);
 107         if (matcher.find()) {
 108             params.put(".runtime.version", matcher.group(1));
 109             params.put(".runtime.version.release", matcher.group(2));
 110             params.put(".runtime.version.major", matcher.group(3));
 111             params.put(".runtime.version.update", matcher.group(5));
 112             params.put(".runtime.version.minor", matcher.group(5));
 113             params.put(".runtime.version.security", matcher.group(5));
 114             params.put(".runtime.version.patch", "0");
 115             params.put(".runtime.version.modifiers", matcher.group(7));
 116         } else {
 117             Pattern newVersionMatcher = Pattern.compile("java version \"((\\d+).(\\d+).(\\d+).(\\d+))(-(.*))?(\\+[^\"]*)?\"");
 118             matcher = newVersionMatcher.matcher(versionOutput);
 119             if (matcher.find()) {
 120                 params.put(".runtime.version", matcher.group(1));
 121                 params.put(".runtime.version.release", matcher.group(1));
 122                 params.put(".runtime.version.major", matcher.group(2));
 123                 params.put(".runtime.version.update", matcher.group(3));
 124                 params.put(".runtime.version.minor", matcher.group(3));
 125                 params.put(".runtime.version.security", matcher.group(4));
 126                 params.put(".runtime.version.patch", matcher.group(5));
 127                 params.put(".runtime.version.modifiers", matcher.group(7));
 128             } else {
 129                 params.put(".runtime.version", "");
 130                 params.put(".runtime.version.release", "");
 131                 params.put(".runtime.version.major", "");
 132                 params.put(".runtime.version.update", "");
 133                 params.put(".runtime.version.minor", "");
 134                 params.put(".runtime.version.security", "");
 135                 params.put(".runtime.version.patch", "");
 136                 params.put(".runtime.version.modifiers", "");
 137             }
 138         }
 139     }
 140 }