jdk/src/java.base/share/classes/sun/misc/VMSupport.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 24268 jdk/src/share/classes/sun/misc/VMSupport.java@43fb1c9e594c
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Support class used by JVMTI and VM attach mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class VMSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Properties agentProps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Returns the agent properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static synchronized Properties getAgentProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (agentProps == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            agentProps = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            initAgentProperties(agentProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return agentProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static native Properties initAgentProperties(Properties props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Write the given properties list to a byte array and return it. Properties with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * a key or value that is not a String is filtered out. The stream written to the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * array is ISO 8859-1 encoded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // stringPropertyNames() returns a snapshot of the property keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        Set<String> keyset = p.stringPropertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        for (String key : keyset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            String value = p.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            props.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        props.store(out, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static byte[] serializePropertiesToByteArray() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return serializePropertiesToByteArray(System.getProperties());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static byte[] serializeAgentPropertiesToByteArray() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return serializePropertiesToByteArray(getAgentProperties());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns true if the given JAR file has the Class-Path attribute in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * main section of the JAR manifest. Throws RuntimeException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * path is not a JAR file or some other error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static boolean isClassPathAttributePresent(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            Manifest man = (new JarFile(path)).getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (man.getMainAttributes().getValue(Attributes.Name.CLASS_PATH) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new RuntimeException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
24268
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   100
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   101
    /*
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   102
     * Return the temporary directory that the VM uses for the attach
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   103
     * and perf data files.
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   104
     *
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   105
     * It is important that this directory is well-known and the
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   106
     * same for all VM instances. It cannot be affected by configuration
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   107
     * variables such as java.io.tmpdir.
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   108
     */
43fb1c9e594c 8041979: sun/jvmstat/monitor/MonitoredVm/CR6672135.java failing on all platforms
sla
parents: 24120
diff changeset
   109
    public static native String getVMTemporaryDirectory();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}