Example usage for javax.management.remote JMXConnector CREDENTIALS

List of usage examples for javax.management.remote JMXConnector CREDENTIALS

Introduction

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

Prototype

String CREDENTIALS

To view the source code for javax.management.remote JMXConnector CREDENTIALS.

Click Source Link

Document

Name of the attribute that specifies the credentials to send to the connector server during connection.

Usage

From source file:org.apache.geode.tools.pulse.internal.data.JMXDataUpdater.java

/**
 * Get the jmx connection//from   w  w  w.  j a  v a2  s .  co m
 */
public JMXConnector connect(String username, String password) {
    // Reference to repository
    Repository repository = Repository.get();
    try {

        String jmxSerURL = "";

        logger.info("{}:{}", resourceBundle.getString("LOG_MSG_USE_LOCATOR_VALUE"),
                repository.getJmxUseLocator());

        if (repository.getJmxUseLocator()) {
            JmxManagerInfo jmxManagerInfo = getManagerInfoFromLocator(repository);

            if (jmxManagerInfo.port == 0) {
                logger.info(resourceBundle.getString("LOG_MSG_LOCATOR_COULD_NOT_FIND_MANAGER"));
            } else {
                logger.info("{}: {}={} & {}={}, {}", resourceBundle.getString("LOG_MSG_LOCATOR_FOUND_MANAGER"),
                        resourceBundle.getString("LOG_MSG_HOST"), jmxManagerInfo.host,
                        resourceBundle.getString("LOG_MSG_PORT"), jmxManagerInfo.port,
                        (jmxManagerInfo.ssl ? resourceBundle.getString("LOG_MSG_WITH_SSL")
                                : resourceBundle.getString("LOG_MSG_WITHOUT_SSL")));

                jmxSerURL = formJMXServiceURLString(jmxManagerInfo.host, String.valueOf(jmxManagerInfo.port));
            }
        } else {
            logger.info("{}={} & {}={}", resourceBundle.getString("LOG_MSG_HOST"), this.serverName,
                    resourceBundle.getString("LOG_MSG_PORT"), this.port);
            jmxSerURL = formJMXServiceURLString(this.serverName, this.port);
        }

        if (StringUtils.isNotBlank(jmxSerURL)) {
            JMXServiceURL url = new JMXServiceURL(jmxSerURL);
            String[] creds = { username, password };
            Map<String, Object> env = new HashMap<String, Object>();
            env.put(JMXConnector.CREDENTIALS, creds);

            if (repository.isUseSSLManager()) {
                // use ssl to connect
                env.put("com.sun.jndi.rmi.factory.socket", new SslRMIClientSocketFactory());
            }
            logger.info("Connecting to jmxURL : {}", jmxSerURL);
            this.conn = JMXConnectorFactory.connect(url, env);
            this.mbs = this.conn.getMBeanServerConnection();
            cluster.setConnectedFlag(true);
        }
    } catch (Exception e) {
        cluster.setConnectedFlag(false);
        cluster.setConnectionErrorMsg(e.getMessage());
        logger.fatal(e.getMessage(), e);
        if (this.conn != null) {
            try {
                this.conn.close();
            } catch (Exception e1) {
                logger.fatal(e1.getMessage(), e1);
            }
            this.conn = null;
        }
    }
    return this.conn;
}

From source file:com.pivotal.gemfire.tools.pulse.internal.data.JMXDataUpdater.java

