blob: 58b1b3f5a70c8f8b626fc60b8b54f25b2fc51159 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.management;
27
28import java.nio.ByteBuffer;
29import java.util.ArrayList;
30import java.util.List;
31import java.util.Iterator;
32import java.util.Set;
33import java.io.IOException;
34
35import sun.misc.Perf;
36import sun.management.counter.Units;
37import sun.management.counter.Counter;
38import sun.management.counter.perf.PerfInstrumentation;
39
40/**
41 * A utility class to support the exporting and importing of the address
42 * of a connector server using the instrumentation buffer.
43 *
44 * @since 1.5
45 */
46public class ConnectorAddressLink {
47
48 private static final String CONNECTOR_ADDRESS_COUNTER =
49 "sun.management.JMXConnectorServer.address";
50
51 /**
52 * Exports the specified connector address to the instrumentation buffer
53 * so that it can be read by this or other Java virtual machines running
54 * on the same system.
55 *
56 * @param address The connector address.
57 */
58 public static void export(String address) {
59 if (address == null || address.length() == 0) {
60 throw new IllegalArgumentException("address not specified");
61 }
62 Perf perf = Perf.getPerf();
63 perf.createString(CONNECTOR_ADDRESS_COUNTER, 1, Units.STRING.intValue(), address);
64 }
65
66 /**
67 * Imports the connector address from the instrument buffer
68 * of the specified Java virtual machine.
69 *
70 * @param vmid an identifier that uniquely identifies a local
71 * Java virtual machine, or <code>0</code> to indicate
72 * the current Java virtual machine.
73 *
74 * @return the value of the connector address, or <code>null</code>
75 * if the target VM has not exported a connector address.
76 *
77 * @throws IOException An I/O error occurred while trying to acquire
78 * the instrumentation buffer.
79 */
80 public static String importFrom(int vmid) throws IOException {
81 Perf perf = Perf.getPerf();
82 ByteBuffer bb;
83 try {
84 bb = perf.attach(vmid, "r");
85 } catch (IllegalArgumentException iae) {
86 throw new IOException(iae.getMessage());
87 }
88 List counters = (new PerfInstrumentation(bb)).findByPattern(CONNECTOR_ADDRESS_COUNTER);
89 Iterator i = counters.iterator();
90 if (i.hasNext()) {
91 Counter c = (Counter)i.next();
92 return (String)c.getValue();
93 } else {
94 return null;
95 }
96 }
97
98}