0N/A/*
5266N/A * Copyright (c) 2012, 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
0N/A * published by the Free Software Foundation.
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/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FilenameFilter;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.rmi.registry.Registry;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/Aimport java.util.Properties;
0N/Aimport java.util.Set;
0N/A
0N/Aimport javax.management.*;
0N/Aimport javax.management.remote.*;
0N/A
0N/Aimport sun.management.AgentConfigurationError;
0N/Aimport sun.management.jmxremote.ConnectorBootstrap;
0N/A
0N/Apublic class JMXStartStopTest {
0N/A
0N/A static boolean verbose = false;
0N/A
0N/A static void dbg_print(String msg){
0N/A if (verbose) {
0N/A System.err.println("DBG: " +msg);
0N/A }
0N/A }
0N/A
0N/A static void dbg_print(String msg, Throwable ex){
0N/A if (verbose) {
0N/A System.err.println("DBG: " + msg + " " + ex.getMessage() );
5266N/A ex.printStackTrace(System.err);
5266N/A }
0N/A }
0N/A
0N/A public static int listMBeans(MBeanServerConnection server, ObjectName pattern, QueryExp query)
0N/A throws Exception {
5266N/A
0N/A Set names = server.queryNames(pattern,query);
0N/A for (Iterator i=names.iterator(); i.hasNext(); ) {
0N/A ObjectName name = (ObjectName)i.next();
0N/A MBeanInfo info = server.getMBeanInfo(name);
0N/A dbg_print("Got MBean: " + name);
0N/A
0N/A MBeanAttributeInfo[] attrs = info.getAttributes();
0N/A if (attrs == null)
0N/A continue;
0N/A
0N/A for (int j=0; j<attrs.length; j++) {
0N/A if (attrs[j].isReadable()) {
0N/A Object o = server.getAttribute(name,attrs[j].getName());
0N/A }
0N/A }
0N/A }
0N/A return names.size();
0N/A }
0N/A
0N/A
0N/A public void run_local(String strPid)
0N/A throws Exception {
5266N/A
0N/A String jmxUrlStr = null;
0N/A int pid = Integer.parseInt(strPid);
0N/A
0N/A try {
0N/A jmxUrlStr = sun.management.ConnectorAddressLink.importFrom(pid);
0N/A dbg_print("Local Service URL: " +jmxUrlStr);
0N/A if ( jmxUrlStr == null ) {
0N/A throw new Exception("No Service URL. Local agent not started?");
0N/A }
0N/A
0N/A JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
0N/A Map m = new HashMap();
0N/A
0N/A JMXConnector c = JMXConnectorFactory.connect(url,m);
0N/A
0N/A MBeanServerConnection conn = c.getMBeanServerConnection();
0N/A ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
0N/A
0N/A int count = listMBeans(conn,pattern,null);
0N/A if (count == 0)
throw new Exception("Expected at least one matching "+ "MBean for "+pattern);
} catch (IOException e) {
dbg_print("Cannot find process : " + pid);
throw e;
}
}
public void run(String args[]) throws Exception {
dbg_print("RmiRegistry lookup...");
int port = 4567;
if (args != null && args.length > 0) {
port = Integer.parseInt(args[0]);
}
dbg_print("Using port: " + port);
int rmiPort = 0;
if (args != null && args.length > 1) {
rmiPort = Integer.parseInt(args[1]);
}
dbg_print("Using rmi port: " + rmiPort);
Registry registry = LocateRegistry.getRegistry(port);
// "jmxrmi"
String[] relist = registry.list();
for (int i = 0; i < relist.length; ++i) {
dbg_print("Got registry: " + relist[i]);
}
String jmxUrlStr = (rmiPort != 0) ?
String.format("service:jmx:rmi://localhost:%d/jndi/rmi://localhost:%d/jmxrmi", rmiPort, port) :
String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",port);
JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
Map m = new HashMap();
JMXConnector c = JMXConnectorFactory.connect(url,m);
MBeanServerConnection conn = c.getMBeanServerConnection();
ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
int count = listMBeans(conn,pattern,null);
if (count == 0)
throw new Exception("Expected at least one matching "+ "MBean for "+pattern);
}
public static void main(String args[]) {
JMXStartStopTest manager = new JMXStartStopTest();
try {
if (args!=null && args[0].equals("local")) {
manager.run_local(args[1]);
} else {
manager.run(args);
}
} catch (RuntimeException r) {
dbg_print("No connection: ", r);
System.out.println("NO_CONN");
System.exit(1);
} catch (Throwable t) {
dbg_print("No connection: ", t);
System.out.println("NO_CONN");
System.exit(2);
}
System.out.println("OK_CONN");
System.exit(0);
}
}