private JMXConnector getJMXConnection() {
    JMXConnector connection = null;
    // Reference to repository
    Repository repository = Repository.get();
    try {// w w w  .j  a  va 2s  .  c  o  m

        String jmxSerURL = "";

        if (LOGGER.infoEnabled()) {
            LOGGER.info(resourceBundle.getString("LOG_MSG_USE_LOCATOR_VALUE") + ":"
                    + repository.getJmxUseLocator());
        }

        if (repository.getJmxUseLocator()) {

            String locatorHost = repository.getJmxHost();
            int locatorPort = Integer.parseInt(repository.getJmxPort());

            if (LOGGER.infoEnabled()) {
                LOGGER.info(resourceBundle.getString("LOG_MSG_HOST") + " : " + locatorHost + " & "
                        + resourceBundle.getString("LOG_MSG_PORT") + " : " + locatorPort);
            }

            InetAddress inetAddr = InetAddress.getByName(locatorHost);

            if ((inetAddr instanceof Inet4Address) || (inetAddr instanceof Inet6Address)) {

                if (inetAddr instanceof Inet4Address) {
                    // Locator has IPv4 Address
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_IPV4_ADDRESS") + " - "
                                + inetAddr.toString());
                    }
                } else {
                    // Locator has IPv6 Address
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_IPV6_ADDRESS") + " - "
                                + inetAddr.toString());
                    }
                }

                JmxManagerInfo jmxManagerInfo = JmxManagerFinder.askLocatorForJmxManager(inetAddr, locatorPort,
                        15000, repository.isUseSSLLocator());

                if (jmxManagerInfo.port == 0) {
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_COULD_NOT_FIND_MANAGER"));
                    }
                } else {
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_FOUND_MANAGER") + " : "
                                + resourceBundle.getString("LOG_MSG_HOST") + " : " + jmxManagerInfo.host + " & "
                                + resourceBundle.getString("LOG_MSG_PORT") + " : " + jmxManagerInfo.port
                                + (jmxManagerInfo.ssl ? resourceBundle.getString("LOG_MSG_WITH_SSL")
                                        : resourceBundle.getString("LOG_MSG_WITHOUT_SSL")));
                    }

                    jmxSerURL = formJMXServiceURLString(jmxManagerInfo.host,
                            String.valueOf(jmxManagerInfo.port));
                }

            } /*
               * else if (inetAddr instanceof Inet6Address) { // Locator has IPv6
               * Address if(LOGGER.infoEnabled()){
               * LOGGER.info(resourceBundle.getString
               * ("LOG_MSG_LOCATOR_IPV6_ADDRESS")); } // update message to display
               * on UI cluster.setConnectionErrorMsg(resourceBundle.getString(
               * "LOG_MSG_JMX_CONNECTION_IPv6_ADDRESS"));
               * 
               * }
               */else {
                // Locator has Invalid locator Address
                if (LOGGER.infoEnabled()) {
                    LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_BAD_ADDRESS"));
                }
                // update message to display on UI
                cluster.setConnectionErrorMsg(resourceBundle.getString("LOG_MSG_JMX_CONNECTION_BAD_ADDRESS"));

            }

        } else {
            if (LOGGER.infoEnabled()) {
                LOGGER.info(resourceBundle.getString("LOG_MSG_HOST") + " : " + this.serverName + " & "
                        + resourceBundle.getString("LOG_MSG_PORT") + " : " + this.port);
            }
            jmxSerURL = formJMXServiceURLString(this.serverName, this.port);
        }

        if (StringUtils.isNotNullNotEmptyNotWhiteSpace(jmxSerURL)) {
            JMXServiceURL url = new JMXServiceURL(jmxSerURL);

            // String[] creds = {"controlRole", "R&D"};
            String[] creds = { this.userName, this.userPassword };
            Map<String, Object> env = new HashMap<String, Object>();

            env.put(JMXConnector.CREDENTIALS, creds);

            if (repository.isUseSSLManager()) {
                // use ssl to connect
                env.put("com.sun.jndi.rmi.factory.socket", new SslRMIClientSocketFactory());
            }

            connection = JMXConnectorFactory.connect(url, env);

            // Register Pulse URL if not already present in the JMX Manager
            registerPulseUrlToManager(connection);
        }
    } catch (Exception e) {
        if (e instanceof UnknownHostException) {
            // update message to display on UI
            cluster.setConnectionErrorMsg(resourceBundle.getString("LOG_MSG_JMX_CONNECTION_UNKNOWN_HOST"));
        }

        // write errors
        StringWriter swBuffer = new StringWriter();
        PrintWriter prtWriter = new PrintWriter(swBuffer);
        e.printStackTrace(prtWriter);
        LOGGER.severe("Exception Details : " + swBuffer.toString() + "\n");
        if (this.conn != null) {
            try {
                this.conn.close();
            } catch (Exception e1) {
                LOGGER.severe("Error closing JMX connection " + swBuffer.toString() + "\n");
            }
            this.conn = null;
        }
    }

    return connection;
}

