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.jpackager.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 import java.io.File; 34 import java.io.IOException; 35 36 import static jdk.jpackager.internal.StandardBundlerParam.*; 37 38 /** 39 * AbstractImageBundler 40 * 41 * This is the base class for each of the Application Image Bundlers. 42 * 43 * It contains methods and parameters common to all Image Bundlers. 44 * 45 * Application Image Bundlers are created in "create-image" mode, 46 * or as an intermeadiate step in "create-installer" mode. 47 * 48 * The concrete implementations are in the platform specific Bundlers. 49 */ 50 public abstract class AbstractImageBundler extends AbstractBundler { 51 52 private final static String JAVA_VERSION_SPEC = 53 "java version \"((\\d+).(\\d+).(\\d+).(\\d+))(-(.*))?(\\+[^\"]*)?\""; 54 55 private static final ResourceBundle I18N = ResourceBundle.getBundle( 56 "jdk.jpackager.internal.resources.AbstractImageBundler"); 57 58 public void imageBundleValidation(Map<String, ? super Object> p) 59 throws ConfigException { 60 StandardBundlerParam.validateMainClassInfoFromAppResources(p); 61 62 boolean hasMainJar = MAIN_JAR.fetchFrom(p) != null; 63 boolean hasMainModule = 64 StandardBundlerParam.MODULE.fetchFrom(p) != null; 65 boolean hasMainClass = MAIN_CLASS.fetchFrom(p) != null; 66 boolean jreInstaller = Arguments.CREATE_JRE_INSTALLER.fetchFrom(p); 67 68 if (!hasMainJar && !hasMainModule && !hasMainClass && !jreInstaller) { 69 throw new ConfigException( 70 I18N.getString("error.no-application-class"), 71 I18N.getString("error.no-application-class.advice")); 72 } 73 } 74 75 public static void extractFlagsFromVersion( 76 Map<String, ? super Object> params, String versionOutput) { 77 Pattern bitArchPattern = Pattern.compile("(\\d*)[- ]?[bB]it"); 78 Matcher matcher = bitArchPattern.matcher(versionOutput); 79 if (matcher.find()) { 80 params.put(".runtime.bit-arch", matcher.group(1)); 81 } else { 82 // presume 32 bit on no match 83 params.put(".runtime.bit-arch", "32"); 84 } 85 86 Pattern oldVersionMatcher = Pattern.compile( 87 "java version \"((\\d+.(\\d+).\\d+)(_(\\d+)))?(-(.*))?\""); 88 matcher = oldVersionMatcher.matcher(versionOutput); 89 if (matcher.find()) { 90 params.put(".runtime.version", matcher.group(1)); 91 params.put(".runtime.version.release", matcher.group(2)); 92 params.put(".runtime.version.major", matcher.group(3)); 93 params.put(".runtime.version.update", matcher.group(5)); 94 params.put(".runtime.version.minor", matcher.group(5)); 95 params.put(".runtime.version.security", matcher.group(5)); 96 params.put(".runtime.version.patch", "0"); 97 params.put(".runtime.version.modifiers", matcher.group(7)); 98 } else { 99 Pattern newVersionMatcher = Pattern.compile(JAVA_VERSION_SPEC); 100 matcher = newVersionMatcher.matcher(versionOutput); 101 if (matcher.find()) { 102 params.put(".runtime.version", matcher.group(1)); 103 params.put(".runtime.version.release", matcher.group(1)); 104 params.put(".runtime.version.major", matcher.group(2)); 105 params.put(".runtime.version.update", matcher.group(3)); 106 params.put(".runtime.version.minor", matcher.group(3)); 107 params.put(".runtime.version.security", matcher.group(4)); 108 params.put(".runtime.version.patch", matcher.group(5)); 109 params.put(".runtime.version.modifiers", matcher.group(7)); 110 } else { 111 params.put(".runtime.version", ""); 112 params.put(".runtime.version.release", ""); 113 params.put(".runtime.version.major", ""); 114 params.put(".runtime.version.update", ""); 115 params.put(".runtime.version.minor", ""); 116 params.put(".runtime.version.security", ""); 117 params.put(".runtime.version.patch", ""); 118 params.put(".runtime.version.modifiers", ""); 119 } 120 } 121 } 122 123 protected File createRoot(Map<String, ? super Object> p, 124 File outputDirectory, boolean dependentTask, 125 String name, String jlinkKey) throws IOException { 126 if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) { 127 throw new RuntimeException(MessageFormat.format( 128 I18N.getString("error.cannot-create-output-dir"), 129 outputDirectory.getAbsolutePath())); 130 } 131 if (!outputDirectory.canWrite()) { 132 throw new RuntimeException(MessageFormat.format( 133 I18N.getString("error.cannot-write-to-output-dir"), 134 outputDirectory.getAbsolutePath())); 135 } 136 if (!dependentTask) { 137 Log.verbose(MessageFormat.format( 138 I18N.getString("message.creating-app-bundle"), 139 name, outputDirectory.getAbsolutePath())); 140 } 141 142 // Create directory structure 143 File rootDirectory = new File(outputDirectory, name); 144 145 if (rootDirectory.exists()) { 146 if (!(FORCE.fetchFrom(p))) { 147 throw new IOException(MessageFormat.format( 148 I18N.getString("error.root-exists-without-force"), 149 rootDirectory.getAbsolutePath())); 150 } 151 IOUtils.deleteRecursive(rootDirectory); 152 } 153 rootDirectory.mkdirs(); 154 155 if (!p.containsKey(JLinkBundlerHelper.JLINK_BUILDER.getID())) { 156 p.put(JLinkBundlerHelper.JLINK_BUILDER.getID(), jlinkKey); 157 } 158 159 return rootDirectory; 160 } 161 162 }