KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > GenericResultWaiter


1 /*
2  * $Id: GenericResultWaiter.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service;
26
27 import java.util.Map JavaDoc;
28
29 import org.ofbiz.base.util.Debug;
30
31 /**
32  * Generic Result Waiter Class
33  *
34  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
35  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
36  * @version $Rev: 5462 $
37  * @since 2.0
38  */

39 public class GenericResultWaiter implements GenericRequester {
40
41     public static final String JavaDoc module = GenericResultWaiter.class.getName();
42     
43     /** Status code for a running service */
44     public static final int SERVICE_RUNNING = -1;
45     /** Status code for a failed service */
46     public static final int SERVICE_FAILED = 0;
47     /** Status code for a successful service */
48     public static final int SERVICE_FINISHED = 1;
49     
50     private boolean completed = false;
51     private int status = -1;
52     private Map JavaDoc result = null;
53     private Throwable JavaDoc t = null;
54
55     /**
56      * @see org.ofbiz.service.GenericRequester#receiveResult(java.util.Map)
57      */

58     public synchronized void receiveResult(Map JavaDoc 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     /**
68      * @see org.ofbiz.service.GenericRequester#receiveThrowable(java.lang.Throwable)
69      */

70     public synchronized void receiveThrowable(Throwable JavaDoc t) {
71         this.t = t;
72         completed = true;
73         status = SERVICE_FAILED;
74         notify();
75     }
76     
77     /**
78      * Returns the status of the service.
79      * @return int Status code
80      */

81     public synchronized int status() {
82         return this.status;
83     }
84     
85     /**
86      * If the service has completed return true
87      * @return boolean
88      */

89     public synchronized boolean isCompleted() {
90         return completed;
91     }
92     
93     /**
94      * Returns the exception which was thrown or null if none
95      * @return Exception
96      */

97     public synchronized Throwable JavaDoc getThrowable() {
98         if (!isCompleted())
99             throw new java.lang.IllegalStateException JavaDoc("Cannot return exception, synchronous call has not completed.");
100         return this.t;
101     }
102
103     /**
104      * Gets the results of the service or null if none
105      * @return Map
106      */

107     public synchronized Map JavaDoc getResult() {
108         if (!isCompleted())
109             throw new java.lang.IllegalStateException JavaDoc("Cannot return result, asynchronous call has not completed.");
110         return result;
111     }
112
113     /**
114      * Waits for the service to complete
115      * @return Map
116      */

117     public synchronized Map JavaDoc waitForResult() {
118         return this.waitForResult(10);
119     }
120
121     /**
122      * Waits for the service to complete, check the status ever n milliseconds
123      * @param milliseconds
124      * @return Map
125      */

126     public synchronized Map JavaDoc 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 JavaDoc e) {
133                 Debug.logError(e, module);
134             }
135         }
136         return this.getResult();
137     }
138 }
139
140
Popular Tags