Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.fusesource.fabric.dosgi.tcp;
 
 

Author(s):
Hiram Chirino
 
 public class BlockingInvocationStrategy implements InvocationStrategy {
 
     public static final BlockingInvocationStrategy INSTANCE = new BlockingInvocationStrategy();
 
     private static final Callable<ObjectEMPTY_CALLABLE = new Callable<Object>() {
         public Object call() {
             return null;
         }
     };
 
     private class BlockingResponseFuture extends FutureTask<Objectimplements ResponseFutureAsyncCallback {
 
         private final ClassLoader loader;
         private final Method method;
         private final SerializationStrategy serializationStrategy;
 
         public BlockingResponseFuture(ClassLoader loaderMethod methodSerializationStrategy serializationStrategy) {
             super();
             this. = loader;
             this. = method;
             this. = serializationStrategy;
         }
 
         public void set(DataByteArrayInputStream sourcethrows IOExceptionClassNotFoundException {
             try {
                 .decodeResponse(.getReturnType(), sourcethis);
             } catch (Throwable e) {
                 super.setException(e);
             }
         }
 
         public void fail(Throwable failure) {
             super.setException(failure);
         }
 
         public void onSuccess(Object result) {
             super.set(result);
         }
 
         public void onFailure(Throwable failure) {
             super.setException(failure);
         }
     }
 
     public ResponseFuture request(SerializationStrategy serializationStrategyClassLoader loaderMethod methodObject[] argsDataByteArrayOutputStream targetthrows Exception {
 
         assert Dispatch.getCurrentQueue() == null : "You should not do blocking RPC class when executing on a dispatch queue";
 
         serializationStrategy.encodeRequest(loadermethod.getParameterTypes(), argstarget);
         return new BlockingResponseFuture(loadermethodserializationStrategy);
     }
 
     public void service(SerializationStrategy serializationStrategyClassLoader loaderMethod methodObject targetDataByteArrayInputStream requestStreamDataByteArrayOutputStream responseStreamRunnable onComplete) {
 
         int pos = responseStream.position();
         try {
 
             Object value = null;
             Throwable error = null;
 
             try {
                 Class<?>[] types = method.getParameterTypes();
                final Object[] args = new Object[types.length];
                serializationStrategy.decodeRequest(loadertypesrequestStreamargs);
                value = method.invoke(targetargs);
            } catch (Throwable t) {
                if (t instanceof InvocationTargetException) {
                    error = t.getCause();
                } else {
                    error = t;
                }
            }
            serializationStrategy.encodeResponse(loadermethod.getReturnType(), valueerrorresponseStream);
        } catch(Exception e) {
            // we failed to encode the response.. reposition and write that error.
            try {
                responseStream.position(pos);
                serializationStrategy.encodeResponse(loadermethod.getReturnType(), nullnew RemoteException(e.toString()), responseStream);
            } catch (Exception unexpected) {
                unexpected.printStackTrace();
            }
        } finally {
            onComplete.run();
        }
    }
New to GrepCode? Check out our FAQ X