Search in sources :

Example 96 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project pentaho-platform by pentaho.

the class JAXRSPluginServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // $NON-NLS-1$
    logger.debug("servicing request for resource " + request.getPathInfo());
    // seldom and don't need to be that performant.
    if (WADL_PATTERN.matcher(request.getPathInfo()).find()) {
        final HttpServletRequest originalRequest = request;
        final String appWadlUrl = request.getPathInfo().substring(request.getPathInfo().indexOf(APPLICATION_WADL), request.getPathInfo().length());
        request = (HttpServletRequest) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { HttpServletRequest.class }, new InvocationHandler() {

            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("getPathInfo")) {
                    return appWadlUrl;
                } else if (method.getName().equals("getRequestURL")) {
                    String url = originalRequest.getRequestURL().toString();
                    return new StringBuffer(url.substring(0, url.indexOf(originalRequest.getPathInfo())) + "/" + appWadlUrl);
                } else if (method.getName().equals("getRequestURI")) {
                    String uri = originalRequest.getRequestURI();
                    return uri.substring(0, uri.indexOf(originalRequest.getPathInfo())) + "/" + appWadlUrl;
                }
                // We don't care about the Method, delegate out to real Request object.
                return method.invoke(originalRequest, args);
            }
        });
        if (originalRequest.getRequestURL() != null) {
            requestThread.set(originalRequest.getRequestURL().toString());
        } else if (originalRequest.getRequestURI() != null) {
            requestThread.set(originalRequest.getRequestURI().toString());
        }
    }
    super.service(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 97 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project pentaho-platform by pentaho.

the class JAXRSServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug("servicing request for resource " + request.getPathInfo());
    }
    if (request.getMethod().equals(GET)) {
        // Extension to allow accept type override from mime-type query param
        final String mimeType = request.getParameter(MIME_TYPE);
        if (mimeType != null) {
            final HttpServletRequest originalRequest = request;
            request = (HttpServletRequest) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { HttpServletRequest.class }, new InvocationHandler() {

                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getName().equals(GET_HEADERS) && args.length > 0 && args[0].equals(ACCEPT)) {
                        return new Enumeration() {

                            boolean hasMore = true;

                            @Override
                            public boolean hasMoreElements() {
                                return hasMore;
                            }

                            @Override
                            public Object nextElement() {
                                hasMore = false;
                                return mimeType;
                            }
                        };
                    }
                    return method.invoke(originalRequest, args);
                }
            });
        }
    }
    super.service(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Enumeration(java.util.Enumeration) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 98 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project fabric8 by jboss-fuse.

the class JolokiaMXBeanProxy method getMXBeanProxy.

@SuppressWarnings("unchecked")
public static <T extends Object> T getMXBeanProxy(String serviceURL, ObjectName objectName, Class<T> mxbeanInterface, String username, String password, MBeanInfo mbeanInfo) {
    // [TODO] this should be obtaind remotely over jolokia
    if (mbeanInfo == null) {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        try {
            try {
                StandardMBean impl = new StandardMBean(Mockito.mock(mxbeanInterface), mxbeanInterface, true);
                server.registerMBean(impl, objectName);
                mbeanInfo = server.getMBeanInfo(objectName);
            } finally {
                if (server.isRegistered(objectName)) {
                    server.unregisterMBean(objectName);
                }
            }
        } catch (Exception ex) {
            throw new IllegalStateException("Cannot obtain MBeanInfo", ex);
        }
    }
    ClassLoader classLoader = mxbeanInterface.getClassLoader();
    InvocationHandler handler = new MXBeanInvocationHandler(serviceURL, objectName, username, password, classLoader, mbeanInfo);
    return (T) Proxy.newProxyInstance(classLoader, new Class<?>[] { mxbeanInterface }, handler);
}
Also used : StandardMBean(javax.management.StandardMBean) InvocationHandler(java.lang.reflect.InvocationHandler) OpenDataException(javax.management.openmbean.OpenDataException) MBeanServer(javax.management.MBeanServer)

Example 99 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project fabric8 by jboss-fuse.

the class InvocationTest method testUnderLoadAsyncProto.

@Test(timeout = 30 * 1000)
public void testUnderLoadAsyncProto() throws Exception {
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    DispatchQueue queue = Dispatch.createQueue();
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        final HelloImpl helloImpl = new HelloImpl();
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return helloImpl;
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        final Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        final AtomicInteger requests = new AtomicInteger(0);
        final AtomicInteger failures = new AtomicInteger(0);
        final long[] latencies = new long[BENCHMARK_CLIENTS * BENCHMARK_INVOCATIONS_PER_CLIENT];
        final long start = System.nanoTime();
        AsyncClient[] threads = new AsyncClient[BENCHMARK_CLIENTS];
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t] = new AsyncClient(t, BENCHMARK_INVOCATIONS_PER_CLIENT, hello, failures, requests, latencies);
            threads[t].start();
        }
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t].join();
        }
        final long end = System.nanoTime();
        long latency_sum = 0;
        for (int t = 0; t < latencies.length; t++) {
            if (latencies[t] != -1) {
                latency_sum += latencies[t];
            }
        }
        double latency_avg = ((latency_sum * 1.0d) / requests.get()) / MILLIS_IN_A_NANO;
        double request_rate = ((requests.get() * 1.0d) / (end - start)) * SECONDS_IN_A_NANO;
        System.err.println(String.format("Requests/Second: %,.2f", request_rate));
        System.err.println(String.format("Average request latency: %,.2f ms", latency_avg));
        System.err.println("Error Ratio: " + failures.get() + " / " + requests.get());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Example 100 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project fabric8 by jboss-fuse.

