ConnectorAddressLink.java 6.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright 2004-2008 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package sun.management;

28
import java.io.IOException;
D
duke 已提交
29
import java.nio.ByteBuffer;
30
import java.util.HashMap;
D
duke 已提交
31
import java.util.Iterator;
32 33 34
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
D
duke 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

import sun.misc.Perf;
import sun.management.counter.Units;
import sun.management.counter.Counter;
import sun.management.counter.perf.PerfInstrumentation;

/**
 * A utility class to support the exporting and importing of the address
 * of a connector server using the instrumentation buffer.
 *
 * @since 1.5
 */
public class ConnectorAddressLink {

    private static final String CONNECTOR_ADDRESS_COUNTER =
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
            "sun.management.JMXConnectorServer.address";

    /*
     * The format of the jvmstat counters representing the properties of
     * a given out-of-the-box JMX remote connector will be as follows:
     *
     * sun.management.JMXConnectorServer.<counter>.<key>=<value>
     *
     * where:
     *
     *     counter = index computed by this class which uniquely identifies
     *               an out-of-the-box JMX remote connector running in this
     *               Java virtual machine.
     *     key/value = a given key/value pair in the map supplied to the
     *                 exportRemote() method.
     *
     * For example,
     *
     * sun.management.JMXConnectorServer.0.remoteAddress=service:jmx:rmi:///jndi/rmi://myhost:5000/jmxrmi
     * sun.management.JMXConnectorServer.0.authenticate=false
     * sun.management.JMXConnectorServer.0.ssl=false
     * sun.management.JMXConnectorServer.0.sslRegistry=false
     * sun.management.JMXConnectorServer.0.sslNeedClientAuth=false
     */
    private static final String REMOTE_CONNECTOR_COUNTER_PREFIX =
            "sun.management.JMXConnectorServer.";

    /*
     * JMX remote connector counter (it will be incremented every
     * time a new out-of-the-box JMX remote connector is created).
     */
    private static AtomicInteger counter = new AtomicInteger();
D
duke 已提交
82 83 84 85 86 87

    /**
     * Exports the specified connector address to the instrumentation buffer
     * so that it can be read by this or other Java virtual machines running
     * on the same system.
     *
88
     * @param address The connector address.
D
duke 已提交
89 90 91 92 93 94
     */
    public static void export(String address) {
        if (address == null || address.length() == 0) {
            throw new IllegalArgumentException("address not specified");
        }
        Perf perf = Perf.getPerf();
95 96
        perf.createString(
                CONNECTOR_ADDRESS_COUNTER, 1, Units.STRING.intValue(), address);
D
duke 已提交
97 98 99 100 101 102
    }

    /**
     * Imports the connector address from the instrument buffer
     * of the specified Java virtual machine.
     *
103 104
     * @param vmid an identifier that uniquely identifies a local Java virtual
     * machine, or <code>0</code> to indicate the current Java virtual machine.
D
duke 已提交
105
     *
106 107
     * @return the value of the connector address, or <code>null</code> if the
     * target VM has not exported a connector address.
D
duke 已提交
108
     *
109 110
     * @throws IOException An I/O error occurred while trying to acquire the
     * instrumentation buffer.
D
duke 已提交
111 112 113 114 115 116 117 118 119
     */
    public static String importFrom(int vmid) throws IOException {
        Perf perf = Perf.getPerf();
        ByteBuffer bb;
        try {
            bb = perf.attach(vmid, "r");
        } catch (IllegalArgumentException iae) {
            throw new IOException(iae.getMessage());
        }
120 121
        List counters =
                new PerfInstrumentation(bb).findByPattern(CONNECTOR_ADDRESS_COUNTER);
D
duke 已提交
122 123
        Iterator i = counters.iterator();
        if (i.hasNext()) {
124 125
            Counter c = (Counter) i.next();
            return (String) c.getValue();
D
duke 已提交
126 127 128 129 130
        } else {
            return null;
        }
    }

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    /**
     * Exports the specified remote connector address and associated
     * configuration properties to the instrumentation buffer so that
     * it can be read by this or other Java virtual machines running
     * on the same system.
     *
     * @param properties The remote connector address properties.
     */
    public static void exportRemote(Map<String, String> properties) {
        final int index = counter.getAndIncrement();
        Perf perf = Perf.getPerf();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            perf.createString(REMOTE_CONNECTOR_COUNTER_PREFIX + index + "." +
                    entry.getKey(), 1, Units.STRING.intValue(), entry.getValue());
        }
    }

    /**
     * Imports the remote connector address and associated
     * configuration properties from the instrument buffer
     * of the specified Java virtual machine.
     *
     * @param vmid an identifier that uniquely identifies a local Java virtual
     * machine, or <code>0</code> to indicate the current Java virtual machine.
     *
     * @return a map containing the remote connector's properties, or an empty
     * map if the target VM has not exported the remote connector's properties.
     *
     * @throws IOException An I/O error occurred while trying to acquire the
     * instrumentation buffer.
     */
    public static Map<String, String> importRemoteFrom(int vmid) throws IOException {
        Perf perf = Perf.getPerf();
        ByteBuffer bb;
        try {
            bb = perf.attach(vmid, "r");
        } catch (IllegalArgumentException iae) {
            throw new IOException(iae.getMessage());
        }
        List counters = new PerfInstrumentation(bb).getAllCounters();
        Map<String, String> properties = new HashMap<String, String>();
        for (Object c : counters) {
            String name = ((Counter) c).getName();
            if (name.startsWith(REMOTE_CONNECTOR_COUNTER_PREFIX) &&
                    !name.equals(CONNECTOR_ADDRESS_COUNTER)) {
                properties.put(name, ((Counter) c).getValue().toString());
            }
        }
        return properties;
    }
D
duke 已提交
181
}