1
2
3
4
5
6
7
8
9
10
11 package org.mule.api;
12
13 import org.mule.DefaultMuleMessage;
14 import org.mule.util.concurrent.DaemonThreadFactory;
15
16 import java.util.List;
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 List transformers;
63
64 protected MuleContext muleContext;
65
66 public FutureMessageResult(Callable callable, MuleContext muleContext)
67 {
68 super(callable);
69 this.executor = DefaultExecutor;
70 this.muleContext = muleContext;
71 }
72
73
74
75
76
77
78
79 public void setExecutor(Executor e)
80 {
81 if (e == null)
82 {
83 throw new IllegalArgumentException("Executor must not be null.");
84 }
85
86 synchronized (this)
87 {
88 this.executor = e;
89 }
90 }
91
92
93
94
95
96
97
98 public void setTransformers(List t)
99 {
100 synchronized (this)
101 {
102 this.transformers = t;
103 }
104 }
105
106 public MuleMessage getMessage() throws InterruptedException, ExecutionException, MuleException
107 {
108 return this.getMessage(this.get());
109 }
110
111 public MuleMessage getMessage(long timeout)
112 throws InterruptedException, ExecutionException, TimeoutException, MuleException
113 {
114 return this.getMessage(this.get(timeout, TimeUnit.MILLISECONDS));
115 }
116
117 private MuleMessage getMessage(Object obj) throws MuleException
118 {
119 MuleMessage result = null;
120 if (obj != null)
121 {
122 if (obj instanceof MuleMessage)
123 {
124 result = (MuleMessage)obj;
125 }
126 else
127 {
128 result = new DefaultMuleMessage(obj, muleContext);
129 }
130
131 synchronized (this)
132 {
133 if (transformers != null)
134 {
135 result.applyTransformers(null, transformers, null);
136 }
137 }
138
139 }
140 return result;
141 }
142
143
144
145
146 public void execute()
147 {
148 synchronized (this)
149 {
150 executor.execute(this);
151 }
152 }
153
154 }