javax.management.remote.JMXServiceURL Java Examples

The following examples show how to use javax.management.remote.JMXServiceURL. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestManager.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void connect(String pid, String address) throws Exception {
    if (address == null) {
        throw new RuntimeException("Local connector address for " +
                                   pid + " is null");
    }

    System.out.println("Connect to process " + pid + " via: " + address);

    JMXServiceURL url = new JMXServiceURL(address);
    JMXConnector c = JMXConnectorFactory.connect(url);
    MBeanServerConnection server = c.getMBeanServerConnection();

    System.out.println("Connected.");

    RuntimeMXBean rt = newPlatformMXBeanProxy(server,
        RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    System.out.println(rt.getName());

    // close the connection
    c.close();
}
 
Example #2
Source File: RMIConnectorLogAttributesTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private JMXConnectorServer startServer(int rmiPort) throws Exception {
    System.out.println("DEBUG: Create RMI registry on port " + rmiPort);
    LocateRegistry.createRegistry(rmiPort);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    HashMap<String,Object> env = new HashMap<String,Object>();

    JMXServiceURL url =
            new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:" + rmiPort + "/jmxrmi");
    JMXConnectorServer cs =
            JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);

    cs.start();
    System.out.println("DEBUG: Started the RMI connector server");
    return cs;
}
 
Example #3
Source File: ProviderTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void dotest(JMXServiceURL url, MBeanServer mbs)
    throws Exception {
    JMXConnectorServer server = null;
    JMXConnector client = null;

    server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    server.start();
    JMXServiceURL outputAddr = server.getAddress();
    System.out.println("Server started ["+ outputAddr+ "]");

    client = JMXConnectorFactory.newJMXConnector(outputAddr, null);

    client.connect();
    System.out.println("Client connected");

    MBeanServerConnection connection
        = client.getMBeanServerConnection();

    System.out.println(connection.getDefaultDomain());
}
 
Example #4
Source File: VirtualMachineMetrics.java    From spring-init with Apache License 2.0 6 votes vote down vote up
public static Map<String, Long> fetch(String pid) {
	if (pid == null) {
		return Collections.emptyMap();
	}
	try {
		VirtualMachine vm = VirtualMachine.attach(pid);
		vm.startLocalManagementAgent();
		String connectorAddress = vm.getAgentProperties()
				.getProperty(CONNECTOR_ADDRESS);
		JMXServiceURL url = new JMXServiceURL(connectorAddress);
		JMXConnector connector = JMXConnectorFactory.connect(url);
		MBeanServerConnection connection = connector.getMBeanServerConnection();
		gc(connection);
		Map<String, Long> metrics = new HashMap<>(
				new BufferPools(connection).getMetrics());
		metrics.putAll(new Threads(connection).getMetrics());
		metrics.putAll(new Classes(connection).getMetrics());
		vm.detach();
		return metrics;
	}
	catch (Exception e) {
		return Collections.emptyMap();
	}
}
 
Example #5
Source File: SecurityTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private JMXServiceURL createServerSide(Map<String, Object> serverMap)
throws Exception {
    final int NINETY_SECONDS = 90;

    System.out.println("SecurityTest::createServerSide: Start") ;

    // Prepare server side security env
    HashMap<String, Object> env = setServerSecurityEnv(serverMap);

    // Create and start mbean server and connector server
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
    cs.start();

    // Waits availibility of connector server
    Utils.waitReady(cs, NINETY_SECONDS);

    JMXServiceURL addr = cs.getAddress();

    System.out.println("SecurityTest::createServerSide: Done.") ;

    return addr;
}
 
