1 17 package org.apache.servicemix.beanflow; 18 19 import org.apache.servicemix.beanflow.support.FieldIntrospector; 20 import org.apache.servicemix.beanflow.support.Introspector; 21 22 import java.util.Iterator ; 23 import java.util.concurrent.CountDownLatch ; 24 import java.util.concurrent.TimeUnit ; 25 26 35 public abstract class AbstractActivity implements Runnable , Activity { 36 37 private State<Transitions> state = new DefaultState<Transitions>(Transitions.Initialised); 38 private Introspector introspector = new FieldIntrospector(); 39 private String failedReason; 40 private Throwable failedException; 41 42 45 public void start() { 46 if (state.compareAndSet(Transitions.Initialised, Transitions.Starting)) { 47 doStart(); 48 state.compareAndSet(Transitions.Starting, Transitions.Started); 49 } 50 } 51 52 55 public void stop() { 56 if (! state.isAny(Transitions.Stopped, Transitions.Failed)) { 57 state.set(Transitions.Stopped); 58 doStop(); 59 } 60 } 61 62 65 public void fail(String reason) { 66 this.failedReason = reason; 67 state.set(Transitions.Failed); 68 doStop(); 69 } 70 71 75 public void fail(String message, Throwable e) { 76 fail(message); 77 this.failedException = e; 78 } 79 80 83 public State<Transitions> getState() { 84 return state; 85 } 86 87 public boolean isStopped() { 88 return state.isAny(Transitions.Stopped, Transitions.Failed); 89 } 90 91 public boolean isFailed() { 92 return state.is(Transitions.Failed); 93 } 94 95 98 public String getFailedReason() { 99 return failedReason; 100 } 101 102 105 public Throwable getFailedException() { 106 return failedException; 107 } 108 109 112 public void onStop(final Runnable runnable) { 113 getState().addRunnable(new Runnable () { 114 115 public void run() { 116 if (isStopped()) { 117 runnable.run(); 118 } 119 } 120 }); 121 } 122 123 126 public void onFailure(final Runnable runnable) { 127 getState().addRunnable(new Runnable () { 128 129 public void run() { 130 if (isFailed()) { 131 runnable.run(); 132 } 133 } 134 }); 135 } 136 137 140 public void join() { 141 final CountDownLatch latch = new CountDownLatch (1); 142 onStop(new Runnable () { 143 public void run() { 144 latch.countDown(); 145 } 146 }); 147 while (!isStopped()) { 148 try { 149 latch.await(2, TimeUnit.SECONDS); 150 } 151 catch (InterruptedException e) { 152 Thread.currentThread().interrupt(); 153 } 154 } 155 } 156 157 163 public boolean join(int time, TimeUnit unit) { 164 final CountDownLatch latch = new CountDownLatch (1); 165 onStop(new Runnable () { 166 public void run() { 167 latch.countDown(); 168 } 169 }); 170 if (!isStopped()) { 171 try { 172 latch.await(time, unit); 173 } 174 catch (InterruptedException e) { 175 Thread.currentThread().interrupt(); 176 } 177 } 178 return isStopped(); 179 } 180 181 protected void doStart() { 184 addListeners(this); 185 } 186 187 protected void doStop() { 188 removeListeners(this); 189 } 190 191 protected Introspector getIntrospector() { 192 return introspector; 193 } 194 195 protected void setIntrospector(Introspector introspector) { 196 this.introspector = introspector; 197 } 198 199 protected void addListeners(Runnable listener) { 200 if (introspector != null) { 201 Iterator <State> iter = introspector.iterator(this); 202 while (iter.hasNext()) { 203 iter.next().addRunnable(listener); 204 } 205 } 206 } 207 208 protected void removeListeners(Runnable listener) { 209 if (introspector != null) { 210 Iterator <State> iter = introspector.iterator(this); 211 while (iter.hasNext()) { 212 iter.next().removeRunnable(listener); 213 } 214 } 215 } 216 } 217 | Popular Tags |