author | mchung |
Tue, 17 Jan 2012 15:55:40 -0800 | |
changeset 11530 | a9d059c15b80 |
parent 5506 | 202f599c92aa |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.management; |
|
27 |
||
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
28 |
import java.io.IOException; |
2 | 29 |
import java.nio.ByteBuffer; |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
30 |
import java.util.HashMap; |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
31 |
import java.util.Iterator; |
2 | 32 |
import java.util.List; |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
33 |
import java.util.Map; |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
34 |
import java.util.concurrent.atomic.AtomicInteger; |
2 | 35 |
|
36 |
import sun.misc.Perf; |
|
37 |
import sun.management.counter.Units; |
|
38 |
import sun.management.counter.Counter; |
|
39 |
import sun.management.counter.perf.PerfInstrumentation; |
|
40 |
||
41 |
/** |
|
42 |
* A utility class to support the exporting and importing of the address |
|
43 |
* of a connector server using the instrumentation buffer. |
|
44 |
* |
|
45 |
* @since 1.5 |
|
46 |
*/ |
|
47 |
public class ConnectorAddressLink { |
|
48 |
||
49 |
private static final String CONNECTOR_ADDRESS_COUNTER = |
|
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
50 |
"sun.management.JMXConnectorServer.address"; |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
51 |
|
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
52 |
/* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
53 |
* The format of the jvmstat counters representing the properties of |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
54 |
* a given out-of-the-box JMX remote connector will be as follows: |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
55 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
56 |
* sun.management.JMXConnectorServer.<counter>.<key>=<value> |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
57 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
58 |
* where: |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
59 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
60 |
* counter = index computed by this class which uniquely identifies |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
61 |
* an out-of-the-box JMX remote connector running in this |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
62 |
* Java virtual machine. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
63 |
* key/value = a given key/value pair in the map supplied to the |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
64 |
* exportRemote() method. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
65 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
66 |
* For example, |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
67 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
68 |
* sun.management.JMXConnectorServer.0.remoteAddress=service:jmx:rmi:///jndi/rmi://myhost:5000/jmxrmi |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
69 |
* sun.management.JMXConnectorServer.0.authenticate=false |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
70 |
* sun.management.JMXConnectorServer.0.ssl=false |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
71 |
* sun.management.JMXConnectorServer.0.sslRegistry=false |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
72 |
* sun.management.JMXConnectorServer.0.sslNeedClientAuth=false |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
73 |
*/ |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
74 |
private static final String REMOTE_CONNECTOR_COUNTER_PREFIX = |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
75 |
"sun.management.JMXConnectorServer."; |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
76 |
|
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
77 |
/* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
78 |
* JMX remote connector counter (it will be incremented every |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
79 |
* time a new out-of-the-box JMX remote connector is created). |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
80 |
*/ |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
81 |
private static AtomicInteger counter = new AtomicInteger(); |
2 | 82 |
|
83 |
/** |
|
84 |
* Exports the specified connector address to the instrumentation buffer |
|
85 |
* so that it can be read by this or other Java virtual machines running |
|
86 |
* on the same system. |
|
87 |
* |
|
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
88 |
* @param address The connector address. |
2 | 89 |
*/ |
90 |
public static void export(String address) { |
|
91 |
if (address == null || address.length() == 0) { |
|
92 |
throw new IllegalArgumentException("address not specified"); |
|
93 |
} |
|
94 |
Perf perf = Perf.getPerf(); |
|
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
95 |
perf.createString( |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
96 |
CONNECTOR_ADDRESS_COUNTER, 1, Units.STRING.intValue(), address); |
2 | 97 |
} |
98 |
||
99 |
/** |
|
100 |
* Imports the connector address from the instrument buffer |
|
101 |
* of the specified Java virtual machine. |
|
102 |
* |
|
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
103 |
* @param vmid an identifier that uniquely identifies a local Java virtual |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
104 |
* machine, or <code>0</code> to indicate the current Java virtual machine. |
2 | 105 |
* |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
106 |
* @return the value of the connector address, or <code>null</code> if the |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
107 |
* target VM has not exported a connector address. |
2 | 108 |
* |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
109 |
* @throws IOException An I/O error occurred while trying to acquire the |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
110 |
* instrumentation buffer. |
2 | 111 |
*/ |
112 |
public static String importFrom(int vmid) throws IOException { |
|
113 |
Perf perf = Perf.getPerf(); |
|
114 |
ByteBuffer bb; |
|
115 |
try { |
|
116 |
bb = perf.attach(vmid, "r"); |
|
117 |
} catch (IllegalArgumentException iae) { |
|
118 |
throw new IOException(iae.getMessage()); |
|
119 |
} |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
120 |
List<Counter> counters = |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
121 |
new PerfInstrumentation(bb).findByPattern(CONNECTOR_ADDRESS_COUNTER); |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
122 |
Iterator<Counter> i = counters.iterator(); |
2 | 123 |
if (i.hasNext()) { |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
124 |
Counter c = i.next(); |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
125 |
return (String) c.getValue(); |
2 | 126 |
} else { |
127 |
return null; |
|
128 |
} |
|
129 |
} |
|
130 |
||
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
131 |
/** |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
132 |
* Exports the specified remote connector address and associated |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
133 |
* configuration properties to the instrumentation buffer so that |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
134 |
* it can be read by this or other Java virtual machines running |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
135 |
* on the same system. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
136 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
137 |
* @param properties The remote connector address properties. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
138 |
*/ |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
139 |
public static void exportRemote(Map<String, String> properties) { |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
140 |
final int index = counter.getAndIncrement(); |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
141 |
Perf perf = Perf.getPerf(); |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
142 |
for (Map.Entry<String, String> entry : properties.entrySet()) { |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
143 |
perf.createString(REMOTE_CONNECTOR_COUNTER_PREFIX + index + "." + |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
144 |
entry.getKey(), 1, Units.STRING.intValue(), entry.getValue()); |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
145 |
} |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
146 |
} |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
147 |
|
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
148 |
/** |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
149 |
* Imports the remote connector address and associated |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
150 |
* configuration properties from the instrument buffer |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
151 |
* of the specified Java virtual machine. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
152 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
153 |
* @param vmid an identifier that uniquely identifies a local Java virtual |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
154 |
* machine, or <code>0</code> to indicate the current Java virtual machine. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
155 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
156 |
* @return a map containing the remote connector's properties, or an empty |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
157 |
* map if the target VM has not exported the remote connector's properties. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
158 |
* |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
159 |
* @throws IOException An I/O error occurred while trying to acquire the |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
160 |
* instrumentation buffer. |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
161 |
*/ |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
162 |
public static Map<String, String> importRemoteFrom(int vmid) throws IOException { |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
163 |
Perf perf = Perf.getPerf(); |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
164 |
ByteBuffer bb; |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
165 |
try { |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
166 |
bb = perf.attach(vmid, "r"); |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
167 |
} catch (IllegalArgumentException iae) { |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
168 |
throw new IOException(iae.getMessage()); |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
169 |
} |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
170 |
List<Counter> counters = new PerfInstrumentation(bb).getAllCounters(); |
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
171 |
Map<String, String> properties = new HashMap<>(); |
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
172 |
for (Counter c : counters) { |
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
173 |
String name = c.getName(); |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
174 |
if (name.startsWith(REMOTE_CONNECTOR_COUNTER_PREFIX) && |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
175 |
!name.equals(CONNECTOR_ADDRESS_COUNTER)) { |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
176 |
properties.put(name, c.getValue().toString()); |
52
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
177 |
} |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
178 |
} |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
179 |
return properties; |
752ebbd838ad
4981215: Publishing a port number for management console to access
lmalvent
parents:
2
diff
changeset
|
180 |
} |
2 | 181 |
} |