Example #6
Source File: RMIConnector.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
private RMIServer findRMIServer(JMXServiceURL directoryURL,
        Map<String, Object> environment)
        throws NamingException, IOException {
    final boolean isIiop = RMIConnectorServer.isIiopURL(directoryURL,true);
    if (isIiop) {
        // Make sure java.naming.corba.orb is in the Map.
        environment.put(EnvHelp.DEFAULT_ORB,resolveOrb(environment));
    }

    String path = directoryURL.getURLPath();
    int end = path.indexOf(';');
    if (end < 0) end = path.length();
    if (path.startsWith("/jndi/"))
        return findRMIServerJNDI(path.substring(6,end), environment, isIiop);
    else if (path.startsWith("/stub/"))
        return findRMIServerJRMP(path.substring(6,end), environment, isIiop);
    else if (path.startsWith("/ior/")) {
        if (!IIOPHelper.isAvailable())
            throw new IOException("iiop protocol not available");
        return findRMIServerIIOP(path.substring(5,end), environment, isIiop);
    } else {
        final String msg = "URL path must begin with /jndi/ or /stub/ " +
                "or /ior/: " + path;
        throw new MalformedURLException(msg);
    }
}
 
Example #7
Source File: JmxmpAgent.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** optionally starts a normal JMXRMI connector in addition */
public JMXConnectorServer startNormalJmxRmiConnectorIfRequested(Properties properties) {
    try {
        String rmiPortS = properties.getProperty(RMI_REGISTRY_PORT_PROPERTY);
        if (rmiPortS==null || rmiPortS.length()==0)
            return null;

        int rmiPort = Integer.parseInt(rmiPortS);
        LocateRegistry.createRegistry(rmiPort);
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        String svc =
            "service:jmx:rmi:///jndi/rmi://localhost:"+rmiPort+"/jmxrmi";

        JMXServiceURL url = new JMXServiceURL(svc);
        RMIConnectorServer rmiServer = new RMIConnectorServer(url, null, mbeanServer);
        rmiServer.start();
        return rmiServer;
    } catch (Exception e) {
        System.err.println("Unable to start JmxmpAgent: "+e);
        throw new RuntimeException(e);
    }
}
 
Example #8
Source File: RMIConnector.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private RMIServer findRMIServer(JMXServiceURL directoryURL,
        Map<String, Object> environment)
        throws NamingException, IOException {
    final boolean isIiop = RMIConnectorServer.isIiopURL(directoryURL,true);
    if (isIiop) {
        // Make sure java.naming.corba.orb is in the Map.
        environment.put(EnvHelp.DEFAULT_ORB,resolveOrb(environment));
    }

    String path = directoryURL.getURLPath();
    int end = path.indexOf(';');
    if (end < 0) end = path.length();
    if (path.startsWith("/jndi/"))
        return findRMIServerJNDI(path.substring(6,end), environment, isIiop);
    else if (path.startsWith("/stub/"))
        return findRMIServerJRMP(path.substring(6,end), environment, isIiop);
    else if (path.startsWith("/ior/")) {
        if (!IIOPHelper.isAvailable())
            throw new IOException("iiop protocol not available");
        return findRMIServerIIOP(path.substring(5,end), environment, isIiop);
    } else {
        final String msg = "URL path must begin with /jndi/ or /stub/ " +
                "or /ior/: " + path;
        throw new MalformedURLException(msg);
    }
}
 
Example #9
Source File: ProviderTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void dotest(JMXServiceURL url, MBeanServer mbs)
    throws Exception {
    JMXConnectorServer server = null;
    JMXConnector client = null;

    server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    server.start();
    JMXServiceURL outputAddr = server.getAddress();
    System.out.println("Server started ["+ outputAddr+ "]");

    client = JMXConnectorFactory.newJMXConnector(outputAddr, null);

    client.connect();
    System.out.println("Client connected");

    MBeanServerConnection connection
        = client.getMBeanServerConnection();

    System.out.println(connection.getDefaultDomain());
}
 
Example #10
Source File: JmxUtil.java    From ankush with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Jmx connection.
 * 
 * @param nodeIp
 *            the node ip
 * @param jmxPort
 *            the jmx port
 * @return the jMX connector
 * @throws Exception
 */
