1 10 11 package org.mule.umo; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.Callable; 14 import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException; 15 import edu.emory.mathcs.backport.java.util.concurrent.Executor; 16 import edu.emory.mathcs.backport.java.util.concurrent.Executors; 17 import edu.emory.mathcs.backport.java.util.concurrent.FutureTask; 18 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; 19 import edu.emory.mathcs.backport.java.util.concurrent.TimeoutException; 20 21 import org.mule.impl.MuleMessage; 22 import org.mule.umo.transformer.TransformerException; 23 import org.mule.umo.transformer.UMOTransformer; 24 import org.mule.util.concurrent.DaemonThreadFactory; 25 26 31 public class FutureMessageResult extends FutureTask 33 { 34 55 private static final Executor DefaultExecutor = Executors.newSingleThreadExecutor(new DaemonThreadFactory( 56 "MuleDefaultFutureMessageExecutor")); 57 58 private Executor executor; 60 private UMOTransformer transformer; 62 63 public FutureMessageResult(Callable callable) 64 { 65 super(callable); 66 this.executor = DefaultExecutor; 67 } 68 69 74 public FutureMessageResult(Callable callable, UMOTransformer transformer) 75 { 76 this(callable); 77 this.transformer = transformer; 78 } 79 80 86 public void setTransformer(UMOTransformer t) 87 { 88 synchronized (this) 89 { 90 this.transformer = t; 91 } 92 } 93 94 100 public void setExecutor(Executor e) 101 { 102 if (e == null) 103 { 104 throw new IllegalArgumentException ("Executor must not be null."); 105 } 106 107 synchronized (this) 108 { 109 this.executor = e; 110 } 111 } 112 113 public UMOMessage getMessage() throws InterruptedException , ExecutionException, TransformerException 114 { 115 return this.getMessage(this.get()); 116 } 117 118 public UMOMessage getMessage(long timeout) 119 throws InterruptedException , ExecutionException, TimeoutException, TransformerException 120 { 121 return this.getMessage(this.get(timeout, TimeUnit.MILLISECONDS)); 122 } 123 124 private UMOMessage getMessage(Object obj) throws TransformerException 125 { 126 if (obj != null) 127 { 128 if (obj instanceof UMOMessage) 129 { 130 return (UMOMessage)obj; 131 } 132 133 synchronized (this) 134 { 135 if (transformer != null) 136 { 137 obj = transformer.transform(obj); 138 } 139 } 140 141 return new MuleMessage(obj); 142 } 143 else 144 { 145 return null; 146 } 147 } 148 149 152 public void execute() 153 { 154 synchronized (this) 155 { 156 executor.execute(this); 157 } 158 } 159 160 } 161 | Popular Tags |