1 package org.xvsm.internal.tasks;
2
3 import java.io.Serializable;
4 import java.net.URI;
5 import java.util.Properties;
6 import java.util.concurrent.atomic.AtomicLong;
7
8 import org.xvsm.internal.ReplySenderPool;
9 import org.xvsm.internal.exceptions.FatalException;
10
11 /***
12 *
13 * @author Christian Schreiber, Michael Proestler
14 *
15 */
16 public abstract class Task implements Runnable, Serializable {
17
18 /***
19 * A boolean which indicates, that the task is done. If so, the answer field
20 * is set.
21 */
22 private boolean hasFinished = false;
23
24 /***
25 * The result of this task. This field is set, whenever the task is done.
26 */
27 private Object result = null;
28
29 /***
30 * The URL of the container for the answer.
31 */
32 private URI answerToContainer = null;
33
34 /***
35 * The aspect context.
36 */
37 private Properties aspectContext = null;
38
39 private static final AtomicLong taskIDCounter = new AtomicLong(0l);
40 private long taskID;
41
42 public Task() {
43 this.taskID = this.generateId();
44 }
45
46 public Task(long taskId){
47 this.taskID = taskId;
48 }
49
50 private long generateId() {
51
52 if (this.taskIDCounter.get() == Long.MAX_VALUE) {
53 taskIDCounter.set(0l);
54 }
55
56 return this.taskIDCounter.incrementAndGet();
57 }
58
59 public long getTaskID() {
60 return taskID;
61 }
62
63 protected void setTaskID(long taskID) {
64 this.taskID = taskID;
65 }
66
67 /***
68 * Sets the result of this task.
69 *
70 * @param o
71 * the result of this task.
72 */
73 public final synchronized void setResult(Object o) {
74 this.result = o;
75 this.hasFinished = true;
76 if (this.getAnswerToContainer() == null) {
77 notifyAll();
78 } else {
79 ReplyTask t = new ReplyTask(this);
80 ReplySenderPool.execute(t);
81 notifyAll();
82 }
83 }
84
85 /***
86 * Receives the result of this task. If no result is set, the call will
87 * block.
88 *
89 * @return the result of this task. if the waiting thread was interrupted.
90 */
91 public final Object readResult() {
92 this.waitUntilFinished();
93 return this.getResult();
94 }
95
96 /***
97 * Returns the result. The method returns <code>null</code> if the result is
98 * not available.
99 *
100 * @return the result or <code>null</code>
101 */
102 public Object getResult() {
103 return this.result;
104 }
105
106 /***
107 * Waits until the task has finished. The call will block until the task has
108 * finished.
109 *
110 */
111 public final synchronized void waitUntilFinished() {
112 while (!hasFinished) {
113 try {
114 wait();
115 } catch (InterruptedException e) {
116 throw new FatalException(e);
117 }
118 }
119 }
120
121 /***
122 * Returns <code>true</code> if the task has finished.
123 *
124 * @return <code>true</code> if the task has finished otherwise
125 * <code>false</code>.
126 */
127 public boolean hasFinished() {
128 return this.hasFinished;
129 }
130
131 /***
132 * @return the answerToContainer
133 */
134 public URI getAnswerToContainer() {
135 return answerToContainer;
136 }
137
138 /***
139 * @param answerToContainer
140 * the answerToContainer to set
141 */
142 public void setAnswerToContainer(URI answerToContainer) {
143 this.answerToContainer = answerToContainer;
144 }
145
146 /***
147 * @return the aspectContext
148 */
149 public Properties getAspectContext() {
150 return aspectContext;
151 }
152
153 /***
154 * @param aspectContext
155 * the aspectContext to set
156 */
157 public void setAspectContext(Properties aspectContext) {
158 this.aspectContext = aspectContext;
159 }
160 }