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:fr.openfarm.jmx.service.JMXQuery.java

@Override
public void connect(String username, String password, String url) throws IOException {
    Map<String, Object> environment = null;
    if ("".equals(username)) {
        username = null;//w ww.j a  v a2 s  .  c o m
    }
    if ("".equals(password)) {
        username = null;
    }
    if (username != null && password != null) {
        environment = new HashMap<String, Object>();
        environment.put(JMXConnector.CREDENTIALS, new String[] { username, password });
        environment.put(USERNAME_KEY, username);
        environment.put(PASSWORD_KEY, password);
    }

    JMXServiceURL jmxUrl = new JMXServiceURL(url);
    if (environment != null) {
        log.info("connect with user/pass");
        connector = JMXConnectorFactory.connect(jmxUrl, environment);
    } else {
        log.info("connect without user/pass");
        connector = JMXConnectorFactory.connect(jmxUrl);
    }
    connection = connector.getMBeanServerConnection();
}

From source file:org.wso2.dss.integration.test.jira.issues.CARBON15928JMXDisablingTest.java

private MBeanInfo testMBeanForDatasource() throws Exception {
    Map<String, String[]> env = new HashMap<>();
    String[] credentials = { "admin", "admin" };
    env.put(JMXConnector.CREDENTIALS, credentials);
    try {//from w  w  w .j a v  a2 s.com
        String url = "service:jmx:rmi://localhost:12311/jndi/rmi://localhost:11199/jmxrmi";
        JMXServiceURL jmxUrl = new JMXServiceURL(url);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl, env);
        MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
        ObjectName mbeanObject = new ObjectName(dataSourceName + ",-1234:type=DataSource");
        MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(mbeanObject);
        return mBeanInfo;
    } catch (MalformedURLException | MalformedObjectNameException | IntrospectionException
            | ReflectionException e) {
        throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
    }
}

From source file:org.fabrician.enabler.KarafContainer.java

public synchronized MBeanServerConnection getMBeanServerConnection() throws Exception {
    if (this.mBeanServer == null) {
        Map<String, String[]> environment = new HashMap<String, String[]>();
        String user = getAdminName();
        String pwd = getAdminPassword();
        String jmxurl = getJmxClientUrl();
        String[] credentials = new String[] { user, pwd };
        environment.put(JMXConnector.CREDENTIALS, credentials);
        JMXServiceURL url = null;
        try {/*  ww  w  .  j a v a 2  s.c  o m*/
            url = new JMXServiceURL(jmxurl);
            getEngineLogger().info("Establishing JMX connection to URL : [" + jmxurl + "]...");
            this.jmxc = JMXConnectorFactory.connect(url, environment);
            this.mBeanServer = jmxc.getMBeanServerConnection();
            getEngineLogger().info("JMX connection established.");
        } catch (Exception ex) {
            getEngineLogger().warning("[" + jmxurl + "] : " + ex.getMessage());
            throw ex;
        }
    }
    return this.mBeanServer;
}

From source file:com.heliosapm.streams.collector.ds.pool.impls.JMXClientPoolBuilder.java

/**
 * Creates a new JMXClientPoolBuilder//from  w  w w  .  j  a  va2 s . c  om
 * @param config The configuration properties
 */
public JMXClientPoolBuilder(final Properties config) {
    final String jmxUrl = config.getProperty(JMX_URL_KEY, "").trim();
    if (jmxUrl.isEmpty())
        throw new IllegalArgumentException("The passed JMXServiceURL was null or empty");
    try {
        url = new JMXServiceURL(jmxUrl);
    } catch (Exception ex) {
        throw new IllegalArgumentException("The passed JMXServiceURL was invalid", ex);
    }
    final String user = config.getProperty(JMX_USER_KEY, "").trim();
    final String password = config.getProperty(JMX_PW_KEY, "").trim();
    if (!user.isEmpty() && !password.isEmpty()) {
        credentials = new String[] { user, password };
        env.put(JMXConnector.CREDENTIALS, credentials);
    } else {
        credentials = null;
    }
    if (config.size() > 1) {
        for (final String key : config.stringPropertyNames()) {
            final String _key = key.trim();
            if (_key.startsWith(JMX_ENVMAP_PREFIX)) {
                final String _envKey = _key.replace(JMX_ENVMAP_PREFIX, "");
                if (_envKey.isEmpty())
                    continue;
                final String _rawValue = config.getProperty(key, "").trim();
                if (_rawValue.isEmpty())
                    continue;
                final Object _convertedValue = StringHelper.convertTyped(_rawValue);
                env.put(_envKey, _convertedValue);
            }
        }
    }
}

From source file:com.zenoss.jmx.JmxClient.java

/**
 * Connects to the JMX Agent/*from w ww.j  a v  a 2s .  c  om*/
 * 
 * @throws JMXException
 *             if an exception occurs during connection
 */
