Example usage for com.google.gwt.user.server.rpc.impl SerializabilityUtil getSerializedTypeName

List of usage examples for com.google.gwt.user.server.rpc.impl SerializabilityUtil getSerializedTypeName

Introduction

In this page you can find the example usage for com.google.gwt.user.server.rpc.impl SerializabilityUtil getSerializedTypeName.

Prototype

public static String getSerializedTypeName(Class<?> instanceType) 

Source Link

Usage

From source file:se.aaslin.developer.roboproxy.remote.RemoteServiceInvocationHandler.java

License:Apache License

@SuppressWarnings("unchecked")
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    RemoteServiceSyncProxy syncProxy = new RemoteServiceSyncProxy(moduleBaseURL, remoteServiceRelativePath,
            serializationPolicyName, cookieManager, context);
    Class<?> remoteServiceInft = method.getDeclaringClass();
    for (Class<?> intf : proxy.getClass().getInterfaces()) {
        if (RemoteService.class.isAssignableFrom(intf)) {
            remoteServiceInft = intf;//ww  w. jav a2  s .c o  m
        }
    }

    SerializationStreamWriter streamWriter = syncProxy.createStreamWriter();

    AsyncCallback<Object> callback = null;
    Class<?>[] paramTypes = method.getParameterTypes();
    try {
        // Determine whether sync or async
        boolean isAsync = false;
        // String serviceIntfName =
        // method.getDeclaringClass().getCanonicalName();
        String serviceIntfName = remoteServiceInft.getCanonicalName();
        int paramCount = paramTypes.length;
        Class<?> returnType = method.getReturnType();
        if (method.getDeclaringClass().getCanonicalName().endsWith("Async")) {
            isAsync = true;
            serviceIntfName = serviceIntfName.substring(0, serviceIntfName.length() - 5);
            paramCount--;
            callback = (AsyncCallback<Object>) args[paramCount];

            // Determine the return type
            Class<?>[] syncParamTypes = new Class[paramCount];
            System.arraycopy(paramTypes, 0, syncParamTypes, 0, paramCount);
            Class<?> clazz;
            try {
                clazz = Class.forName(serviceIntfName);
            } catch (ClassNotFoundException e) {
                throw new InvocationException("There are not sync version of " + serviceIntfName + "Async");
            }
            Method syncMethod = clazz.getMethod(method.getName(), syncParamTypes);
            if (syncMethod != null) {
                returnType = syncMethod.getReturnType();
            } else {
                throw new InvocationException("Sync & Async method does not match.");
            }
        }

        // Interface name
        streamWriter.writeString(serviceIntfName);
        // Method name
        streamWriter.writeString(method.getName());

        // Params count
        streamWriter.writeInt(paramCount);

        // Params type
        for (int i = 0; i < paramCount; i++) {
            // streamWriter.writeString(computeBinaryClassName(paramTypes[i]));
            streamWriter.writeString(SerializabilityUtil.getSerializedTypeName(paramTypes[i]));
        }

        // Params
        for (int i = 0; i < paramCount; i++) {
            writeParam(streamWriter, paramTypes[i], args[i]);
        }

        String payload = streamWriter.toString();
        if (isAsync) {
            final RemoteServiceSyncProxy syncProxy_2 = syncProxy;
            final Class<?> returnType_2 = returnType;
            final String payload_2 = payload;
            final AsyncCallback<Object> callback_2 = callback;
            AsyncTask<Void, Void, Object> asyncTask = new AsyncTask<Void, Void, Object>() {

                @Override
                protected Object doInBackground(Void... params) {
                    try {
                        return syncProxy_2.doInvoke(getReaderFor(returnType_2), payload_2);
                    } catch (Throwable e) {
                        return e;
                    }
                }

                @Override
                protected void onPostExecute(Object result) {
                    if (result instanceof Throwable) {
                        Throwable caught = (Throwable) result;
                        if (callback_2 != null) {
                            callback_2.onFailure(caught);
                        }
                    } else {
                        if (callback_2 != null) {
                            callback_2.onSuccess(result);
                        }
                    }
                }

            };
            asyncTask.execute();

            return null;
        } else {
            return syncProxy.doInvoke(getReaderFor(returnType), payload);
        }
        /*
         * Object result = syncProxy.doInvoke(getReaderFor(returnType),
         * payload); if (callback != null){ callback.onSuccess(result); }
         * return result;
         */
    } catch (Throwable ex) {
        if (callback != null) {
            callback.onFailure(ex);
            return null;
        }
        Class<?>[] expClasses = method.getExceptionTypes();
        for (Class<?> clazz : expClasses) {
            if (clazz.isAssignableFrom(ex.getClass())) {
                throw ex;
            }
        }

        throw ex;
    }
}