Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/ConnectorAddressLink.java
38827 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import java.io.IOException;28import java.nio.ByteBuffer;29import java.util.HashMap;30import java.util.Iterator;31import java.util.List;32import java.util.Map;33import java.util.concurrent.atomic.AtomicInteger;3435import sun.misc.Perf;36import sun.management.counter.Units;37import sun.management.counter.Counter;38import sun.management.counter.perf.PerfInstrumentation;3940/**41* A utility class to support the exporting and importing of the address42* of a connector server using the instrumentation buffer.43*44* @since 1.545*/46public class ConnectorAddressLink {4748private static final String CONNECTOR_ADDRESS_COUNTER =49"sun.management.JMXConnectorServer.address";5051/*52* The format of the jvmstat counters representing the properties of53* a given out-of-the-box JMX remote connector will be as follows:54*55* sun.management.JMXConnectorServer.<counter>.<key>=<value>56*57* where:58*59* counter = index computed by this class which uniquely identifies60* an out-of-the-box JMX remote connector running in this61* Java virtual machine.62* key/value = a given key/value pair in the map supplied to the63* exportRemote() method.64*65* For example,66*67* sun.management.JMXConnectorServer.0.remoteAddress=service:jmx:rmi:///jndi/rmi://myhost:5000/jmxrmi68* sun.management.JMXConnectorServer.0.authenticate=false69* sun.management.JMXConnectorServer.0.ssl=false70* sun.management.JMXConnectorServer.0.sslRegistry=false71* sun.management.JMXConnectorServer.0.sslNeedClientAuth=false72*/73private static final String REMOTE_CONNECTOR_COUNTER_PREFIX =74"sun.management.JMXConnectorServer.";7576/*77* JMX remote connector counter (it will be incremented every78* time a new out-of-the-box JMX remote connector is created).79*/80private static AtomicInteger counter = new AtomicInteger();8182/**83* Exports the specified connector address to the instrumentation buffer84* so that it can be read by this or other Java virtual machines running85* on the same system.86*87* @param address The connector address.88*/89public static void export(String address) {90if (address == null || address.length() == 0) {91throw new IllegalArgumentException("address not specified");92}93Perf perf = Perf.getPerf();94perf.createString(95CONNECTOR_ADDRESS_COUNTER, 1, Units.STRING.intValue(), address);96}9798/**99* Imports the connector address from the instrument buffer100* of the specified Java virtual machine.101*102* @param vmid an identifier that uniquely identifies a local Java virtual103* machine, or <code>0</code> to indicate the current Java virtual machine.104*105* @return the value of the connector address, or <code>null</code> if the106* target VM has not exported a connector address.107*108* @throws IOException An I/O error occurred while trying to acquire the109* instrumentation buffer.110*/111public static String importFrom(int vmid) throws IOException {112Perf perf = Perf.getPerf();113ByteBuffer bb;114try {115bb = perf.attach(vmid, "r");116} catch (IllegalArgumentException iae) {117throw new IOException(iae.getMessage());118}119List<Counter> counters =120new PerfInstrumentation(bb).findByPattern(CONNECTOR_ADDRESS_COUNTER);121Iterator<Counter> i = counters.iterator();122if (i.hasNext()) {123Counter c = i.next();124return (String) c.getValue();125} else {126return null;127}128}129130/**131* Exports the specified remote connector address and associated132* configuration properties to the instrumentation buffer so that133* it can be read by this or other Java virtual machines running134* on the same system.135*136* @param properties The remote connector address properties.137*/138public static void exportRemote(Map<String, String> properties) {139final int index = counter.getAndIncrement();140Perf perf = Perf.getPerf();141for (Map.Entry<String, String> entry : properties.entrySet()) {142perf.createString(REMOTE_CONNECTOR_COUNTER_PREFIX + index + "." +143entry.getKey(), 1, Units.STRING.intValue(), entry.getValue());144}145}146147/**148* Imports the remote connector address and associated149* configuration properties from the instrument buffer150* of the specified Java virtual machine.151*152* @param vmid an identifier that uniquely identifies a local Java virtual153* machine, or <code>0</code> to indicate the current Java virtual machine.154*155* @return a map containing the remote connector's properties, or an empty156* map if the target VM has not exported the remote connector's properties.157*158* @throws IOException An I/O error occurred while trying to acquire the159* instrumentation buffer.160*/161public static Map<String, String> importRemoteFrom(int vmid) throws IOException {162Perf perf = Perf.getPerf();163ByteBuffer bb;164try {165bb = perf.attach(vmid, "r");166} catch (IllegalArgumentException iae) {167throw new IOException(iae.getMessage());168}169List<Counter> counters = new PerfInstrumentation(bb).getAllCounters();170Map<String, String> properties = new HashMap<>();171for (Counter c : counters) {172String name = c.getName();173if (name.startsWith(REMOTE_CONNECTOR_COUNTER_PREFIX) &&174!name.equals(CONNECTOR_ADDRESS_COUNTER)) {175properties.put(name, c.getValue().toString());176}177}178return properties;179}180}181182183