public class JMXMPProtocolrovider implements IJMXProtocolProvider {
public JMXConnector getJMXConnector(Attribute attibute) throws Exception {
String userName = attibute.getUserName();
String userPwd = attibute.getPassword();
String host = attibute.getHost();
int port = attibute.getPort();
JMXConnector connector = null;
JMXServiceURL url = null;
try {
url = new JMXServiceURL("service:jmx:jmxmp://" + host + ":" + port);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "com.sun.jmx.remote.protocol");
env.put("jmx.remote.profiles", "SASL/PLAIN");
env.put("jmx.remote.sasl.callback.handler", new SaslUserPasswordCallbackHandler(userName, userPwd));
env.put(GenericConnector.MESSAGE_CONNECTION, new MuxSocketClientMessageConnection(url.getHost(), url.getPort()));
// install SASL/PLAIN mechanism provider
SaslClientSecurityProvider.install();
if(url != null) {
return JMXConnectorFactory.connect(url, env);
}
return null;
}
}
public class IIOPProtocolProvdier implements IJMXProtocolProvider {
private static final ObjectName RUNTIME_OBJECTNAME;
static{
try{
RUNTIME_OBJECTNAME = new ObjectName( "java.lang:type=Runtime" );
}
catch( final Exception ex ){
throw new IllegalStateException( ex );
}
}
public JMXConnector getJMXConnector(Attribute attibute) throws Exception {
String userName = attibute.getUserName();
String userPwd = attibute.getPassword();
String host = attibute.getHost();
int port = attibute.getPort();
JMXConnector connector = null;
Hashtable<String, Object> env = new Hashtable<String, Object>();
String prividerUrl = "iiop://" + host + ":" + port;
String[] credentials = new String[] { userName, userPwd };
env.clear();
env.put(Context.PROVIDER_URL, prividerUrl);
env.put(JMXConnector.CREDENTIALS, credentials);
env.put(Context.AUTHORITATIVE, userName);
env.put(Context.SECURITY_CREDENTIALS, userPwd);
try {
JMXServiceURL url = new JMXServiceURL("iiop", host, port, "/jndi/corbaname::1.2@" + host + ":" + port
+ "#jmx/rmi/RMIConnectorServer");
connector = JMXConnectorFactory.connect(url, env);
connector.connect( );
MBeanServerConnection c = connector.getMBeanServerConnection( );
c.getMBeanInfo( RUNTIME_OBJECTNAME ).getAttributes( );
} catch (MalformedURLException e) {
throw new OperationNoSuccessException("connection to connector server fail,either no legal protocol could be found in a specification string or the string could not be parsed",e);
} catch (IOException e) {
throw new OperationNoSuccessException("connection to connector server fail", e);
} catch (SecurityException e) {
throw new OperationNoSuccessException("user [" + userName + "] was not authenticated with this connection", e);
}
return connector;
}
}
取Jmx连接
最新推荐文章于 2023-12-31 13:15:00 发布