1 /*
   2  * Copyright (c) 2014, 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 com.oracle.bundlers.windows;
  27 
  28 import com.oracle.bundlers.StandardBundlerParam;
  29 import com.sun.javafx.tools.packager.Log;
  30 import com.sun.javafx.tools.packager.bundlers.IOUtils;
  31 import com.sun.javafx.tools.packager.bundlers.RelativeFileSet;
  32 
  33 import java.io.ByteArrayOutputStream;
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.PrintStream;
  37 import java.util.Map;
  38 import java.util.ResourceBundle;
  39 import java.util.function.BiFunction;
  40 import java.util.function.Function;
  41 import java.util.regex.Matcher;
  42 import java.util.regex.Pattern;
  43 
  44 
  45 public class WindowsBundlerParam<T> extends StandardBundlerParam<T> {
  46     
  47     private static final ResourceBundle I18N = ResourceBundle.getBundle("com.oracle.bundlers.windows.WindowsBundlerParam");
  48     
  49     public WindowsBundlerParam(String name, String description, String id, Class<T> valueType, String[] fallbackIDs, Function<Map<String, ? super Object>, T> defaultValueFunction, boolean requiresUserSetting, BiFunction<String, Map<String, ? super Object>, T> stringConverter) {
  50         super(name, description, id, valueType, fallbackIDs, defaultValueFunction, requiresUserSetting, stringConverter);
  51     }
  52 
  53     public static final StandardBundlerParam<String> MENU_GROUP =
  54             new StandardBundlerParam<>(
  55                     I18N.getString("param.menu-group.name"),
  56                     I18N.getString("param.menu-group.description"),
  57                     "win.menuGroup",
  58                     String.class,
  59                     new String[] {VENDOR.getID(), CATEGORY.getID(), },
  60                     params -> I18N.getString("param.menu-group.default"),
  61                     false,
  62                     (s, p) -> s
  63             );
  64 
  65     public static final StandardBundlerParam<Boolean> BIT_ARCH_64 =
  66             new StandardBundlerParam<>(
  67                     I18N.getString("param.64-bit.name"),
  68                     I18N.getString("param.64-bit.description"),
  69                     "win.64Bit",
  70                     Boolean.class,
  71                     null,
  72                     params -> System.getProperty("os.arch").contains("64"),
  73                     false,
  74                     (s, p) -> Boolean.valueOf(s)
  75             );
  76 
  77     public static final StandardBundlerParam<Boolean> BIT_ARCH_64_RUNTIME =
  78             new StandardBundlerParam<>(
  79                     I18N.getString("param.runtime-64-bit.name"),
  80                     I18N.getString("param.runtime-64-bit.description"),
  81                     "win.64BitJreRuntime",
  82                     Boolean.class,
  83                     null,
  84                     params -> {extractFlagsFromRuntime(params); return "64".equals(params.get(".runtime.bit-arc"));},
  85                     false,
  86                     (s, p) -> Boolean.valueOf(s)
  87             );
  88        
  89     public static void extractFlagsFromRuntime(Map<String, ? super Object> params) {
  90         if (params.containsKey(".runtime.autodetect")) return;
  91         
  92         params.put(".runtime.autodetect", "attempted");
  93         RelativeFileSet runtime = RUNTIME.fetchFrom(params);
  94         String commandline;
  95         if (runtime == null) {
  96             //its ok, request to use system JRE
  97             //TODO extract from system properties
  98             commandline = "java version \"" + System.getProperty("java.version") + "\"\n"
  99                     + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.version") + ", " + System.getProperty("java.vm.info") + ")\n";  
 100         } else {
 101             File runtimePath = runtime.getBaseDirectory();
 102             File launcherPath = new File(runtimePath, "bin\\java");
 103     
 104             ProcessBuilder pb = new ProcessBuilder(launcherPath.getAbsolutePath(), "-version");
 105             try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
 106                 try (PrintStream pout = new PrintStream(baos)) {
 107                     IOUtils.exec(pb, Log.isDebug(), true, pout);                    
 108                 }
 109                 
 110                 commandline = baos.toString();
 111             } catch (IOException e) {
 112                 e.printStackTrace();
 113                 params.put(".runtime.autodetect", "failed");
 114                 return;
 115             }
 116         }
 117         extractFlagsFromVersion(params, commandline);
 118         params.put(".runtime.autodetect", "succeeded");
 119     }
 120 
 121     public static void extractFlagsFromVersion(Map<String, ? super Object> params, String versionOutput) {
 122         Pattern bitArchPattern = Pattern.compile("(\\d*)[- ]?[bB]it");
 123         Matcher matcher = bitArchPattern.matcher(versionOutput);
 124         if (matcher.find()) {
 125             params.put(".runtime.bit-arch", matcher.group(1));
 126         } else {
 127             // presume 32 bit on no match
 128             params.put(".runtime.bit-arch", "32");
 129         }
 130 
 131         Pattern versionMatcher = Pattern.compile("java version \"((\\d+.\\d+.\\d+)_(\\d+))(-(.*))?\"");
 132         matcher = versionMatcher.matcher(versionOutput);
 133         if (matcher.find()) {
 134             params.put(".runtime.version", matcher.group(1));
 135             params.put(".runtime.version.release", matcher.group(2));
 136             params.put(".runtime.version.update", matcher.group(3));
 137             params.put(".runtime.version.modifiers", matcher.group(5));
 138         } else {
 139             params.put(".runtime.version", "");
 140             params.put(".runtime.version.release", "");
 141             params.put(".runtime.version.update", "");
 142             params.put(".runtime.version.modifiers", "");
 143         }
 144     }
 145 }