From source file:edu.nwpu.gemfire.monitor.data.JMXDataUpdater.java

private JMXConnector getJMXConnection() {
    JMXConnector connection = null;
    // Reference to repository
    Repository repository = Repository.get();
    try {//from   w w w  . j  a  v  a 2s  .  co m

        String jmxSerURL = "";

        if (LOGGER.infoEnabled()) {
            LOGGER.info(resourceBundle.getString("LOG_MSG_USE_LOCATOR_VALUE") + ":"
                    + repository.getJmxUseLocator());
        }

        if (repository.getJmxUseLocator()) {

            String locatorHost = repository.getJmxHost();
            int locatorPort = Integer.parseInt(repository.getJmxPort());

            if (LOGGER.infoEnabled()) {
                LOGGER.info(resourceBundle.getString("LOG_MSG_HOST") + " : " + locatorHost + " & "
                        + resourceBundle.getString("LOG_MSG_PORT") + " : " + locatorPort);
            }

            InetAddress inetAddr = InetAddress.getByName(locatorHost);

            if ((inetAddr instanceof Inet4Address) || (inetAddr instanceof Inet6Address)) {

                if (inetAddr instanceof Inet4Address) {
                    // Locator has IPv4 Address
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_IPV4_ADDRESS") + " - "
                                + inetAddr.toString());
                    }
                } else {
                    // Locator has IPv6 Address
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_IPV6_ADDRESS") + " - "
                                + inetAddr.toString());
                    }
                }

                JmxManagerInfo jmxManagerInfo = JmxManagerFinder.askLocatorForJmxManager(inetAddr, locatorPort,
                        15000, repository.isUseSSLLocator());

                if (jmxManagerInfo.port == 0) {
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_COULD_NOT_FIND_MANAGER"));
                    }
                } else {
                    if (LOGGER.infoEnabled()) {
                        LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_FOUND_MANAGER") + " : "
                                + resourceBundle.getString("LOG_MSG_HOST") + " : " + jmxManagerInfo.host + " & "
                                + resourceBundle.getString("LOG_MSG_PORT") + " : " + jmxManagerInfo.port
                                + (jmxManagerInfo.ssl ? resourceBundle.getString("LOG_MSG_WITH_SSL")
                                        : resourceBundle.getString("LOG_MSG_WITHOUT_SSL")));
                    }

                    jmxSerURL = formJMXServiceURLString(jmxManagerInfo.host,
                            String.valueOf(jmxManagerInfo.port));
                }

            } /*
               * else if (inetAddr instanceof Inet6Address) { // Locator has IPv6
               * Address if(LOGGER.infoEnabled()){
               * LOGGER.info(resourceBundle.getString
               * ("LOG_MSG_LOCATOR_IPV6_ADDRESS")); } // update message to display
               * on UI cluster.setConnectionErrorMsg(resourceBundle.getString(
               * "LOG_MSG_JMX_CONNECTION_IPv6_ADDRESS"));
               *
               * }
               */else {
                // Locator has Invalid locator Address
                if (LOGGER.infoEnabled()) {
                    LOGGER.info(resourceBundle.getString("LOG_MSG_LOCATOR_BAD_ADDRESS"));
                }
                // update message to display on UI
                cluster.setConnectionErrorMsg(resourceBundle.getString("LOG_MSG_JMX_CONNECTION_BAD_ADDRESS"));

            }

        } else {
            if (LOGGER.infoEnabled()) {
                LOGGER.info(resourceBundle.getString("LOG_MSG_HOST") + " : " + this.serverName + " & "
                        + resourceBundle.getString("LOG_MSG_PORT") + " : " + this.port);
            }
            jmxSerURL = formJMXServiceURLString(this.serverName, this.port);
        }

        if (StringUtils.isNotNullNotEmptyNotWhiteSpace(jmxSerURL)) {
            JMXServiceURL url = new JMXServiceURL(jmxSerURL);

            // String[] creds = {"controlRole", "R&D"};
            String[] creds = { this.userName, this.userPassword };
            Map<String, Object> env = new HashMap<String, Object>();

            env.put(JMXConnector.CREDENTIALS, creds);

            if (repository.isUseSSLManager()) {
                // use ssl to connect
                env.put("com.sun.jndi.rmi.factory.socket", new SslRMIClientSocketFactory());
            }

            connection = JMXConnectorFactory.connect(url, env);

            // Register Pulse URL if not already present in the JMX Manager
            registerPulseUrlToManager(connection);
        }
    } catch (Exception e) {
        if (e instanceof UnknownHostException) {
            // update message to display on UI
            cluster.setConnectionErrorMsg(resourceBundle.getString("LOG_MSG_JMX_CONNECTION_UNKNOWN_HOST"));
        }

        // write errors
        StringWriter swBuffer = new StringWriter();
        PrintWriter prtWriter = new PrintWriter(swBuffer);
        e.printStackTrace(prtWriter);
        LOGGER.severe("Exception Details : " + swBuffer.toString() + "\n");
        if (this.conn != null) {
            try {
                this.conn.close();
            } catch (Exception e1) {
                LOGGER.severe("Error closing JMX connection " + swBuffer.toString() + "\n");
            }
            this.conn = null;
        }
    }

    return connection;
}

