Search in sources :

Example 1 with InvalidContextClassLoaderException

use of org.opennms.netmgt.jmx.impl.connection.connectors.IsolatingClassLoader.InvalidContextClassLoaderException in project opennms by OpenNMS.

the class JBossMBeanServerConnector method createConnection.

@Override
public JmxServerConnectionWrapper createConnection(final InetAddress ipAddress, final Map<String, String> propertiesMap) throws JmxServerConnectionException {
    JBossConnectionWrapper wrapper = null;
    ClassLoader icl = null;
    final ClassLoader originalLoader = Thread.currentThread().getContextClassLoader();
    String connectionType = ParameterMap.getKeyedString(propertiesMap, "factory", "RMI");
    String timeout = ParameterMap.getKeyedString(propertiesMap, "timeout", "3000");
    String jbossVersion = ParameterMap.getKeyedString(propertiesMap, "version", "4");
    String port = ParameterMap.getKeyedString(propertiesMap, "port", "1099");
    if (jbossVersion == null || jbossVersion.startsWith("4")) {
        try {
            icl = new IsolatingClassLoader("jboss", new URL[] { new File(System.getProperty("opennms.home") + "/lib/jboss/jbossall-client.jar").toURI().toURL() }, originalLoader, PACKAGES, true);
        } catch (MalformedURLException e) {
            LOG.error("JBossConnectionWrapper MalformedURLException", e);
        } catch (InvalidContextClassLoaderException e) {
            LOG.error("JBossConnectionWrapper InvalidContextClassLoaderException", e);
        }
    } else if (jbossVersion.startsWith("3")) {
        PrivilegedAction<IsolatingClassLoader> action = new PrivilegedAction<IsolatingClassLoader>() {

            @Override
            public IsolatingClassLoader run() {
                try {
                    return new IsolatingClassLoader("jboss", new URL[] { new File(System.getProperty("opennms.home") + "/lib/jboss/jbossall-client32.jar").toURI().toURL() }, originalLoader, PACKAGES, true);
                } catch (MalformedURLException e) {
                    LOG.error("JBossConnectionWrapper MalformedURLException", e);
                } catch (InvalidContextClassLoaderException e) {
                    LOG.error("JBossConnectionWrapper InvalidContextClassLoaderException", e);
                }
                return null;
            }
        };
        AccessController.doPrivileged(action);
    }
    if (icl == null) {
        return null;
    }
    Thread.currentThread().setContextClassLoader(icl);
    if (connectionType.equals("RMI")) {
        InitialContext ctx = null;
        final String hostAddress = InetAddressUtils.toUrlIpAddress(ipAddress);
        try {
            Hashtable<String, String> props = new Hashtable<String, String>();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
            props.put(Context.PROVIDER_URL, "jnp://" + hostAddress + ":" + port);
            props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
            props.put("jnp.sotimeout", timeout);
            ctx = new InitialContext(props);
            Object rmiAdaptor = ctx.lookup("jmx/rmi/RMIAdaptor");
            wrapper = new JBossConnectionWrapper(MBeanServerProxy.buildServerProxy(rmiAdaptor));
        } catch (Throwable e) {
            LOG.debug("JBossConnectionFactory - unable to get MBeanServer using RMI on {}:{}", hostAddress, port);
        } finally {
            try {
                if (ctx != null) {
                    ctx.close();
                }
            } catch (Throwable e1) {
                LOG.debug("JBossConnectionFactory error closing initial context");
            }
        }
    } else if (connectionType.equals("HTTP")) {
        InitialContext ctx = null;
        String invokerSuffix = null;
        final String hostAddress = InetAddressUtils.toUrlIpAddress(ipAddress);
        try {
            Hashtable<String, String> props = new Hashtable<String, String>();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
            props.put(Context.PROVIDER_URL, "http://" + hostAddress + ":" + port + "/invoker/JNDIFactory");
            props.put("jnp.sotimeout", timeout);
            ctx = new InitialContext(props);
            Object rmiAdaptor = ctx.lookup("jmx/rmi/RMIAdaptor");
            wrapper = new JBossConnectionWrapper(MBeanServerProxy.buildServerProxy(rmiAdaptor));
        } catch (Throwable e) {
            LOG.debug("JBossConnectionFactory - unable to get MBeanServer using HTTP on {}{}", hostAddress, invokerSuffix);
        } finally {
            try {
                if (ctx != null)
                    ctx.close();
            } catch (NamingException e1) {
                LOG.debug("JBossConnectionFactory error closing initial context");
            }
        }
    }
    Thread.currentThread().setContextClassLoader(originalLoader);
    return wrapper;
}
Also used : MalformedURLException(java.net.MalformedURLException) Hashtable(java.util.Hashtable) InvalidContextClassLoaderException(org.opennms.netmgt.jmx.impl.connection.connectors.IsolatingClassLoader.InvalidContextClassLoaderException) URL(java.net.URL) InitialContext(javax.naming.InitialContext) PrivilegedAction(java.security.PrivilegedAction) NamingException(javax.naming.NamingException) File(java.io.File)

Aggregations

File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 PrivilegedAction (java.security.PrivilegedAction)1 Hashtable (java.util.Hashtable)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 InvalidContextClassLoaderException (org.opennms.netmgt.jmx.impl.connection.connectors.IsolatingClassLoader.InvalidContextClassLoaderException)1