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