Search in sources :

Example 1 with ChildFirstClassLoaderBuilder

use of org.jboss.as.model.test.ChildFirstClassLoaderBuilder in project wildfly by wildfly.

the class ClientCompatibilityUnitTestCase method createClient.

protected static ModelControllerClient createClient(final String artifact, final String version, final String host, final int port) throws Exception {
    final ChildFirstClassLoaderBuilder classLoaderBuilder = new ChildFirstClassLoaderBuilder(false);
    classLoaderBuilder.addRecursiveMavenResourceURL(artifact + ":" + version, excludes);
    classLoaderBuilder.addParentFirstClassPattern("org.jboss.as.controller.client.ModelControllerClientConfiguration");
    classLoaderBuilder.addParentFirstClassPattern("org.jboss.as.controller.client.ModelControllerClient");
    classLoaderBuilder.addParentFirstClassPattern("org.jboss.as.controller.client.OperationMessageHandler");
    classLoaderBuilder.addParentFirstClassPattern("org.jboss.as.controller.client.Operation");
    classLoaderBuilder.addParentFirstClassPattern("org.jboss.as.controller.client.OperationResponse*");
    final ClassLoader classLoader = classLoaderBuilder.build();
    final Class<?> factoryClass = classLoader.loadClass("org.jboss.as.controller.client.ModelControllerClient$Factory");
    final Method factory = factoryClass.getMethod("create", String.class, int.class);
    try {
        final Object client = factory.invoke(null, host, port);
        final InvocationHandler invocationHandler = new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return method.invoke(client, args);
            }
        };
        final Class<?>[] interfaces = new Class<?>[] { ModelControllerClient.class };
        return (ModelControllerClient) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
    } catch (InvocationTargetException e) {
        Throwable t = e.getCause();
        if (t == null) {
            throw e;
        }
        throw t instanceof Exception ? (Exception) t : new RuntimeException(t);
    }
}
Also used : ChildFirstClassLoaderBuilder(org.jboss.as.model.test.ChildFirstClassLoaderBuilder) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) BeforeClass(org.junit.BeforeClass)

Aggregations

IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 ChildFirstClassLoaderBuilder (org.jboss.as.model.test.ChildFirstClassLoaderBuilder)1 BeforeClass (org.junit.BeforeClass)1