From source file:org.wso2.carbon.integration.test.metrics.CarbonMetricsTestCase.java

/**
 * This method will force metric manager to collect metrics by invoking report() method
 * using remote jmx/*ww w. j a va 2s .  c o  m*/
 * @throws IOException
 * @throws MalformedObjectNameException
 */
private void invokeJMXReportOperation()
        throws IOException, MalformedObjectNameException, XPathExpressionException {
    int JMXServicePort = Integer.parseInt(cepServer.getInstance().getPorts().get("jmxserver"));
    int RMIRegistryPort = Integer.parseInt(cepServer.getInstance().getPorts().get("rmiregistry"));
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost:" + JMXServicePort
            + "/jndi/rmi://localhost:" + RMIRegistryPort + "/jmxrmi");
    Map<String, String[]> env = new HashMap<>();
    String[] credentials = { "admin", "admin" };
    env.put(JMXConnector.CREDENTIALS, credentials);
    JMXConnector jmxConnector = JMXConnectorFactory.connect(url, env);
    MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
    ObjectName mbeanName = new ObjectName("org.wso2.carbon:type=MetricManager");
    MetricManagerMXBean mbeanProxy = MBeanServerInvocationHandler.newProxyInstance(mbeanServerConnection,
            mbeanName, MetricManagerMXBean.class, true);
    mbeanProxy.report();
    jmxConnector.close();
}

From source file:com.googlecode.jmxtrans.model.Server.java

/**
 * Generates the proper username/password environment for JMX connections.
 *///from  w  w  w.  j av  a2  s .  co  m
@JsonIgnore
public ImmutableMap<String, ?> getEnvironment() {
    if (getProtocolProviderPackages() != null && getProtocolProviderPackages().contains("weblogic")) {
        ImmutableMap.Builder<String, String> environment = ImmutableMap.builder();
        if ((username != null) && (password != null)) {
            environment.put(PROTOCOL_PROVIDER_PACKAGES, getProtocolProviderPackages());
            environment.put(SECURITY_PRINCIPAL, username);
            environment.put(SECURITY_CREDENTIALS, password);
        }
        return environment.build();
    }

    ImmutableMap.Builder<String, String[]> environment = ImmutableMap.builder();
    if ((username != null) && (password != null)) {
        String[] credentials = new String[] { username, password };
        environment.put(JMXConnector.CREDENTIALS, credentials);
    }

    return environment.build();
}

From source file:org.hyperic.hq.product.jmx.MxUtil.java

