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.internal.misc; 27 28 import static java.lang.Thread.State.*; 29 import java.util.Properties; 30 31 public class VM { 32 33 // the init level when the VM is fully initialized 34 private static final int JAVA_LANG_SYSTEM_INITED = 1; 35 private static final int MODULE_SYSTEM_INITED = 2; 36 private static final int SYSTEM_LOADER_INITIALIZING = 3; 37 private static final int SYSTEM_BOOTED = 4; 38 39 // 0, 1, 2, ... 40 private static volatile int initLevel; 41 private static final Object lock = new Object(); 42 43 /** 44 * Sets the init level. 45 * 46 * @see java.lang.System#initPhase1 47 * @see java.lang.System#initPhase2 48 * @see java.lang.System#initPhase3 49 */ 120 return pageAlignDirectMemory; 121 } 122 123 /** 124 * Returns true if the given class loader is in the system domain 125 * in which all permissions are granted. 126 */ 127 public static boolean isSystemDomainLoader(ClassLoader loader) { 128 return loader == null; 129 } 130 131 /** 132 * Returns the system property of the specified key saved at 133 * system initialization time. This method should only be used 134 * for the system properties that are not changed during runtime. 135 * It accesses a private copy of the system properties so 136 * that user's locking of the system properties object will not 137 * cause the library to deadlock. 138 * 139 * Note that the saved system properties do not include 140 * the ones set by sun.misc.Version.init(). 141 * 142 */ 143 public static String getSavedProperty(String key) { 144 if (savedProps.isEmpty()) 145 throw new IllegalStateException("Should be non-empty if initialized"); 146 147 return savedProps.getProperty(key); 148 } 149 150 // TODO: the Property Management needs to be refactored and 151 // the appropriate prop keys need to be accessible to the 152 // calling classes to avoid duplication of keys. 153 private static final Properties savedProps = new Properties(); 154 155 // Save a private copy of the system properties and remove 156 // the system properties that are not intended for public access. 157 // 158 // This method can only be invoked during system initialization. 159 public static void saveAndRemoveProperties(Properties props) { 160 if (initLevel() != 0) 161 throw new IllegalStateException("Wrong init level"); 162 163 savedProps.putAll(props); 164 165 // Set the maximum amount of direct memory. This value is controlled 166 // by the vm option -XX:MaxDirectMemorySize=<size>. 167 // The maximum amount of allocatable direct buffer memory (in bytes) 168 // from the system property sun.nio.MaxDirectMemorySize set by the VM. 169 // The system property will be removed. 170 String s = (String)props.remove("sun.nio.MaxDirectMemorySize"); 171 if (s != null) { 172 if (s.equals("-1")) { 173 // -XX:MaxDirectMemorySize not given, take default 174 directMemory = Runtime.getRuntime().maxMemory(); 175 } else { 176 long l = Long.parseLong(s); 177 if (l > -1) 178 directMemory = l; 179 } 180 } 181 182 // Check if direct buffers should be page aligned 183 s = (String)props.remove("sun.nio.PageAlignDirectMemory"); | 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.internal.misc; 27 28 import static java.lang.Thread.State.*; 29 import java.util.Map; 30 import java.util.HashMap; 31 import java.util.Properties; 32 import java.util.Collections; 33 34 public class VM { 35 36 // the init level when the VM is fully initialized 37 private static final int JAVA_LANG_SYSTEM_INITED = 1; 38 private static final int MODULE_SYSTEM_INITED = 2; 39 private static final int SYSTEM_LOADER_INITIALIZING = 3; 40 private static final int SYSTEM_BOOTED = 4; 41 42 // 0, 1, 2, ... 43 private static volatile int initLevel; 44 private static final Object lock = new Object(); 45 46 /** 47 * Sets the init level. 48 * 49 * @see java.lang.System#initPhase1 50 * @see java.lang.System#initPhase2 51 * @see java.lang.System#initPhase3 52 */ 123 return pageAlignDirectMemory; 124 } 125 126 /** 127 * Returns true if the given class loader is in the system domain 128 * in which all permissions are granted. 129 */ 130 public static boolean isSystemDomainLoader(ClassLoader loader) { 131 return loader == null; 132 } 133 134 /** 135 * Returns the system property of the specified key saved at 136 * system initialization time. This method should only be used 137 * for the system properties that are not changed during runtime. 138 * It accesses a private copy of the system properties so 139 * that user's locking of the system properties object will not 140 * cause the library to deadlock. 141 * 142 * Note that the saved system properties do not include 143 * the ones set by java.lang.VersionProps.init(). 144 * 145 */ 146 public static String getSavedProperty(String key) { 147 if (savedProps == null) 148 throw new IllegalStateException("Not yet initialized"); 149 150 return savedProps.get(key); 151 } 152 153 /** 154 * Gets an unmodifiable view of the system properties saved at system 155 * initialization time. This method should only be used 156 * for the system properties that are not changed during runtime. 157 * 158 * Note that the saved system properties do not include 159 * the ones set by java.lang.VersionProps.init(). 160 */ 161 public static Map<String, String> getSavedProperties() { 162 if (savedProps == null) 163 throw new IllegalStateException("Not yet initialized"); 164 165 return savedProps; 166 } 167 168 // TODO: the Property Management needs to be refactored and 169 // the appropriate prop keys need to be accessible to the 170 // calling classes to avoid duplication of keys. 171 private static Map<String, String> savedProps; 172 173 // Save a private copy of the system properties and remove 174 // the system properties that are not intended for public access. 175 // 176 // This method can only be invoked during system initialization. 177 public static void saveAndRemoveProperties(Properties props) { 178 if (initLevel() != 0) 179 throw new IllegalStateException("Wrong init level"); 180 181 @SuppressWarnings("unchecked") 182 Map<String, String> sp = new HashMap<>((Map)props); 183 // only main thread is running at this time, so savedProps and 184 // its content will be correctly published to threads started later 185 savedProps = Collections.unmodifiableMap(sp); 186 187 // Set the maximum amount of direct memory. This value is controlled 188 // by the vm option -XX:MaxDirectMemorySize=<size>. 189 // The maximum amount of allocatable direct buffer memory (in bytes) 190 // from the system property sun.nio.MaxDirectMemorySize set by the VM. 191 // The system property will be removed. 192 String s = (String)props.remove("sun.nio.MaxDirectMemorySize"); 193 if (s != null) { 194 if (s.equals("-1")) { 195 // -XX:MaxDirectMemorySize not given, take default 196 directMemory = Runtime.getRuntime().maxMemory(); 197 } else { 198 long l = Long.parseLong(s); 199 if (l > -1) 200 directMemory = l; 201 } 202 } 203 204 // Check if direct buffers should be page aligned 205 s = (String)props.remove("sun.nio.PageAlignDirectMemory"); |