< 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 
  34 /*
  35  * Support class used by JVMTI and VM attach mechanism.
  36  */
  37 public class VMSupport {
  38 
  39     private static Properties agentProps = null;
  40     /**
  41      * Returns the agent properties.
  42      */
  43     public static synchronized Properties getAgentProperties() {
  44         if (agentProps == null) {
  45             agentProps = new Properties();
  46             initAgentProperties(agentProps);
  47         }
  48         return agentProps;
  49     }
  50     private static native Properties initAgentProperties(Properties props);
  51 
  52     private static String convertToBytesStr(String str, boolean escapeSpace) throws Exception {
  53         str = str.replace("\n", "\\n")
  54                  .replace("\r", "\\r")
  55                  .replace("\t", "\\t")
  56                  .replace("\f", "\\f");
  57         if (escapeSpace) {
  58             str = str.replace(" ", "\\ ");
  59         }
  60 
  61         String result = "";
  62         for (char ch : str.toCharArray()) {
  63             result += ((ch < 0x20) || (ch > 0x7e)) ? String.format("\\u%04X", (int)ch)
  64                                                    : ch;
  65         }
  66 
  67         return result;
  68     }
  69 
  70     /**
  71      * Write the given properties list to a byte array and return it. Properties with
  72      * a key or value that is not a String is filtered out.

  73      */
  74     private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
  75         var joiner = new StringJoiner("\n");
  76         joiner.add((new Date()).toString());
  77         for (var entry : p.entrySet()) {
  78             var key = convertToBytes(entry.getKey().toString(), true);
  79             var value = convertToBytes(entry.getValue().toString(), false);
  80             joiner.add(key + "=" + value);



  81         }
  82         return joiner.toString().getBytes();


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