public static JMXConnector getMBeanConnector(Properties config) throws MalformedURLException, IOException {

    String jmxUrl = config.getProperty(MxUtil.PROP_JMX_URL);
    Map map = new HashMap();

    String user = config.getProperty(PROP_JMX_USERNAME);
    String pass = config.getProperty(PROP_JMX_PASSWORD);

    map.put(JMXConnector.CREDENTIALS, new String[] { user, pass });

    // required for Oracle AS
    String providerPackages = config.getProperty(PROP_JMX_PROVIDER_PKGS);
    if (providerPackages != null)
        map.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, providerPackages);

    if (jmxUrl == null) {
        throw new MalformedURLException(PROP_JMX_URL + "==null");
    }/*from www  . ja v a2s .  c o  m*/

    if (jmxUrl.startsWith(PTQL_PREFIX)) {
        jmxUrl = getUrlFromPid(jmxUrl.substring(PTQL_PREFIX.length()));
    }

    JMXServiceURL url = new JMXServiceURL(jmxUrl);

    String proto = url.getProtocol();
    if (proto.equals("t3") || proto.equals("t3s")) {
        //http://edocs.bea.com/wls/docs92/jmx/accessWLS.html
        //WebLogic support, requires:
        //cp $WLS_HOME/server/lib/wljmxclient.jar pdk/lib/
        //cp $WLS_HOME/server/lib/wlclient.jar pdk/lib/
        map.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
        map.put(Context.SECURITY_PRINCIPAL, user);
        map.put(Context.SECURITY_CREDENTIALS, pass);
    }

    JMXConnector connector = JMXConnectorFactory.connect(url, map);
    if (log.isDebugEnabled()) {
        log.debug("created new JMXConnector url=" + url + ", classloader="
                + Thread.currentThread().getContextClassLoader());
    }
    return connector;
}

From source file:org.eclipse.virgo.server.svt.hostedrepo.RemoteHostedRepositoryTests.java

private static MBeanServerConnection getMBeanServerConnection() throws Exception {
    String severDir = null;/*from  ww  w  . j av a  2 s . c  om*/
    String[] creds = { "admin", "springsource" };
    Map<String, String[]> env = new HashMap<String, String[]>();

    File testExpanded = new File("src/test/resources");
    for (File mainDir : testExpanded.listFiles()) {
        if (mainDir.isDirectory() && mainDir.getName().equals("virgo-web-server")) {
            severDir = new File(mainDir.toURI()).getCanonicalPath();
        }
    }
    env.put(JMXConnector.CREDENTIALS, creds);
    System.setProperty("javax.net.ssl.trustStore", severDir + KEYSTORE);
    System.setProperty("javax.net.ssl.trustStorePassword", KEYPASSWORD);
    JMXServiceURL url = new JMXServiceURL(JMXURL);
    connection = JMXConnectorFactory.connect(url, env).getMBeanServerConnection();
    return connection;
}

From source file:org.eclipse.virgo.server.svt.hostedrepo.RemoteHostedRepositoryOtherThan8080Tests.java

private static MBeanServerConnection getMBeanServerConnection() throws Exception {
    String severDir = null;/*  ww  w.j a  v a2 s . co m*/
    String[] creds = { "admin", "springsource" };
    Map<String, String[]> env = new HashMap<String, String[]>();

    File testExpanded = new File("src/test/resources");
    for (File mainDir : testExpanded.listFiles()) {
        if (mainDir.isDirectory() && mainDir.getName().equals("virgo-web-server2")) {
            severDir = new File(mainDir.toURI()).getCanonicalPath();
        }
    }
    env.put(JMXConnector.CREDENTIALS, creds);
    System.setProperty("javax.net.ssl.trustStore", severDir + KEYSTORE);
    System.setProperty("javax.net.ssl.trustStorePassword", KEYPASSWORD);
    JMXServiceURL url = new JMXServiceURL(JMXURL);
    connection = JMXConnectorFactory.connect(url, env).getMBeanServerConnection();
    return connection;
}

From source file:org.kuali.test.utils.Utils.java

public static JMXConnector getJMXConnector(String url, String username, String password)
        throws MalformedURLException, IOException {
    JMXConnector retval = null;/*  ww w . ja v a 2s. co m*/

    if (StringUtils.isNotBlank(url)) {
        JMXServiceURL serviceUrl = new JMXServiceURL(url);

        Map map = null;

        if (StringUtils.isNotBlank(username)) {
            map = new HashMap();
            map.put(JMXConnector.CREDENTIALS, new String[] { username, password });
        }

        retval = JMXConnectorFactory.connect(serviceUrl, map);
    }

    return retval;
}