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 sun.misc;
  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) {
  92                     return true;
  93                 }
  94             }
  95             return false;
  96         } catch (IOException ioe) {
  97             throw new RuntimeException(ioe.getMessage());
  98         }
  99     }
 100 }