0N/A/*
2362N/A * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.management;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.concurrent.atomic.AtomicInteger;
0N/A
0N/Aimport sun.misc.Perf;
0N/Aimport sun.management.counter.Units;
0N/Aimport sun.management.counter.Counter;
0N/Aimport sun.management.counter.perf.PerfInstrumentation;
0N/A
0N/A/**
0N/A * A utility class to support the exporting and importing of the address
0N/A * of a connector server using the instrumentation buffer.
0N/A *
0N/A * @since 1.5
0N/A */
0N/Apublic class ConnectorAddressLink {
0N/A
0N/A private static final String CONNECTOR_ADDRESS_COUNTER =
0N/A "sun.management.JMXConnectorServer.address";
0N/A
0N/A /*
0N/A * The format of the jvmstat counters representing the properties of
0N/A * a given out-of-the-box JMX remote connector will be as follows:
0N/A *
0N/A * sun.management.JMXConnectorServer.<counter>.<key>=<value>
0N/A *
0N/A * where:
0N/A *
0N/A * counter = index computed by this class which uniquely identifies
0N/A * an out-of-the-box JMX remote connector running in this
0N/A * Java virtual machine.
0N/A * key/value = a given key/value pair in the map supplied to the
0N/A * exportRemote() method.
0N/A *
0N/A * For example,
0N/A *
0N/A * sun.management.JMXConnectorServer.0.remoteAddress=service:jmx:rmi:///jndi/rmi://myhost:5000/jmxrmi
0N/A * sun.management.JMXConnectorServer.0.authenticate=false
0N/A * sun.management.JMXConnectorServer.0.ssl=false
0N/A * sun.management.JMXConnectorServer.0.sslRegistry=false
0N/A * sun.management.JMXConnectorServer.0.sslNeedClientAuth=false
0N/A */
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();
/**
* 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.
*
* @param address The connector address.
*/
public static void export(String address) {
if (address == null || address.length() == 0) {
throw new IllegalArgumentException("address not specified");
}
Perf perf = Perf.getPerf();
perf.createString(
CONNECTOR_ADDRESS_COUNTER, 1, Units.STRING.intValue(), address);
}
/**
* Imports the connector address 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 the value of the connector address, or <code>null</code> if the
* target VM has not exported a connector address.
*
* @throws IOException An I/O error occurred while trying to acquire the
* instrumentation buffer.
*/
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());
}
List counters =
new PerfInstrumentation(bb).findByPattern(CONNECTOR_ADDRESS_COUNTER);
Iterator i = counters.iterator();
if (i.hasNext()) {
Counter c = (Counter) i.next();
return (String) c.getValue();
} else {
return null;
}
}
/**
* 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;
}
}