the class InvocationTest method testOverflow.

@Test(timeout = 30 * 1000)
public void testOverflow() throws Exception {
    DispatchQueue queue = Dispatch.createQueue();
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return new HelloImpl();
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        final Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        final AtomicInteger requests = new AtomicInteger(0);
        final AtomicInteger responses = new AtomicInteger(0);
        final AtomicInteger failures = new AtomicInteger(0);
        char[] chars = new char[65 * 1024];
        final String payload = new String(chars);
        Thread[] threads = new Thread[BENCHMARK_CLIENTS];
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t] = new Thread() {

                public void run() {
                    try {
                        requests.incrementAndGet();
                        hello.hello(payload);
                        responses.incrementAndGet();
                    } catch (Throwable t) {
                        failures.incrementAndGet();
                    }
                }
            };
            threads[t].start();
        }
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t].join(10000);
            System.err.format("REQUEST: %d of %d%n", requests.get(), BENCHMARK_CLIENTS);
            System.err.format("RESPONSES: %d of %d%n", responses.get(), BENCHMARK_CLIENTS);
            assertEquals(threads[t].isAlive(), false);
        }
        assertEquals(BENCHMARK_CLIENTS, requests.get());
        assertEquals(BENCHMARK_CLIENTS, responses.get());
        assertEquals(0, failures.get());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)411 Method (java.lang.reflect.Method)232 Test (org.junit.Test)70 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 Proxy (java.lang.reflect.Proxy)28 ArrayList (java.util.ArrayList)25 Map (java.util.Map)23 IOException (java.io.IOException)19 Field (java.lang.reflect.Field)19 HashMap (java.util.HashMap)18 AccessibleObject (java.lang.reflect.AccessibleObject)16 List (java.util.List)16 BindingProvider (javax.xml.ws.BindingProvider)14 PersistentClass (org.hibernate.mapping.PersistentClass)12 RootClass (org.hibernate.mapping.RootClass)12 Before (org.junit.Before)10 Connection (java.sql.Connection)9 LinkedHashMap (java.util.LinkedHashMap)9 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)8 DexMakerTest (com.android.dx.DexMakerTest)7