1 25 package org.ofbiz.service; 26 27 import java.util.Map ; 28 29 import org.ofbiz.base.util.Debug; 30 31 39 public class GenericResultWaiter implements GenericRequester { 40 41 public static final String module = GenericResultWaiter.class.getName(); 42 43 44 public static final int SERVICE_RUNNING = -1; 45 46 public static final int SERVICE_FAILED = 0; 47 48 public static final int SERVICE_FINISHED = 1; 49 50 private boolean completed = false; 51 private int status = -1; 52 private Map result = null; 53 private Throwable t = null; 54 55 58 public synchronized void receiveResult(Map result) { 59 this.result = result; 60 completed = true; 61 status = SERVICE_FINISHED; 62 notify(); 63 if (Debug.verboseOn()) 64 Debug.logVerbose("Received Result (" + completed + ") -- " + result, module); 65 } 66 67 70 public synchronized void receiveThrowable(Throwable t) { 71 this.t = t; 72 completed = true; 73 status = SERVICE_FAILED; 74 notify(); 75 } 76 77 81 public synchronized int status() { 82 return this.status; 83 } 84 85 89 public synchronized boolean isCompleted() { 90 return completed; 91 } 92 93 97 public synchronized Throwable getThrowable() { 98 if (!isCompleted()) 99 throw new java.lang.IllegalStateException ("Cannot return exception, synchronous call has not completed."); 100 return this.t; 101 } 102 103 107 public synchronized Map getResult() { 108 if (!isCompleted()) 109 throw new java.lang.IllegalStateException ("Cannot return result, asynchronous call has not completed."); 110 return result; 111 } 112 113 117 public synchronized Map waitForResult() { 118 return this.waitForResult(10); 119 } 120 121 126 public synchronized Map waitForResult(long milliseconds) { 127 if (Debug.verboseOn()) Debug.logVerbose("Waiting for results...", module); 128 while (!isCompleted()) { 129 try { 130 this.wait(milliseconds); 131 if (Debug.verboseOn()) Debug.logVerbose("Waiting...", module); 132 } catch (java.lang.InterruptedException e) { 133 Debug.logError(e, module); 134 } 135 } 136 return this.getResult(); 137 } 138 } 139 140 | Popular Tags |