Example usage for javax.management.remote JMXAuthenticator JMXAuthenticator

List of usage examples for javax.management.remote JMXAuthenticator JMXAuthenticator

Introduction

In this page you can find the example usage for javax.management.remote JMXAuthenticator JMXAuthenticator.

Prototype

JMXAuthenticator

Source Link

Usage

From source file:org.jcommon.com.util.jmx.RmiAdptor.java

public void setCserver(MBeanServer server) throws MalformedURLException, IOException {
    if (port == 0 || name == null || addr == null) {
        throw new NullPointerException("data not be ready");
    }/*from w  w  w  .j ava  2  s . c o  m*/
    try {
        registry = LocateRegistry.createRegistry(port);
    } catch (RemoteException e) {

    }
    HashMap<String, Object> prop = new HashMap<String, Object>();
    if (CREDENTIALS != null) {
        authenticator = new JMXAuthenticator() {

            public Subject authenticate(Object credentials) {
                logger.info(credentials.getClass().getName() + " is trying connect...");
                if (credentials instanceof String) {
                    if (credentials.equals(CREDENTIALS)) {
                        return new Subject();
                    }
                } else if (credentials instanceof String[]) {
                    String[] copy = (String[]) credentials;
                    String username = copy.length > 0 ? copy[0] : null;
                    String passwd = copy.length > 1 ? copy[1] : null;
                    logger.info(username + " is trying connect...");
                    if (passwd.equals(CREDENTIALS) && username.equals(user)) {
                        return new Subject();
                    }
                }
                throw new SecurityException("not authicated");
            }
        };

        prop.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
    }
    String url = "service:jmx:rmi:///jndi/rmi://" + addr + ":" + port + "/" + name;
    this.cserver = JMXConnectorServerFactory.newJMXConnectorServer(new JMXServiceURL(url), prop, server);
}