1
2
3
4
5
6
7
8
9
10
11 package org.mule.umo;
12
13 import org.mule.impl.MuleMessage;
14 import org.mule.umo.transformer.TransformerException;
15 import org.mule.umo.transformer.UMOTransformer;
16 import org.mule.util.concurrent.DaemonThreadFactory;
17
18 import edu.emory.mathcs.backport.java.util.concurrent.Callable;
19 import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
20 import edu.emory.mathcs.backport.java.util.concurrent.Executor;
21 import edu.emory.mathcs.backport.java.util.concurrent.Executors;
22 import edu.emory.mathcs.backport.java.util.concurrent.FutureTask;
23 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24 import edu.emory.mathcs.backport.java.util.concurrent.TimeoutException;
25
26
27
28
29
30
31
32 public class FutureMessageResult extends FutureTask
33 {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 private static final Executor DefaultExecutor = Executors.newSingleThreadExecutor(
56 new DaemonThreadFactory("MuleDefaultFutureMessageExecutor"));
57
58
59 private Executor executor;
60
61
62 private UMOTransformer transformer;
63
64 public FutureMessageResult(Callable callable)
65 {
66 super(callable);
67 this.executor = DefaultExecutor;
68 }
69
70
71
72
73
74
75
76 public void setExecutor(Executor e)
77 {
78 if (e == null)
79 {
80 throw new IllegalArgumentException("Executor must not be null.");
81 }
82
83 synchronized (this)
84 {
85 this.executor = e;
86 }
87 }
88
89
90
91
92
93
94
95 public void setTransformer(UMOTransformer t)
96 {
97 synchronized (this)
98 {
99 this.transformer = t;
100 }
101 }
102
103 public UMOMessage getMessage() throws InterruptedException, ExecutionException, TransformerException
104 {
105 return this.getMessage(this.get());
106 }
107
108 public UMOMessage getMessage(long timeout)
109 throws InterruptedException, ExecutionException, TimeoutException, TransformerException
110 {
111 return this.getMessage(this.get(timeout, TimeUnit.MILLISECONDS));
112 }
113
114 private UMOMessage getMessage(Object obj) throws TransformerException
115 {
116 if (obj != null)
117 {
118 if (obj instanceof UMOMessage)
119 {
120 return (UMOMessage)obj;
121 }
122
123 synchronized (this)
124 {
125 if (transformer != null)
126 {
127 obj = transformer.transform(obj);
128 }
129 }
130
131 return new MuleMessage(obj);
132 }
133 else
134 {
135 return null;
136 }
137 }
138
139
140
141
142 public void execute()
143 {
144 synchronized (this)
145 {
146 executor.execute(this);
147 }
148 }
149
150 }