public void connect() throws JmxException {

    // short-circuit to avoid re-connecting
    if (_connected) {
        return;
    }

    try {
        // honor the authentication credentials
        Map<String, Object> env = new HashMap<String, Object>();
        env.put(JMXConnector.CREDENTIALS, _creds);

        _connector = JMXConnectorFactory.connect(_url, env);
        _server = _connector.getMBeanServerConnection();
    } catch (IOException e) {
        throw new JmxException("Failed to connect to " + _url, e);
    }

    _connected = true;
}

From source file:io.github.albertopires.mjc.JConsoleM.java

private JConsoleM(ServerConfiguration serverConfiguration) throws Exception {
    JMXConnector jmxc = null;/*from   w ww  .  j  a  v  a 2  s .c om*/
    if (serverConfiguration.getAuthenticate()) {
        String urlStr = "service:jmx:http-remoting-jmx://" + serverConfiguration.getHost() + ":"
                + serverConfiguration.getPort();
        Map<String, Object> env = new HashMap<String, Object>();
        String[] creds = { serverConfiguration.getUser(), serverConfiguration.getPassword() };
        env.put(JMXConnector.CREDENTIALS, creds);
        jmxc = JMXConnectorFactory.connect(new JMXServiceURL(urlStr), env);
    } else {
        String urlStr = "service:jmx:rmi:///jndi/rmi://" + serverConfiguration.getHost() + ":"
                + serverConfiguration.getPort() + "/jmxrmi";
        //         String urlStr = "service:jmx:remote+http://" + serverConfiguration.getHost() + ":" + serverConfiguration.getPort();
        JMXServiceURL url = new JMXServiceURL(urlStr);
        jmxc = JMXConnectorFactory.connect(url, null);
    }
    mbsc = jmxc.getMBeanServerConnection();
    ncpu = getAvailableProcessors();
}

From source file:com.zabbix.gateway.JMXItemChecker.java

public JMXItemChecker(JSONObject request) throws ZabbixException {
    super(request);

    try {//from   w w  w  . j a  v a  2  s .co  m
        String conn = request.getString(JSON_TAG_CONN);
        int port = request.getInt(JSON_TAG_PORT);

        jmxc = null;
        mbsc = null;
        String jmx_url = "service:jmx:rmi:///jndi/rmi://[" + conn + "]:" + port + "/jmxrmi"; // default
        String jboss_url = "service:jmx:remoting-jmx://" + conn + ":" + port; // jboss
        String t3_url = "service:jmx:t3://" + conn + ":" + port
                + "/jndi/weblogic.management.mbeanservers.runtime"; // T3
        String t3s_url = "service:jmx:t3s://" + conn + ":" + port
                + "/jndi/weblogic.management.mbeanservers.runtime"; // T3S
        protocol = "jmx";
        String tested_url = jmx_url;

        username = request.optString(JSON_TAG_USERNAME, null);
        password = request.optString(JSON_TAG_PASSWORD, null);

        //if (null != username && null == password || null == username && null != password)
        //   throw new IllegalArgumentException("invalid username and password nullness combination");

        if (null != username) {
            // Testing if username is like "<user>:<protocol>"
            int protocol_in_username = username.indexOf(':');
            if (protocol_in_username != -1) {
                String result[] = username.split(":");
                username = result[0];
                protocol = result[1];
            }
        }

        switch (protocol) {
        case "jmx":
        case "jmxs":
            tested_url = jmx_url;
            break;
        case "jboss":
            tested_url = jboss_url;
            break;
        case "t3":
            tested_url = t3_url;
            break;
        case "t3s":
            tested_url = t3s_url;
            break;
        default:
            tested_url = jmx_url;
            break;
        }

        logger.info("Using url '{}' with user '{}'", tested_url, username);

        HashMap<String, Object> env = new HashMap<String, Object>();
        env.put(JMXConnector.CREDENTIALS, new String[] { username, password });

        if (protocol.equals("t3") || protocol.equals("t3s")) {
            env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
            env.put(javax.naming.Context.SECURITY_PRINCIPAL, ((String[]) env.get(JMXConnector.CREDENTIALS))[0]);
            env.put(javax.naming.Context.SECURITY_CREDENTIALS,
                    ((String[]) env.get(JMXConnector.CREDENTIALS))[1]);
        }

        // Required by SSL
        if (protocol.equals("jmxs")) {
            env.put("com.sun.jndi.rmi.factory.socket", new SslRMIClientSocketFactory());
        }

        url = new JMXServiceURL(tested_url);
        jmxc = ZabbixJMXConnectorFactory.connect(url, env);
        mbsc = jmxc.getMBeanServerConnection();
    } catch (Exception e) {
        throw new ZabbixException(e);
    } finally {
        try {
            if (null != jmxc)
                jmxc.close();
        } catch (java.io.IOException exception) {
        }

        jmxc = null;
        mbsc = null;
    }
}

From source file:fullThreadDump.java

