Search in sources :

Example 1 with GwtTestRpcException

use of com.googlecode.gwt.test.exceptions.GwtTestRpcException in project gwt-test-utils by gwt-test-utils.

the class GwtRpcInvocationHandler method invoke.

@SuppressWarnings("unchecked")
public Object invoke(Object proxy, Method method, Object[] args) {
    Object[] subArgs = new Object[args.length - 1];
    for (int i = 0; i < args.length - 1; i++) {
        subArgs[i] = args[i];
    }
    final AsyncCallback<Object> callback = (AsyncCallback<Object>) args[args.length - 1];
    final Method m = methodTable.get(method);
    Command asyncCallbackCommand;
    if (m == null) {
        logger.error("Method not found " + method);
        // error 500 async call
        asyncCallbackCommand = new Command() {

            public void execute() {
                callback.onFailure(new StatusCodeException(500, "No method found"));
            }
        };
    } else {
        try {
            logger.debug("Invoking " + m + " on " + target.getClass().getName());
            List<RemoteServiceExecutionHandler> handlers = GwtConfig.get().getModuleRunner().getRemoteServiceExecutionHandlers();
            // notify
            for (RemoteServiceExecutionHandler handler : handlers) {
                handler.beforeRpcRequestSerialization(m, subArgs);
            }
            // Serialize objects
            Object[] serializedArgs = new Object[subArgs.length];
            for (int i = 0; i < subArgs.length; i++) {
                try {
                    serializedArgs[i] = serializerHander.serializeUnserialize(subArgs[i]);
                } catch (Exception e) {
                    throw new GwtTestRpcException("Error while serializing argument " + i + " of type " + subArgs[i].getClass().getName() + " in method " + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "(..)", e);
                }
            }
            // notify
            for (RemoteServiceExecutionHandler handler : handlers) {
                handler.beforeRpcRequestSerialization(m, serializedArgs);
            }
            AbstractRemoteServiceServletPatcher.currentCalledMethod = m;
            // notify
            for (RemoteServiceExecutionHandler handler : handlers) {
                handler.beforeRpcMethodExecution(m, serializedArgs);
            }
            Object resultObject = m.invoke(target, serializedArgs);
            // notify
            for (RemoteServiceExecutionHandler handler : handlers) {
                handler.afterRpcMethodExecution(m, resultObject);
            }
            Object returnObject;
            try {
                // notify
                for (RemoteServiceExecutionHandler handler : handlers) {
                    handler.beforeRpcResponseSerialization(m, resultObject);
                }
                returnObject = serializerHander.serializeUnserialize(resultObject);
                // notify
                for (RemoteServiceExecutionHandler handler : handlers) {
                    handler.afterRpcResponseSerialization(m, returnObject);
                }
            } catch (Exception e) {
                throw new GwtTestRpcException("Error while serializing object of type " + resultObject.getClass().getName() + " which was returned from RPC Service " + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "(..)", e);
            }
            final Object o = returnObject;
            // success async call
            asyncCallbackCommand = new Command() {

                public void execute() {
                    logger.debug("Result of " + m.getName() + " : " + o);
                    callback.onSuccess(o);
                }
            };
        } catch (final InvocationTargetException e) {
            if (GwtTestException.class.isInstance(e.getCause())) {
                throw (GwtTestException) e.getCause();
            }
            asyncCallbackCommand = new Command() {

                public void execute() {
                    logger.info("Exception when invoking service throw to handler " + e.getMessage());
                    exceptionHandler.handle(e.getCause(), callback);
                }
            };
        } catch (final IllegalAccessException e) {
            asyncCallbackCommand = new Command() {

                public void execute() {
                    logger.error("GWT RPC exception : " + e.toString(), e);
                    callback.onFailure(new StatusCodeException(500, e.toString()));
                }
            };
        } catch (final IllegalArgumentException e) {
            asyncCallbackCommand = new Command() {

                public void execute() {
                    logger.error("GWT RPC exception : " + e.toString(), e);
                    callback.onFailure(new StatusCodeException(500, e.toString()));
                }
            };
        }
    }
    // delegate the execution to the Browser simulator
    BrowserSimulatorImpl.get().recordAsyncCall(asyncCallbackCommand);
    // async callback always return void
    return null;
}
Also used : AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) RemoteServiceExecutionHandler(com.googlecode.gwt.test.RemoteServiceExecutionHandler) StatusCodeException(com.google.gwt.user.client.rpc.StatusCodeException) Method(java.lang.reflect.Method) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) GwtTestRpcException(com.googlecode.gwt.test.exceptions.GwtTestRpcException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StatusCodeException(com.google.gwt.user.client.rpc.StatusCodeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Command(com.google.gwt.user.client.Command) GwtTestRpcException(com.googlecode.gwt.test.exceptions.GwtTestRpcException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException)

Example 2 with GwtTestRpcException

use of com.googlecode.gwt.test.exceptions.GwtTestRpcException in project gwt-test-utils by gwt-test-utils.

the class SerializableModifier method readObject.

public static void readObject(Serializable ex, ObjectInputStream ois) {
    try {
        // call the default read method
        ois.defaultReadObject();
        // keep non transient/static/final value somhere
        Map<Field, Object> buffer = getFieldValues(ex);
        // call the exported default constructor to reinitialise triansient
        // field
        // values
        // which are not expected to be null
        GwtReflectionUtils.callPrivateMethod(ex, DEFAULT_CONS_METHOD_NAME);
        // set the kept field values
        for (Map.Entry<Field, Object> entry : buffer.entrySet()) {
            entry.getKey().set(ex, entry.getValue());
        }
    } catch (Exception e) {
        throw new GwtTestRpcException("Error during deserialization of object " + ex.getClass().getName(), e);
    }
}
Also used : Field(java.lang.reflect.Field) GwtTestRpcException(com.googlecode.gwt.test.exceptions.GwtTestRpcException) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) GwtTestRpcException(com.googlecode.gwt.test.exceptions.GwtTestRpcException)

Example 3 with GwtTestRpcException

use of com.googlecode.gwt.test.exceptions.GwtTestRpcException in project gwt-test-utils by gwt-test-utils.

the class RemoteServiceCreateHandler method getRemoveServiceRelativePath.

private String getRemoveServiceRelativePath(Class<?> clazz) {
    CtClass ctClass = GwtClassPool.getCtClass((clazz));
    Object[] annotations = ctClass.getAvailableAnnotations();
    for (Object o : annotations) {
        if (Proxy.isProxyClass(o.getClass())) {
            AnnotationImpl annotation = (AnnotationImpl) Proxy.getInvocationHandler(o);
            if (annotation.getTypeName().equals(RemoteServiceRelativePath.class.getName())) {
                return ((StringMemberValue) annotation.getAnnotation().getMemberValue("value")).getValue();
            }
        }
    }
    throw new GwtTestRpcException("Cannot find the '@" + RemoteServiceRelativePath.class.getSimpleName() + "' annotation on RemoteService interface '" + clazz.getName() + "'");
}
Also used : CtClass(javassist.CtClass) GwtTestRpcException(com.googlecode.gwt.test.exceptions.GwtTestRpcException) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationImpl(javassist.bytecode.annotation.AnnotationImpl) RemoteServiceRelativePath(com.google.gwt.user.client.rpc.RemoteServiceRelativePath)

Aggregations

GwtTestRpcException (com.googlecode.gwt.test.exceptions.GwtTestRpcException)3 Command (com.google.gwt.user.client.Command)1 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 RemoteServiceRelativePath (com.google.gwt.user.client.rpc.RemoteServiceRelativePath)1 StatusCodeException (com.google.gwt.user.client.rpc.StatusCodeException)1 RemoteServiceExecutionHandler (com.googlecode.gwt.test.RemoteServiceExecutionHandler)1 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CtClass (javassist.CtClass)1 AnnotationImpl (javassist.bytecode.annotation.AnnotationImpl)1 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)1