Example usage for com.google.gwt.user.client.rpc InvocationException InvocationException

List of usage examples for com.google.gwt.user.client.rpc InvocationException InvocationException

Introduction

In this page you can find the example usage for com.google.gwt.user.client.rpc InvocationException InvocationException.

Prototype

public InvocationException(String s) 

Source Link

Document

Constructs an exception with the given description.

Usage

From source file:com.gdevelop.gwt.syncrpc.RemoteServiceInvocationHandler.java

License:Apache License

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    RemoteServiceSyncProxy syncProxy = createSyncProxy();
    Class remoteServiceInft = method.getDeclaringClass();
    for (Class intf : proxy.getClass().getInterfaces()) {
        if (RemoteService.class.isAssignableFrom(intf)) {
            remoteServiceInft = intf;/*from  w w w.  java  2  s . co  m*/
        }
    }
    SerializationStreamWriter streamWriter = syncProxy.createStreamWriter();
    AsyncCallback 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) args[paramCount];
            if (asyncCallbackWrapperSupplier != null) {
                callback = asyncCallbackWrapperSupplier.get().wrap(callback);
            }
            // 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]));
        }
        // 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 callback_2 = callback;
            new Thread() {
                public void run() {
                    Object result;
                    try {
                        result = syncProxy_2.doInvoke(getReaderFor(returnType_2), payload_2);
                        if (callback_2 != null) {
                            callback_2.onSuccess(result);
                        }
                    } catch (Throwable e) {
                        if (callback_2 != null) {
                            callback_2.onFailure(e);
                        }
                    }
                }
            }.start();
            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 new InvocationException("Exception while invoking the remote service "
                + method.getDeclaringClass().getName() + "." + method.getName(), ex);
    }
}

From source file:com.google.appinventor.client.youngandroid.CodeblocksConnection.java

License:Open Source License

/**
 * Creates an AsyncCallback<Boolean> that wraps the given callback.
 * If onSuccess is called with false, it will call onFailure.
 *
 * @param callback an optional callback to receive success or failure
 *//* w ww . ja  va2 s  . c  o  m*/
private static AsyncCallback<Boolean> createBooleanCallbackWrapper(final AsyncCallback<Void> voidCallback) {
    return new AsyncCallback<Boolean>() {
        @Override
        public void onSuccess(Boolean okay) {
            if (okay) {
                if (voidCallback != null) {
                    voidCallback.onSuccess(null);
                }
            } else {
                // TODO(lizlooney,sharon) - make codeblocks indicate failure in a way that triggers
                // onFailure below with an appropriate Throwable so we know what happened.
                if (voidCallback != null) {
                    voidCallback
                            .onFailure(new InvocationException("Received false response from blocks editor"));
                }
            }
        }

        @Override
        public void onFailure(Throwable caught) {
            if (voidCallback != null) {
                voidCallback.onFailure(caught);
            }
        }
    };
}

From source file:com.google.appinventor.client.youngandroid.CodeblocksManager.java

License:Open Source License

private boolean checkConnection(boolean codeblocksMustBeOpen, AsyncCallback<Void> callback) {
    if (conn == null) {
        if (codeblocksMustBeOpen) {
            // If codeblocks is not open, tell the user to open it.
            // The user can resolve the issue by opening
            // the codeblocks editor.
            ErrorReporter.reportInfo(MESSAGES.noCodeblocksConnection());
            if (callback != null) {
                callback.onFailure(new InvocationException(MESSAGES.noCodeblocksConnection()));
            }//w  ww .  j a v a2 s .c om
        } else {
            // It's ok if codeblocks is not open.
            if (callback != null) {
                callback.onSuccess(null);
            }
        }
        return false;

    } else if (unresponsiveConnection) {
        // Codeblocks was open, but has been closed or is unresponsive.
        if (codeblocksMustBeOpen) {
            // If codeblocks has been closed or is unresponsive, report that.
            // The user can resolve this problem by restarting the
            // codeblocks editor.
            ErrorReporter.reportInfo(MESSAGES.codeblocksConnectionUnresponsive());
            if (callback != null) {
                callback.onFailure(new InvocationException(MESSAGES.codeblocksConnectionUnresponsive()));
            }
        } else {
            // It's ok if codeblocks has been closed or is unresponsive.
            if (callback != null) {
                callback.onSuccess(null);
            }
        }
        return false;
    }

    // Codeblocks is open and responsive.
    return true;
}

