Coverage Report - net.sf.sapjcosupport.AsyncMethodCall
 
Classes in this File Line Coverage Branch Coverage Complexity
AsyncMethodCall
0% 
0% 
2,333
 
 1  
 package net.sf.sapjcosupport;
 2  
 
 3  
 /**
 4  
  * Utility class to execute a method call asynchronously.
 5  
  * <p/>
 6  
  * This asynchronous method invoker uses a 'pull' mechanism to await completion.
 7  
  * The client code just calls {@link #start()} and
 8  
  * continues doing what it needs to do. When it is ready to wait for completion of the async method,
 9  
  * it calls {@link #waitForCompletion()}.
 10  
  *
 11  
  * @author Niki Driessen
 12  
  * @since 1.0
 13  
  */
 14  0
 public abstract class AsyncMethodCall extends Thread {
 15  
     /**
 16  
      * A semaphore to synchronize the start and end of the method call
 17  
      */
 18  0
     private final Object semaphore = new Object();
 19  
     private boolean done;
 20  
 
 21  
     /**
 22  
      * Implementation code of the method call.
 23  
      * <p/>
 24  
      * This is the code that gets executed asynchronously, when {@link #start()}
 25  
      * is called.
 26  
      *
 27  
      * @see #start()
 28  
      */
 29  
     protected abstract void executeMethod();
 30  
 
 31  
     /**
 32  
      * This method is a subclassed {@link Thread#run()} method
 33  
      * that handles executing the actual method.
 34  
      *
 35  
      * @see #executeMethod()
 36  
      * @see Thread#start()
 37  
      * @see Thread#stop()
 38  
      * @see Thread#Thread(ThreadGroup,
 39  
      *      Runnable, String)
 40  
      * @see Runnable#run()
 41  
      */
 42  
     public void run() {
 43  0
         synchronized (semaphore) {
 44  
             try {
 45  0
                 executeMethod();
 46  0
             } catch (Throwable t) {
 47  0
                 t.printStackTrace();
 48  
             } finally {
 49  0
                 done = true;
 50  0
                 semaphore.notifyAll();
 51  0
             }
 52  0
         }
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Synchronization point for the asynchronous method.
 57  
      * <p/>
 58  
      * Call this method when you are ready to wait for the async method to
 59  
      * complete its work. This method will block until {@link #executeMethod()} is finished
 60  
      * (which can be the case when this method is called, and it will return immediately...)
 61  
      */
 62  
     public void waitForCompletion() {
 63  0
         synchronized (semaphore) {
 64  0
             while (!done) {
 65  
                 try {
 66  0
                     semaphore.wait();
 67  0
                 } catch (Exception e) {
 68  0
                     throw new RuntimeException(e.getMessage());
 69  0
                 }
 70  
             }
 71  0
             done = false;
 72  0
         }
 73  0
     }
 74  
 
 75  
 }