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 jdk.packager.services.userjvmoptions;
  27 
  28 import jdk.packager.services.UserJvmOptionsService;
  29 
  30 import java.security.AllPermission;
  31 import java.util.LinkedHashMap;
  32 import java.util.LinkedList;
  33 import java.util.List;
  34 import java.util.Map;
  35 
  36 /**
  37  *
  38  * Access the UserJVMOptions via a native library provided by the launcher.
  39  *
  40  * Do not instantiate this class directly, instead use
  41  * {@see jdk.packager.services.UserJvmOptionsService#getUserJVMDefaults()}
  42  * to get an instance.
  43  *
  44  * @since 8u40
  45  */
  46 final public class LauncherUserJvmOptions implements UserJvmOptionsService {
  47 
  48     private static final Object semaphore = new Object();
  49 
  50     static {
  51         try {
  52             checkAllPermissions();
  53             System.loadLibrary("packager");
  54         } catch (SecurityException se) {
  55             se.printStackTrace(System.err);
  56             // fail to load, we will also throw on all public methods
  57         }
  58     }
  59 
  60     private static void checkAllPermissions() {
  61         final SecurityManager securityManager = System.getSecurityManager();
  62         if (securityManager != null) {
  63             securityManager.checkPermission(new AllPermission());
  64         }
  65     }
  66 
  67     /**
  68      * Access the default User JVM Option for a specific key
  69      *
  70      * @param option the key for the User JVM Option
  71      *
  72      * @return the default value of the user JVM Option.  Currently set user
  73      * values are not considered, and only the default is returned.  If there
  74      * is no default value then null is returned.
  75      */
  76     private static native String _getUserJvmOptionDefaultValue(String option);
  77 
  78     /**
  79      * This lists the keys for User JVM Options that will have a default
  80      * provided by the launcher.
  81      *
  82      * This list will be a subset of the keys used by the launcher and only
  83      * lists those values that will have a default value provided if the user
  84      * does not set a value of their own.
  85      *
  86      * @return an array of keys in no particular order.
  87      */
  88     private static native String[] _getUserJvmOptionDefaultKeys();
  89     /**
  90      * Access the current User JVM Option for a specific key
  91      *
  92      * @param option the key for the User JVM Option
  93      *
  94      * @return the current value of the user JVM Option.  If the user has not
  95      * set a value then the default value is returned, except in the case where
  96      * there is no default value, where null is returned.
  97      */
  98     private static native String _getUserJvmOptionValue(String option);
  99 
 100     /**
 101      * Update the all User JVM Options
 102      *
 103      * All option/value pairs will be replaced with the values provided. The
 104      * parameters options and values are paired at the same index.
 105      * Example: options[3] = -Xmx and values[3] = 999m
 106      * This cannot be used to adjust default values.
 107      *
 108      * @param options the keys for the User JVM Options
 109      * @param values the values for the User JVM Options
 110      */
 111     private static native void _setUserJvmKeysAndValues(String[] options, String[] values);
 112 
 113     /**
 114      * This lists the keys for all User JVM Options that will be used by the
 115      * launcher.
 116      *
 117      * This list will be a superset of the defaults as may also include user
 118      * values that do not have a default.
 119      *
 120      * @return an array of keys in no particular order.
 121      */
 122     private static native String[] _getUserJvmOptionKeys();
 123 
 124     @Override
 125     public Map<String, String> getUserJVMOptions() {
 126         checkAllPermissions();
 127         synchronized (semaphore) {
 128             Map<String, String> results = new LinkedHashMap<>();
 129             for (String s : _getUserJvmOptionKeys()) {
 130                 results.put(s, _getUserJvmOptionValue(s));
 131             }
 132             return results;
 133         }
 134     }
 135 
 136     @Override
 137     public void setUserJVMOptions(Map<String, String> options) {
 138         checkAllPermissions();
 139         synchronized (semaphore) {
 140             List<String> keys = new LinkedList<>();
 141             List<String> values = new LinkedList<>();
 142 
 143             for (Map.Entry<String, String> option : options.entrySet()) {
 144                 if (option.getKey() == null) {
 145                     throw new IllegalArgumentException("For Launcher Backed UserJVMOptions keys in the UserJVMOptions map cannot be null.");
 146                 }
 147                 if (option.getValue() == null) {
 148                     throw new IllegalArgumentException("For Launcher Backed UserJVMOptions values in the UserJVMOptions map cannot be null.  Keys are removed by absence, not by setting keys to null.");
 149                 }
 150                 keys.add(option.getKey());
 151                 values.add(option.getValue());
 152             }
 153 
 154             _setUserJvmKeysAndValues(keys.toArray(new String[keys.size()]),
 155                     values.toArray(new String[values.size()]));
 156         }
 157     }
 158 
 159     @Override
 160     public Map<String, String> getUserJVMOptionDefaults() {
 161         checkAllPermissions();
 162         synchronized (semaphore) {
 163             Map<String, String> results = new LinkedHashMap<>();
 164             for (String s : _getUserJvmOptionDefaultKeys()) {
 165                 results.put(s, _getUserJvmOptionDefaultValue(s));
 166             }
 167             return results;
 168         }
 169     }
 170 }