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.tools.packager.windows;
  27 
  28 import com.oracle.tools.packager.StandardBundlerParam;
  29 import com.oracle.tools.packager.Log;
  30 import com.oracle.tools.packager.IOUtils;
  31 import com.oracle.tools.packager.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(WindowsBundlerParam.class.getName());
  48     
  49     public WindowsBundlerParam(String name, String description, String id, Class<T> valueType, Function<Map<String, ? super Object>, T> defaultValueFunction, BiFunction<String, Map<String, ? super Object>, T> stringConverter) {
  50         super(name, description, id, valueType, defaultValueFunction, 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                     params -> params.containsKey(VENDOR.getID())
  60                             ? VENDOR.fetchFrom(params)
  61                             : params.containsKey(CATEGORY.getID())
  62                             ? CATEGORY.fetchFrom(params)
  63                             : I18N.getString("param.menu-group.default"),
  64                     (s, p) -> s
  65             );
  66 
  67     public static final StandardBundlerParam<Boolean> BIT_ARCH_64 =
  68             new StandardBundlerParam<>(
  69                     I18N.getString("param.64-bit.name"),
  70                     I18N.getString("param.64-bit.description"),
  71                     "win.64Bit",
  72                     Boolean.class,
  73                     params -> System.getProperty("os.arch").contains("64"),
  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                     params -> {extractFlagsFromRuntime(params); return "64".equals(params.get(".runtime.bit-arch"));},
  84                     (s, p) -> Boolean.valueOf(s)
  85             );
  86        
  87     public static void extractFlagsFromRuntime(Map<String, ? super Object> params) {
  88         if (params.containsKey(".runtime.autodetect")) return;
  89         
  90         params.put(".runtime.autodetect", "attempted");
  91         RelativeFileSet runtime = RUNTIME.fetchFrom(params);
  92         String commandline;
  93         if (runtime == null) {
  94             //its ok, request to use system JRE
  95             //TODO extract from system properties
  96             commandline = "java version \"" + System.getProperty("java.version") + "\"\n"
  97                     + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.version") + ", " + System.getProperty("java.vm.info") + ")\n";  
  98         } else {
  99             File runtimePath = runtime.getBaseDirectory();
 100             File launcherPath = new File(runtimePath, "bin\\java");
 101     
 102             ProcessBuilder pb = new ProcessBuilder(launcherPath.getAbsolutePath(), "-version");
 103             try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
 104                 try (PrintStream pout = new PrintStream(baos)) {
 105                     IOUtils.exec(pb, Log.isDebug(), true, pout);                    
 106                 }
 107                 
 108                 commandline = baos.toString();
 109             } catch (IOException e) {
 110                 e.printStackTrace();
 111                 params.put(".runtime.autodetect", "failed");
 112                 return;
 113             }
 114         }
 115         extractFlagsFromVersion(params, commandline);
 116         params.put(".runtime.autodetect", "succeeded");
 117     }
 118 
 119     public static void extractFlagsFromVersion(Map<String, ? super Object> params, String versionOutput) {
 120         Pattern bitArchPattern = Pattern.compile("(\\d*)[- ]?[bB]it");
 121         Matcher matcher = bitArchPattern.matcher(versionOutput);
 122         if (matcher.find()) {
 123             params.put(".runtime.bit-arch", matcher.group(1));
 124         } else {
 125             // presume 32 bit on no match
 126             params.put(".runtime.bit-arch", "32");
 127         }
 128 
 129         Pattern versionMatcher = Pattern.compile("java version \"((\\d+.\\d+.\\d+)_(\\d+))(-(.*))?\"");
 130         matcher = versionMatcher.matcher(versionOutput);
 131         if (matcher.find()) {
 132             params.put(".runtime.version", matcher.group(1));
 133             params.put(".runtime.version.release", matcher.group(2));
 134             params.put(".runtime.version.update", matcher.group(3));
 135             params.put(".runtime.version.modifiers", matcher.group(5));
 136         } else {
 137             params.put(".runtime.version", "");
 138             params.put(".runtime.version.release", "");
 139             params.put(".runtime.version.update", "");
 140             params.put(".runtime.version.modifiers", "");
 141         }
 142     }
 143 }