From source file:com.gwtplatform.mvp.rebind.PlaceTokenRegistryGenerator.java

License:Apache License

/**
 * Checks if the given place tokens are valid.
 *///from   w  w  w.  j  a  v a 2  s  . c o  m
private static void checkPlaces(final Map<String, JClassType> placeTokens) {
    for (Map.Entry<String, JClassType> entry : placeTokens.entrySet()) {
        if (!entry.getKey().startsWith("/") && !entry.getKey().startsWith("!/")) {
            throw new InvocationException("The token '" + entry.getKey() + "' of '"
                    + entry.getValue().getQualifiedSourceName() + "' should start with a '/' or '!/'!");
        }
    }
}

From source file:com.seanchenxi.resteasy.autobean.client.BaseResponseHandler.java

License:Apache License

@Override
protected void handleResponse(Response response, Class<T> clazz, AsyncCallback<T> callback,
        String resourceName) {//from ww  w  .ja v a  2s  .co m
    T result = null;
    Throwable caught = null;
    try {
        RESTResponse restResponse = REST.decodeResponse(response.getText());
        int statusCode = response.getStatusCode();
        if (statusCode != Response.SC_OK) {
            caught = new StatusCodeException(statusCode, response.getText());
        } else if (restResponse == null) {
            caught = new InvocationException(
                    "No response payload from " + uri + " of resource " + resourceName);
        } else if (REST.isReturnValue(restResponse)) {
            if (!Void.class.equals(clazz)) {
                Splittable data = StringQuoter.split(restResponse.getPayload());
                if (data != null)
                    result = (T) REST.decodeData(clazz, data);
            }
        } else if (REST.isThrowable(restResponse)) {
            caught = REST.decodeThrowable(restResponse.getPayload());
        } else {
            caught = new InvocationException(restResponse + " from " + uri + " of resource " + resourceName);
        }
    } catch (SerializationException e) {
        caught = new IncompatibleRemoteServiceException(
                "The response: \n{" + response.getText() + "}\n could not be deserialized", e);
    } catch (Throwable e) {
        caught = e;
    }

    if (caught != null) {
        callback.onFailure(caught);
    } else {
        callback.onSuccess(result);
    }
}

From source file:org.rapla.rest.gwtjsonrpc.client.impl.JsonCall.java

License:Apache License

protected void processResponse(final int sc, final String responseText, final String statusText,
        String contentType) {/*from w w w.j a v a 2 s  .  c  o  m*/
    if (isJsonBody(contentType)) {
        final RpcResult r;
        try {
            r = parse(jsonParser, responseText);
        } catch (RuntimeException e) {
            RpcCompleteEvent.fire(this);
            callback.onFailure(new InvocationException("Bad JSON response: " + e));
            return;
        }

        if (r.error() != null) {
            Exception e = null;
            final ExceptionDeserializer exceptionDeserializer = proxy.getExceptionDeserializer();
            if (exceptionDeserializer != null) {
                final RpcError error = r.error();
                final Data data = error.data();
                final String exception = data != null ? data.exception() : null;
                final String message = error.message();
                String[] paramObj = data.params();
                final List<String> parameter = paramObj != null ? Arrays.asList(paramObj) : null;
                e = exceptionDeserializer.deserialize(exception, message, parameter);
            }
            if (e == null) {
                final String errmsg = r.error().message();
                e = new RemoteJsonException(errmsg, r.error().code(), new JSONObject(r.error()).get("data"));
            }
            RpcCompleteEvent.fire(this);
            callback.onFailure(e);
            return;
        }

        if (sc == Response.SC_OK) {
            RpcCompleteEvent.fire(this);
            JsonUtil.invoke(resultDeserializer, callback, r);
            return;
        }
    }

    if (sc == Response.SC_OK) {
        RpcCompleteEvent.fire(this);
        callback.onFailure(new InvocationException("No JSON response"));
    } else {
        RpcCompleteEvent.fire(this);

        callback.onFailure(new StatusCodeException(sc, statusText));
    }
}

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;/*  w ww  . ja  va2  s.  com*/
        }
    }

    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;
    }
}