< prev index next >

src/java.base/share/classes/jdk/internal/vm/VMSupport.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 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 package jdk.internal.vm;
  26 
  27 import java.io.ByteArrayOutputStream;
  28 import java.io.IOException;
  29 import java.util.Properties;
  30 import java.util.Set;
  31 import java.util.jar.JarFile;
  32 import java.util.jar.Manifest;
  33 import java.util.jar.Attributes;



  34 
  35 /*
  36  * Support class used by JVMTI and VM attach mechanism.
  37  */
  38 public class VMSupport {
  39 
  40     private static Properties agentProps = null;
  41     /**
  42      * Returns the agent properties.
  43      */
  44     public static synchronized Properties getAgentProperties() {
  45         if (agentProps == null) {
  46             agentProps = new Properties();
  47             initAgentProperties(agentProps);
  48         }
  49         return agentProps;
  50     }
  51     private static native Properties initAgentProperties(Properties props);
  52 























  53     /**
  54      * Write the given properties list to a byte array and return it. Properties with
  55      * a key or value that is not a String is filtered out. The stream written to the byte
  56      * array is ISO 8859-1 encoded.
  57      */
  58     private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
  59         ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
  60 
  61         Properties props = new Properties();
  62 
  63         // stringPropertyNames() returns a snapshot of the property keys
  64         Set<String> keyset = p.stringPropertyNames();
  65         for (String key : keyset) {
  66             String value = p.getProperty(key);
  67             props.put(key, value);
  68         }
  69 
  70         props.store(out, null);
  71         return out.toByteArray();
  72     }
  73 
  74     public static byte[] serializePropertiesToByteArray() throws IOException {
  75         return serializePropertiesToByteArray(System.getProperties());
  76     }
  77 
  78     public static byte[] serializeAgentPropertiesToByteArray() throws IOException {
  79         return serializePropertiesToByteArray(getAgentProperties());
  80     }
  81 
  82     /*
  83      * Returns true if the given JAR file has the Class-Path attribute in the
  84      * main section of the JAR manifest. Throws RuntimeException if the given
  85      * path is not a JAR file or some other error occurs.
  86      */
  87     public static boolean isClassPathAttributePresent(String path) {
  88         try {
  89             Manifest man = (new JarFile(path)).getManifest();
  90             if (man != null) {
  91                 if (man.getMainAttributes().getValue(Attributes.Name.CLASS_PATH) != null) {
   1 /*
   2  * Copyright (c) 2005, 2020, 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 package jdk.internal.vm;
  26 

  27 import java.io.IOException;
  28 import java.util.Properties;

  29 import java.util.jar.JarFile;
  30 import java.util.jar.Manifest;
  31 import java.util.jar.Attributes;
  32 import java.util.*;
  33 import java.nio.*;
  34 import java.nio.charset.*;
  35 
  36 /*
  37  * Support class used by JVMTI and VM attach mechanism.
  38  */
  39 public class VMSupport {
  40 
  41     private static Properties agentProps = null;
  42     /**
  43      * Returns the agent properties.
  44      */
  45     public static synchronized Properties getAgentProperties() {
  46         if (agentProps == null) {
  47             agentProps = new Properties();
  48             initAgentProperties(agentProps);
  49         }
  50         return agentProps;
  51     }
  52     private static native Properties initAgentProperties(Properties props);
  53 
  54     private static String convertToBytesStr(String str, boolean escapeSpace) throws Exception {
  55         if (escapeSpace) {
  56             str = str.replace(" ", "\\ ");
  57         }
  58 
  59         var charBuf = CharBuffer.wrap(str);
  60         var byteBuf = ByteBuffer.allocate(charBuf.length() * 5); // 1 unicode char might have 4 digits + 2 ('\u')
  61         var encoder = StandardCharsets.ISO_8859_1
  62                                       .newEncoder()
  63                                       .onUnmappableCharacter(CodingErrorAction.REPORT);
  64         CoderResult result;
  65         do {
  66             result = encoder.encode(charBuf, byteBuf, false);
  67             if (result.isUnmappable()) {
  68                 byteBuf.put(String.format("\\u%04X", (int)charBuf.get()).getBytes());
  69             } else if (result.isError()) {
  70                 result.throwException();
  71             }
  72         } while (result.isError());
  73 
  74         return new String(byteBuf.array());
  75     }
  76 
  77     /**
  78      * Write the given properties list to a byte array and return it. Properties with
  79      * a key or value that is not a String is filtered out. The stream written to the byte
  80      * array is ISO 8859-1 encoded.
  81      */
  82     private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
  83         var joiner = new StringJoiner("\n");
  84         joiner.add((new Date()).toString());
  85         for (var entry : p.entrySet()) {
  86             var key = convertToBytes(entry.getKey().toString(), true);
  87             var value = convertToBytes(entry.getValue().toString(), false);
  88             joiner.add(key + "=" + value);



  89         }
  90         return joiner.toString().getBytes();


  91     }
  92 
  93     public static byte[] serializePropertiesToByteArray() throws IOException {
  94         return serializePropertiesToByteArray(System.getProperties());
  95     }
  96 
  97     public static byte[] serializeAgentPropertiesToByteArray() throws IOException {
  98         return serializePropertiesToByteArray(getAgentProperties());
  99     }
 100 
 101     /*
 102      * Returns true if the given JAR file has the Class-Path attribute in the
 103      * main section of the JAR manifest. Throws RuntimeException if the given
 104      * path is not a JAR file or some other error occurs.
 105      */
 106     public static boolean isClassPathAttributePresent(String path) {
 107         try {
 108             Manifest man = (new JarFile(path)).getManifest();
 109             if (man != null) {
 110                 if (man.getMainAttributes().getValue(Attributes.Name.CLASS_PATH) != null) {
< prev index next >