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