1616 */
1717package com .performizeit .jmxsupport ;
1818
19- import com .sun .tools .attach .AgentInitializationException ;
20- import com .sun .tools .attach .AgentLoadException ;
21- import com .sun .tools .attach .AttachNotSupportedException ;
22- import java .io .File ;
19+
2320import java .io .IOException ;
24- import java .lang .reflect .Method ;
2521import java .net .MalformedURLException ;
26- import java .net .URL ;
27- import java .net .URLClassLoader ;
2822import java .util .HashMap ;
2923import java .util .Map ;
3024import java .util .logging .Level ;
@@ -48,9 +42,7 @@ public class JMXConnection {
4842 String userName = "" ;
4943 String userPassword = "" ;
5044 JMXServiceURL serviceURL ;
51- private static final String CONNECTOR_ADDRESS =
52- "com.sun.management.jmxremote.localConnectorAddress" ;
53- private String connectURL ;
45+ private final String connectURL ;
5446 private boolean originalThreadContentionEnabledValue ;
5547
5648 public boolean isOriginalThreadContentionEnabledValue () {
@@ -61,38 +53,7 @@ public void setOriginalThreadContentionEnabledValue(boolean originalThreadConten
6153 this .originalThreadContentionEnabledValue = originalThreadContentionEnabledValue ;
6254 }
6355
64- public JMXConnection (String pid ) throws AttachNotSupportedException , IOException , AgentLoadException , AgentInitializationException {
65- addToolsJar ();
66- connectURL = pid ;
67- // attach to the target application
68- com .sun .tools .attach .VirtualMachine vm =
69- com .sun .tools .attach .VirtualMachine .attach (pid .toString ());
70- JMXServiceURL u ;
71- try {
72- // get the connector address
73- String connectorAddress =
74- vm .getAgentProperties ().getProperty (CONNECTOR_ADDRESS );
75-
76- // no connector address, so we start the JMX agent
77- if (connectorAddress == null ) {
78- String agent = vm .getSystemProperties ().getProperty ("java.home" )
79- + File .separator + "lib" + File .separator
80- + "management-agent.jar" ;
81- vm .loadAgent (agent );
82-
83- // agent is started, get the connector address
84- connectorAddress =
85- vm .getAgentProperties ().getProperty (CONNECTOR_ADDRESS );
86- }
87-
88- // establish connection to connector server
89- // System.out.println(connectorAddress);
90- serviceURL = new JMXServiceURL (connectorAddress );
9156
92- } finally {
93- vm .detach ();
94- }
95- }
9657
9758 public JMXConnection (String serverUrl , String passwd ) throws MalformedURLException {
9859
@@ -114,11 +75,11 @@ public String getConnectURL() {
11475 return connectURL ;
11576 }
11677
117- public MBeanServerConnection getServerConnection () throws MalformedURLException , IOException {
78+ public MBeanServerConnection getServerConnection () throws IOException {
11879 if (server == null ) {
11980
120- Map env = new HashMap ();
121- if (userName .length () > 0 ) {
81+ Map < String , String []> env = new HashMap <> ();
82+ if (! userName .isEmpty () ) {
12283 String [] creds = {userName , userPassword };
12384 env .put (JMXConnector .CREDENTIALS , creds );
12485 }
@@ -136,9 +97,7 @@ public MBeanServerConnection getServerConnection() throws MalformedURLException,
13697 RUNTIME = new ObjectName ("java.lang:type=Runtime" );
13798 GC = new ObjectName ("java.lang:type=GarbageCollector,name=*" );
13899 THREADING = new ObjectName ("java.lang:type=Threading" );
139- } catch (MalformedObjectNameException ex ) {
140- Logger .getLogger (JMXConnection .class .getName ()).log (Level .SEVERE , null , ex );
141- } catch (NullPointerException ex ) {
100+ } catch (MalformedObjectNameException | NullPointerException ex ) {
142101 Logger .getLogger (JMXConnection .class .getName ()).log (Level .SEVERE , null , ex );
143102 }
144103 }
@@ -149,59 +108,39 @@ public long getUptime() {
149108
150109 l = (Long ) getServerConnection ().getAttribute (JMXConnection .RUNTIME , "Uptime" );
151110
152- } catch (MBeanException ex ) {
153- Logger .getLogger (JMXConnection .class .getName ()).log (Level .SEVERE , null , ex );
154- } catch (AttributeNotFoundException ex ) {
155- Logger .getLogger (JMXConnection .class .getName ()).log (Level .SEVERE , null , ex );
156- } catch (InstanceNotFoundException ex ) {
157- Logger .getLogger (JMXConnection .class .getName ()).log (Level .SEVERE , null , ex );
158- } catch (ReflectionException ex ) {
159- Logger .getLogger (JMXConnection .class .getName ()).log (Level .SEVERE , null , ex );
160- } catch (IOException ex ) {
111+ } catch (MBeanException | IOException | ReflectionException | InstanceNotFoundException |
112+ AttributeNotFoundException ex ) {
161113 Logger .getLogger (JMXConnection .class .getName ()).log (Level .SEVERE , null , ex );
162114 }
163115 return l ;
164116 }
165117
166- public static float inSecsTimestamp (long ts ) {
167- return ((float ) ts ) / 1000 ;
168-
169- }
170-
171- boolean isUseAuthentication () {
172- return !userName .isEmpty ();
173- }
174-
175118 public CompositeData [] getThreads (long [] thIds , int stackTraceEntriesNo ) throws Exception {
176119 String [] signature = {"[J" , "int" };
177120 Object [] params = {thIds , stackTraceEntriesNo };
178- CompositeData [] threads = (CompositeData []) server .invoke (THREADING , "getThreadInfo" , params , signature );
179- return threads ;
121+ return (CompositeData []) server .invoke (THREADING , "getThreadInfo" , params , signature );
180122 }
181123
182124 public long [] getThreadsCPU (long [] thIds ) throws Exception {
183125
184126 String [] signature = {"[J" };
185127 Object [] params = {thIds };
186- long [] threadsCPU = (long []) server .invoke (THREADING , "getThreadCpuTime" , params , signature );
187- return threadsCPU ;
128+ return (long []) server .invoke (THREADING , "getThreadCpuTime" , params , signature );
188129 }
189130
190131 public long getThreadCPU (long thId ) throws Exception {
191132
192133 String [] signature = {"long" };
193134 Object [] params = {thId };
194- long threadCPU = (Long ) server .invoke (THREADING , "getThreadCpuTime" , params , signature );
195- return threadCPU ;
135+ return (long ) (Long ) server .invoke (THREADING , "getThreadCpuTime" , params , signature );
196136
197137 }
198138
199139 public long [] getThreadsAllocBytes (long [] thIds ) throws Exception {
200140
201141 String [] signature = {"[J" };
202142 Object [] params = {thIds };
203- long [] threadsAllocBytes = (long []) server .invoke (THREADING , "getThreadAllocatedBytes" , params , signature );
204- return threadsAllocBytes ;
143+ return (long []) server .invoke (THREADING , "getThreadAllocatedBytes" , params , signature );
205144 }
206145 private boolean supportAdvFeatures = true ;
207146
@@ -213,47 +152,4 @@ public void unsetJava16_25andAbove() {
213152 supportAdvFeatures = false ;
214153 }
215154
216- public static Class addToolsJar () {
217- try {
218- return com .sun .tools .attach .VirtualMachine .class ;
219- } catch (Throwable t ) {
220- System .out .println ("tools.jar not in class path from" + System .getProperty ("java.home" ));
221- File toolsJar = new File (System .getProperty ("java.home" ) + "/lib/tools.jar" ); //when jdk
222- System .out .println ("try:" + toolsJar );
223- if (toolsJar .exists ()) {
224- addURL (toolsJar );
225- System .out .println (toolsJar );
226- } else {
227- toolsJar = new File (System .getProperty ("java.home" ) + "/../lib/tools.jar" ); // when jre part of jdk
228- System .out .println ("try:" + toolsJar );
229- if (toolsJar .exists ()) {
230- addURL (toolsJar );
231- System .out .println ("Found:" +toolsJar );
232- } else {
233- System .out .println ("Unable to locate tools.jar pls add it to classpath" );
234- }
235- }
236- }
237- return com .sun .tools .attach .VirtualMachine .class ;
238-
239-
240-
241- }
242-
243- public static void addURL (File file ) throws RuntimeException {
244- try {
245- URL url = file .toURL ();
246- URLClassLoader classLoader = (URLClassLoader ) ClassLoader .getSystemClassLoader ();
247- Class clazz = URLClassLoader .class ;
248-
249- // Use reflection
250- Method method = clazz .getDeclaredMethod ("addURL" , new Class []{URL .class });
251- method .setAccessible (true );
252- method .invoke (classLoader , new Object []{url });
253-
254- } catch (Exception e ) {
255- throw new RuntimeException (e );
256-
257- }
258- }
259155}
0 commit comments