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             // fail to load, we will also throw on all public methods
  56         }
  57     }
  58 
  59     private static void checkAllPermissions() {
  60         final SecurityManager securityManager = System.getSecurityManager();
  61         if (securityManager != null) {
  62             securityManager.checkPermission(new AllPermission());
  63         }
  64     }
  65 
  66     /**
  67      * Access the default User JVM Option for a specific key
  68      *
  69      * @param option the key for the User JVM Option
  70      *
  71      * @return the default value of the user JVM Option.  Currently set user
  72      * values are not considered, and only the default is returned.  If there
  73      * is no default value then null is returned.
  74      */
  75     private static native String _getUserJvmOptionDefaultValue(String option);
  76 
  77     /**
  78      * This lists the keys for User JVM Options that will have a default
  79      * provided by the launcher.
  80      *
  81      * This list will be a subset of the keys used by the launcher and only
  82      * lists those values that will have a default value provided if the user
  83      * does not set a value of their own.
  84      *
  85      * @return an array of keys in no particular order.
  86      */
  87     private static native String[] _getUserJvmOptionDefaultKeys();
  88     /**
  89      * Access the current User JVM Option for a specific key
  90      *
  91      * @param option the key for the User JVM Option
  92      *
  93      * @return the current value of the user JVM Option.  If the user has not
  94      * set a value then the default value is returned, except in the case where
  95      * there is no default value, where null is returned.
  96      */
  97     private static native String _getUserJvmOptionValue(String option);
  98 
  99     /**
 100      * Update the all User JVM Options
 101      *
 102      * All option/value pairs will be replaced with the values provided. The
 103      * parameters options and values are paired at the same index.
 104      * Example: options[3] = -Xmx and values[3] = 999m
 105      * This cannot be used to adjust default values.
 106      *
 107      * @param options the keys for the User JVM Options
 108      * @param values the values for the User JVM Options
 109      */
 110     private static native void _setUserJvmKeysAndValues(String[] options, String[] values);
 111 
 112     /**
 113      * This lists the keys for all User JVM Options that will be used by the
 114      * launcher.
 115      *
 116      * This list will be a superset of the defaults as may also include user
 117      * values that do not have a default.
 118      *
 119      * @return an array of keys in no particular order.
 120      */
 121     private static native String[] _getUserJvmOptionKeys();
 122 
 123     @Override
 124     public Map<String, String> getUserJVMOptions() {
 125         checkAllPermissions();
 126         synchronized (semaphore) {
 127             Map<String, String> results = new LinkedHashMap<>();
 128             for (String s : _getUserJvmOptionKeys()) {
 129                 results.put(s, _getUserJvmOptionValue(s));
 130             }
 131             return results;
 132         }
 133     }
 134 
 135     @Override
 136     public void setUserJVMOptions(Map<String, String> options) {
 137         checkAllPermissions();
 138         synchronized (semaphore) {
 139             List<String> keys = new LinkedList<>();
 140             List<String> values = new LinkedList<>();
 141 
 142             for (Map.Entry<String, String> option : options.entrySet()) {
 143                 if (option.getKey() == null) {
 144                     throw new IllegalArgumentException("For Launcher Backed UserJVMOptions keys in the UserJVMOptions map cannot be null.");
 145                 }
 146                 if (option.getValue() == null) {
 147                     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.");
 148                 }
 149                 keys.add(option.getKey());
 150                 values.add(option.getValue());
 151             }
 152 
 153             _setUserJvmKeysAndValues(keys.toArray(new String[keys.size()]),
 154                     values.toArray(new String[values.size()]));
 155         }
 156     }
 157 
 158     @Override
 159     public Map<String, String> getUserJVMOptionDefaults() {
 160         checkAllPermissions();
 161         synchronized (semaphore) {
 162             Map<String, String> results = new LinkedHashMap<>();
 163             for (String s : _getUserJvmOptionDefaultKeys()) {
 164                 results.put(s, _getUserJvmOptionDefaultValue(s));
 165             }
 166             return results;
 167         }
 168     }
 169 }