private void connect(boolean jbossRemotingJMX, String hostname, String user, String passwd) {
    String urlString;//from   w w  w  .  j  a v a  2s .  com
    try {
        HashMap<String, String[]> env = new HashMap<String, String[]>();
        String[] creds = new String[2];
        creds[0] = user;
        creds[1] = passwd;
        env.put(JMXConnector.CREDENTIALS, creds);
        if (jbossRemotingJMX) {
            urlString = "service:jmx:remoting-jmx://" + hostname;
            this.serviceURL = new JMXServiceURL(urlString);
            System.out.println("\n\nConnecting to " + urlString);
        } else {
            urlString = "/jndi/rmi://" + hostname + "/jmxrmi";
            this.serviceURL = new JMXServiceURL("rmi", "", 0, urlString);
            System.out.println("\n\nConnecting to " + urlString);
        }

        //this.jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
        this.jmxConnector = JMXConnectorFactory.connect(serviceURL, env);
        this.server = jmxConnector.getMBeanServerConnection();
    } catch (MalformedURLException e) {
        // should not reach here
    } catch (IOException e) {
        System.err.println("\nCommunication error: " + e.getMessage());
        System.exit(1);
    }
}

From source file:org.wso2.carbon.esb.mediator.test.property.PropertyIntegrationAxis2PropertiesTestCase.java

@Test(groups = { "wso2.esb" }, description = "Send messages using  ConcurrentConsumers "
        + "and MaxConcurrentConsumers Axis2 level properties")
public void maxConcurrentConsumersTest() throws Exception {

    String carbonHome = System.getProperty(ServerConstants.CARBON_HOME);
    String confDir = carbonHome + File.separator + "repository" + File.separator + "conf" + File.separator;

    //enabling jms transport with ActiveMQ
    File originalConfig = new File(
            TestConfigurationProvider.getResourceLocation() + File.separator + "artifacts" + File.separator
                    + "AXIS2" + File.separator + "config" + File.separator + "property_axis2_server.xml");
    File destDir = new File(confDir + "axis2" + File.separator);
    FileUtils.copyFileToDirectory(originalConfig, destDir);

    serverManager.restartGracefully();/*from ww  w.j  ava 2s . c o  m*/

    super.init(); // after restart the server instance initialization
    JMXServiceURL url = new JMXServiceURL(
            "service:jmx:rmi://" + context.getDefaultInstance().getHosts().get("default") + ":11311/jndi/rmi://"
                    + context.getDefaultInstance().getHosts().get("default") + ":10199/jmxrmi");

    HashMap<String, String[]> environment = new HashMap<String, String[]>();
    String[] credentials = new String[] { "admin", "admin" };
    environment.put(JMXConnector.CREDENTIALS, credentials);

    MBeanServerConnection mBeanServerConnection = JMXConnectorFactory.connect(url, environment)
            .getMBeanServerConnection();

    int beforeThreadCount = (Integer) mBeanServerConnection
            .getAttribute(new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME), "ThreadCount");

    String queueName = "SimpleStockQuoteService";

    for (int x = 0; x < 200; x++) {
        JMSQueueMessageProducer sender = new JMSQueueMessageProducer(
                JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration());

        try {
            sender.connect(queueName);
            for (int i = 0; i < 3; i++) {
                sender.pushMessage("<?xml version='1.0' encoding='UTF-8'?>"
                        + "<soapenv:Envelope xmlns:soapenv=\"http://schemas." + "xmlsoap.org/soap/envelope/\""
                        + " xmlns:ser=\"http://services.samples\" xmlns:xsd=\""
                        + "http://services.samples/xsd\">" + "   <soapenv:Header/>" + "   <soapenv:Body>"
                        + "      <ser:placeOrder>" + "         <ser:order>"
                        + "            <xsd:price>100</xsd:price>"
                        + "            <xsd:quantity>2000</xsd:quantity>"
                        + "            <xsd:symbol>JMSTransport</xsd:symbol>" + "         </ser:order>"
                        + "      </ser:placeOrder>" + "   </soapenv:Body>" + "</soapenv:Envelope>");
            }
        } finally {
            sender.disconnect();
        }
    }

    OMElement synapse = esbUtils
            .loadResource("/artifacts/ESB/mediatorconfig/property/" + "ConcurrentConsumers.xml");
    updateESBConfiguration(JMSEndpointManager.setConfigurations(synapse));

    int afterThreadCount = (Integer) mBeanServerConnection
            .getAttribute(new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME), "ThreadCount");

    assertTrue((afterThreadCount - beforeThreadCount) <= 150, "Expected thread count range" + " not met");
}

From source file:org.apache.cassandra.tools.AbstractJmxClient.java

private void connect() throws IOException {
    JMXServiceURL jmxUrl = new JMXServiceURL(String.format(FMT_URL, host, port));
    Map<String, Object> env = new HashMap<String, Object>();

    if (username != null)
        env.put(JMXConnector.CREDENTIALS, new String[] { username, password });

    jmxc = JMXConnectorFactory.connect(jmxUrl, env);
    mbeanServerConn = jmxc.getMBeanServerConnection();
}