private JMXConnector jmxConnection(String nodeIp, int jmxPort) {
	JMXConnector connector = null;
	try {
		JMXServiceURL address = new JMXServiceURL(
				"service:jmx:rmi:///jndi/rmi://" + nodeIp + ":" + jmxPort
						+ "/jmxrmi");
		connector = JMXConnectorFactory.connect(address);
	} catch (Exception e) {
		if (e.getMessage().contains("java.net.ConnectException")) {
			logger.info("Host unavailable at Node..." + nodeIp);
			logger.error(e.getMessage());
			// throw new AnkushException("Host unavailable-" + nodeIp);
		}
	}
	return connector;
}
 
Example #11
Source File: ServerProvider.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
                                                Map<String,?> environment,
                                                MBeanServer mbeanServer)
        throws IOException {
    if (!serviceURL.getProtocol().equals("rmi")) {
        throw new MalformedURLException("Protocol not rmi: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
 
Example #12
Source File: SecurityTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void run(Map<String, Object> serverArgs, String clientArgs[]) {

        System.out.println("SecurityTest::run: Start") ;
        int errorCount = 0;

        try {
            // Initialise the server side
            JMXServiceURL urlToUse = createServerSide(serverArgs);

            // Run client side
            errorCount = runClientSide(clientArgs, urlToUse.toString());

            if ( errorCount == 0 ) {
                System.out.println("SecurityTest::run: Done without any error") ;
            } else {
                System.out.println(
                    "SecurityTest::run: Done with " + errorCount + " error(s)");
                throw new RuntimeException("errorCount = " + errorCount);
            }

            cs.stop();

        } catch(Exception e) {
            throw new RuntimeException(e);
        }

    }
 
Example #13
Source File: ServerProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
                                                Map<String,?> environment,
                                                MBeanServer mbeanServer)
        throws IOException {
    if (!serviceURL.getProtocol().equals("rmi")) {
        throw new MalformedURLException("Protocol not rmi: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
 
Example #14
Source File: RMIConnector.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private RMIConnector(RMIServer rmiServer, JMXServiceURL address,
        Map<String, ?> environment) {
    if (rmiServer == null && address == null) throw new
            IllegalArgumentException("rmiServer and jmxServiceURL both null");
    initTransients();

    this.rmiServer = rmiServer;
    this.jmxServiceURL = address;
    if (environment == null) {
        this.env = Collections.emptyMap();
    } else {
        EnvHelp.checkAttributes(environment);
        this.env = Collections.unmodifiableMap(environment);
    }
}
 
Example #15
Source File: JMXConnectorProviderImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnector newJMXConnector(JMXServiceURL url,
                                    Map<String,?> map)
    throws IOException {
    final String protocol = url.getProtocol();
    called = true;
    System.out.println("JMXConnectorProviderImpl called");

    if(protocol.equals("rmi"))
        return new RMIConnector(url, map);
    if(protocol.equals("throw-provider-exception"))
        throw new JMXProviderException("I have been asked to throw");

    throw new IllegalArgumentException("UNKNOWN PROTOCOL");
}
 
Example #16
Source File: ServerProvider.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
                                                Map<String,?> environment,
                                                MBeanServer mbeanServer)
        throws IOException {
    if (!serviceURL.getProtocol().equals("iiop")) {
        throw new MalformedURLException("Protocol not iiop: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
 
Example #17
Source File: ServerProvider.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
                                                Map<String,?> environment,
                                                MBeanServer mbeanServer)
        throws IOException {
    if (!serviceURL.getProtocol().equals("rmi")) {
        throw new MalformedURLException("Protocol not rmi: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
 
Example #18
Source File: Server.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static String start() throws Exception {
    int serverPort = 12345;
    ObjectName name = new ObjectName("test", "foo", "bar");
    MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
    SteMBean bean = new Ste();
    jmxServer.registerMBean(bean, name);
    boolean exported = false;
    Random rnd = new Random(System.currentTimeMillis());
    do {
        try {
            LocateRegistry.createRegistry(serverPort);
            exported = true;
        } catch (ExportException ee) {
            if (ee.getCause() instanceof BindException) {
                serverPort = rnd.nextInt(10000) + 4096;
            } else {
                throw ee;
            }
        }

    } while (!exported);
    JMXServiceURL serverUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + serverPort + "/test");
    JMXConnectorServer jmxConnector = JMXConnectorServerFactory.newJMXConnectorServer(serverUrl, null, jmxServer);
    jmxConnector.start();

    return serverUrl.toString();
}
 
Example #19
Source File: ClientProvider.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnector newJMXConnector(JMXServiceURL serviceURL,
                                    Map<String,?> environment)
        throws IOException {
    if (!serviceURL.getProtocol().equals("iiop")) {
        throw new MalformedURLException("Protocol not iiop: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnector(serviceURL, environment);
}
 
Example #20
Source File: ResourceManagerJMXTest.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
private void jmxAuthInvalidCreds(JMXServiceURL jmxRmiServiceURL) {
    RMTHelper.log("Test invalid JMX auth without creds (expect SecurityException)");
    try {
        JMXConnectorFactory.connect(jmxRmiServiceURL, new HashMap<String, Object>(0));
    } catch (Exception e) {
        assertTrue("JMX auth must throw SecurityException if a client tries to connect without creds in the " +
                   "env", e instanceof SecurityException);
    }
}
 
Example #21
Source File: ServerProvider.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
                                                Map<String,?> environment,
                                                MBeanServer mbeanServer)
        throws IOException {
    if (!serviceURL.getProtocol().equals("iiop")) {
        throw new MalformedURLException("Protocol not iiop: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
 
Example #22
Source File: Server.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static String start() throws Exception {
    int serverPort = 12345;
    ObjectName name = new ObjectName("test", "foo", "bar");
    MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
    SteMBean bean = new Ste();
    jmxServer.registerMBean(bean, name);
    boolean exported = false;
    Random rnd = new Random(System.currentTimeMillis());
    do {
        try {
            LocateRegistry.createRegistry(serverPort);
            exported = true;
        } catch (ExportException ee) {
            if (ee.getCause() instanceof BindException) {
                serverPort = rnd.nextInt(10000) + 4096;
            } else {
                throw ee;
            }
        }

    } while (!exported);
    JMXServiceURL serverUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + serverPort + "/test");
    JMXConnectorServer jmxConnector = JMXConnectorServerFactory.newJMXConnectorServer(serverUrl, null, jmxServer);
    jmxConnector.start();

    return serverUrl.toString();
}
 
Example #23
Source File: MBeanProcessController.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Connects to the JMX agent in the local process.
 * 
 * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector in the process
 * @throws IOException if the JDK management agent cannot be found and loaded
 */
private void connect() throws ConnectionFailedException, IOException {
  try {
    final JMXServiceURL jmxUrl = getJMXServiceURL();
    this.jmxc = JMXConnectorFactory.connect(jmxUrl);
    this.server = this.jmxc.getMBeanServerConnection();
  } catch (AttachNotSupportedException e) {
    throw new ConnectionFailedException("Failed to connect to process '" + this.pid + "'", e);
  }
}
 
Example #24
Source File: JmxRBACProviderServerGroupScopedRolesTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void test(String userName) throws Exception {
    String urlString = System.getProperty("jmx.service.url", "service:jmx:remoting-jmx://"
                    + NetworkUtils.formatPossibleIpv6Address(masterClientConfig.getHostControllerManagementAddress()) + ":12345");
    JmxManagementInterface jmx = JmxManagementInterface.create(new JMXServiceURL(urlString),
            userName, RbacAdminCallbackHandler.STD_PASSWORD,
            null // not needed, as the only thing from JmxManagementInterface used in this test is getConnection()
    );
    try {
        getAttribute(userName, jmx);
        setAttribute(userName, jmx);
        dumpServices(userName, jmx);
        operationReadOnly(userName, jmx);
        operationWriteOnly(userName, jmx);
        operationReadWrite(userName, jmx);
        operationUnknown(userName, jmx);
        deactivateMBeanSensitivity();
        getAttribute(userName, jmx);
        setAttribute(userName, jmx);
        dumpServices(userName, jmx);
        operationReadOnly(userName, jmx);
        operationWriteOnly(userName, jmx);
        operationReadWrite(userName, jmx);
        operationUnknown(userName, jmx);
    } finally {
        jmx.close();
    }
}
 
Example #25
Source File: JMXConnectWithTimeout.java    From SuitAgent with Apache License 2.0 5 votes vote down vote up
/**
 * JMX连接
 * @param url
 * JMX连接地址
 * @param jmxUser
 * JMX授权用户 null为无授权用户
 * @param jmxPassword
 * JMX授权密码 null为无授权密码
 * @param timeout
 * 超时时间
 * @param unit
 * 超时单位
 * @return
 * @throws IOException
 */
public static JMXConnector connectWithTimeout( final JMXServiceURL url,String jmxUser,String jmxPassword, long timeout, TimeUnit unit) throws Exception {
    final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1);
    ExecuteThreadUtil.execute(() -> {
        try {
            JMXConnector connector;
            if(jmxUser != null && jmxPassword != null){
                Map<String,Object> env = new HashMap<>();
                String[] credentials = new String[] { jmxUser, jmxPassword };
                env.put(JMXConnector.CREDENTIALS, credentials);
                connector = JMXConnectorFactory.connect(url,env);
            }else{
                connector = JMXConnectorFactory.connect(url,null);
            }
            if (!blockingQueue.offer(connector))
                connector.close();
        } catch (Throwable t) {
            blockingQueue.offer(t);
        }
    });

    Object result = BlockingQueueUtil.getResult(blockingQueue,timeout,unit);
    blockingQueue.clear();


    if (result instanceof JMXConnector){
        return (JMXConnector) result;
    }else if (result == null){
        throw new SocketTimeoutException("Connect timed out: " + url);
    }else if(result instanceof Throwable){
        throw new IOException("JMX Connect Failed : " + url,((Throwable) result));
    }
    return null;
}
 
Example #26
Source File: RMIConnector.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private RMIConnector(RMIServer rmiServer, JMXServiceURL address,
        Map<String, ?> environment) {
    if (rmiServer == null && address == null) throw new
            IllegalArgumentException("rmiServer and jmxServiceURL both null");
    initTransients();

    this.rmiServer = rmiServer;
    this.jmxServiceURL = address;
    if (environment == null) {
        this.env = Collections.emptyMap();
    } else {
        EnvHelp.checkAttributes(environment);
        this.env = Collections.unmodifiableMap(environment);
    }
}
 
Example #27
Source File: JMXConnectorProviderImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnector newJMXConnector(JMXServiceURL url,
                                    Map<String,?> map)
    throws IOException {
    final String protocol = url.getProtocol();
    called = true;
    System.out.println("JMXConnectorProviderImpl called");

    if(protocol.equals("rmi"))
        return new RMIConnector(url, map);
    if(protocol.equals("throw-provider-exception"))
        throw new JMXProviderException("I have been asked to throw");

    throw new IllegalArgumentException("UNKNOWN PROTOCOL");
}
 
Example #28
Source File: ServerProvider.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
                                                Map<String,?> environment,
                                                MBeanServer mbeanServer)
        throws IOException {
    if (!serviceURL.getProtocol().equals("rmi")) {
        throw new MalformedURLException("Protocol not rmi: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
 
Example #29
Source File: RMIConnectorLogAttributesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private JMXConnector connectToServer(JMXConnectorServer server) throws IOException, MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, ReflectionException, MBeanException {
    JMXServiceURL url = server.getAddress();
    Map<String, Object> env = new HashMap<String, Object>();
    JMXConnector connector = JMXConnectorFactory.connect(url, env);

    System.out.println("DEBUG: Client connected to RMI at: " + url);

    return connector;
}
 
Example #30
Source File: ClientProvider.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public JMXConnector newJMXConnector(JMXServiceURL serviceURL,
                                    Map<String,?> environment)
        throws IOException {
    if (!serviceURL.getProtocol().equals("rmi")) {
        throw new MalformedURLException("Protocol not rmi: " +
                                        serviceURL.getProtocol());
    }
    return new RMIConnector(serviceURL, environment);
}