Search in sources :

Example 81 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project opennms by OpenNMS.

the class JmxCommand method execute.

protected void execute() throws CmdLineException, CmdRunException {
    try (JMXConnector connector = getJmxConnector()) {
        MBeanServerConnection mbeanServerConnection = connector.getMBeanServerConnection();
        execute(mbeanServerConnection);
    } catch (MBeanServerQueryException | JMException | IOException e) {
        throw new CmdRunException(e);
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerQueryException(org.opennms.features.jmxconfiggenerator.jmxconfig.query.MBeanServerQueryException) JMException(javax.management.JMException) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 82 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project opennms by OpenNMS.

the class DefaultJmxCollector method collect.

@Override
public void collect(JmxCollectorConfig config, MBeanServer mBeanServer, JmxSampleProcessor sampleProcessor) throws JmxServerConnectionException {
    Map<String, String> mergedStringMap = new HashMap<>(config.getServiceProperties());
    if (mBeanServer != null) {
        mergedStringMap.putAll(mBeanServer.getParameterMap());
    }
    JmxConnectionManager connectionManager = new DefaultConnectionManager(config.getRetries());
    try (JmxServerConnectionWrapper connectionWrapper = connectionManager.connect(config.getConnectionName(), InetAddressUtils.addr(config.getAgentAddress()), mergedStringMap, null)) {
        Objects.requireNonNull(connectionWrapper, "connectionWrapper should never be null");
        Objects.requireNonNull(connectionWrapper.getMBeanServerConnection(), "connectionWrapper.getMBeanServerConnection() should never be null");
        final MBeanServerConnection concreteConnection = connectionWrapper.getMBeanServerConnection();
        collect(concreteConnection, config.getJmxCollection(), sampleProcessor);
    }
}
Also used : HashMap(java.util.HashMap) DefaultConnectionManager(org.opennms.netmgt.jmx.impl.connection.connectors.DefaultConnectionManager) JmxConnectionManager(org.opennms.netmgt.jmx.connection.JmxConnectionManager) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 83 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project opennms by OpenNMS.

the class DefaultJmxConnector method createConnection.

public JmxServerConnectionWrapper createConnection(JmxConnectionConfig config) throws JmxServerConnectionException {
    try {
        // If we're trying to create a connection to a localhost address...
        if (config.isLocalConnection()) {
            // this JVM's MBeanServer directly.
            return new PlatformMBeanServerConnector().createConnection();
        }
        // Create URL
        final String urlString = config.getUrl();
        final JMXServiceURL url = new JMXServiceURL(urlString);
        LOG.debug("JMX: {} - {}", config.getFactory(), url);
        // Apply password strategy
        final Map<String, Object> env = new HashMap<>();
        config.getPasswordStategy().apply(env, config);
        // Create connection and connect
        final JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, env);
        try {
            connector.connect(env);
        } catch (SecurityException x) {
            throw new JmxServerConnectionException("Security exception: bad credentials", x);
        }
        // Wrap Connection
        MBeanServerConnection connection = connector.getMBeanServerConnection();
        JmxServerConnectionWrapper connectionWrapper = new DefaultConnectionWrapper(connector, connection);
        return connectionWrapper;
    } catch (MalformedURLException e) {
        throw new JmxServerConnectionException(e);
    } catch (IOException e) {
        throw new JmxServerConnectionException(e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) IOException(java.io.IOException) JmxServerConnectionException(org.opennms.netmgt.jmx.connection.JmxServerConnectionException) JMXConnector(javax.management.remote.JMXConnector) JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 84 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project opennms by OpenNMS.

the class JmxRemoteAdminIT method canConnect.

@Test
public void canConnect() throws Exception {
    final InetSocketAddress addr = m_testEnvironment.getServiceAddress(ContainerAlias.OPENNMS, 18980);
    final String hostString = "localhost".equals(addr.getHostString()) ? "127.0.0.1" : addr.getHostString();
    final int port = addr.getPort();
    final RMISocketFactory socketFactory = new JMXTestClientSocketFactory();
    System.setProperty("sun.rmi.transport.tcp.responseTimeout", "5000");
    final Callable<Integer> getRmiConnection = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            LOG.debug("getRmiConnection({}:{})", hostString, port);
            try {
                final Registry registry = LocateRegistry.getRegistry(hostString, port, socketFactory);
                final String[] bound = registry.list();
                LOG.debug("bound={}", Arrays.asList(bound));
                if (bound.length > 0) {
                    return bound.length;
                }
            } catch (final Exception e) {
            }
            return null;
        }
    };
    await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getRmiConnection, greaterThanOrEqualTo(1));
    final Callable<Integer> getJmxConnection = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            LOG.debug("getJmxConnection({}:{})", hostString, port);
            try {
                RMISocketFactory.setSocketFactory(socketFactory);
                final Map<String, Object> env = new HashMap<>();
                final String[] credentials = { "admin", "admin" };
                env.put(JMXConnector.CREDENTIALS, credentials);
                env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, socketFactory);
                final String urlString = String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", hostString, port);
                LOG.debug("getJmxConnection(): connecting to {}", urlString);
                final JMXServiceURL url = new JMXServiceURL(urlString);
                final JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
                final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
                LOG.debug("mbeanCount={}", mbsc.getMBeanCount());
                if (mbsc.getMBeanCount() > 0) {
                    return mbsc.getMBeanCount();
                }
            } catch (final Exception e) {
            }
            return null;
        }
    };
    await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getJmxConnection, greaterThanOrEqualTo(1));
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RMISocketFactory(java.rmi.server.RMISocketFactory) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 85 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project wildfly by wildfly.

the class MBeanTCCLTestCase method testTCCLInMBeanInvocation.

/**
     * Tests the MBean was deployed successfully and can be invoked. The fact that the MBean deployed successfully is a sign that the TCCL access from within the MBean code, worked fine
     *
     * @throws Exception
     */
@Test
public void testTCCLInMBeanInvocation() throws Exception {
    final MBeanServerConnection mBeanServerConnection = this.getMBeanServerConnection();
    final ObjectName mbeanObjectName = new ObjectName("wildfly:name=tccl-test-mbean");
    final int num1 = 3;
    final int num2 = 4;
    // invoke the operation on MBean
    final Integer sum = (Integer) mBeanServerConnection.invoke(mbeanObjectName, "add", new Object[] { num1, num2 }, new String[] { Integer.TYPE.getName(), Integer.TYPE.getName() });
    Assert.assertEquals("Unexpected return value from MBean: " + mbeanObjectName, num1 + num2, (int) sum);
}
Also used : MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

MBeanServerConnection (javax.management.MBeanServerConnection)125 JMXConnector (javax.management.remote.JMXConnector)84 ObjectName (javax.management.ObjectName)73 JMXServiceURL (javax.management.remote.JMXServiceURL)59 JMXConnectorServer (javax.management.remote.JMXConnectorServer)38 Test (org.junit.Test)35 IOException (java.io.IOException)31 MBeanServer (javax.management.MBeanServer)28 HashMap (java.util.HashMap)23 Attribute (javax.management.Attribute)15 NotificationListener (javax.management.NotificationListener)13 MalformedURLException (java.net.MalformedURLException)12 ArrayList (java.util.ArrayList)12 Notification (javax.management.Notification)12 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Map (java.util.Map)10 List (java.util.List)8 RemoteException (java.rmi.RemoteException)7 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7