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