All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.co.bssd.jmx.RemoteManagementBeanServerFactory Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.bssd.jmx;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public final class RemoteManagementBeanServerFactory {

	private RemoteManagementBeanServerFactory() {
		super();
	}

	public static ManagementBeanServer managementBeanServer(String hostname,
			int port) {
		String url = connectionUrl(hostname, port);
		MBeanServerConnection connection = mbeanServerConnection(url);
		return new ManagementBeanServer(connection);
	}

	private static MBeanServerConnection mbeanServerConnection(String url) {
		try {
			JMXServiceURL jmxServiceUrl = new JMXServiceURL(url);
			JMXConnector jmxConnector = JMXConnectorFactory
					.connect(jmxServiceUrl);
			return jmxConnector.getMBeanServerConnection();
		} catch (Exception e) {
			throw new JmxException("Unable to obtain connection to url '" + url
					+ "'", e);
		}
	}

	private static String connectionUrl(String hostname, int port) {
		return String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi",
				hostname, port);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy