Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011, FuseSource Corp. All rights reserved. http://fusesource.com The software in this package is published under the terms of the CDDL license a copy of which has been included with this distribution in the license.txt file.
  
  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 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