KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > AbstractActivity


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
23 import java.util.concurrent.CountDownLatch JavaDoc;
24 import java.util.concurrent.TimeUnit JavaDoc;
25
26 /**
27  * A useful base class which allows simple bean activities to be written easily.
28  * When this activity is started it will listen to all the state values which
29  * can be found by the introspector (such as all the fields by default) calling
30  * the {@link run} method when the state changes so that the activity can be
31  * evaluted.
32  *
33  * @version $Revision: $
34  */

35 public abstract class AbstractActivity implements Runnable JavaDoc, Activity {
36
37     private State<Transitions> state = new DefaultState<Transitions>(Transitions.Initialised);
38     private Introspector introspector = new FieldIntrospector();
39     private String JavaDoc failedReason;
40     private Throwable JavaDoc failedException;
41
42     /**
43      * Starts the activity
44      */

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     /**
53      * Stops the activity
54      */

55     public void stop() {
56         if (! state.isAny(Transitions.Stopped, Transitions.Failed)) {
57             state.set(Transitions.Stopped);
58             doStop();
59         }
60     }
61
62     /**
63      * Stops the activity with a failed state, giving the reason for the failure
64      */

65     public void fail(String JavaDoc reason) {
66         this.failedReason = reason;
67         state.set(Transitions.Failed);
68         doStop();
69     }
70
71     /**
72      * Stops the activity with a failed state with the given reason and
73      * exception.
74      */

75     public void fail(String JavaDoc message, Throwable JavaDoc e) {
76         fail(message);
77         this.failedException = e;
78     }
79
80     /**
81      * Returns the current running state of this activity
82      */

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     /**
96      * Returns the reason for the failure
97      */

98     public String JavaDoc getFailedReason() {
99         return failedReason;
100     }
101
102     /**
103      * Returns the exception which caused the failure
104      */

105     public Throwable JavaDoc getFailedException() {
106         return failedException;
107     }
108
109     /**
110      * A helper method to add a task to fire when the activity is completed
111      */

112     public void onStop(final Runnable JavaDoc runnable) {
113         getState().addRunnable(new Runnable JavaDoc() {
114
115             public void run() {
116                 if (isStopped()) {
117                     runnable.run();
118                 }
119             }
120         });
121     }
122
123     /**
124      * A helper method to add a task to fire when the activity fails
125      */

126     public void onFailure(final Runnable JavaDoc runnable) {
127         getState().addRunnable(new Runnable JavaDoc() {
128
129             public void run() {
130                 if (isFailed()) {
131                     runnable.run();
132                 }
133             }
134         });
135     }
136
137     /**
138      * A helper method to block the calling thread until the activity completes
139      */

140     public void join() {
141         final CountDownLatch JavaDoc latch = new CountDownLatch JavaDoc(1);
142         onStop(new Runnable JavaDoc() {
143             public void run() {
144                 latch.countDown();
145             }
146         });
147         while (!isStopped()) {
148             try {
149                 latch.await(2, TimeUnit.SECONDS);
150             }
151             catch (InterruptedException JavaDoc e) {
152                 Thread.currentThread().interrupt();
153             }
154         }
155     }
156     
157     /**
158      * A helper method to block the calling thread up until some maximum timeout until the activity
159      * completes or the timeout expires
160      *
161      * @return true if the activity stopped within the given time or false if not.
162      */

163     public boolean join(int time, TimeUnit JavaDoc unit) {
164         final CountDownLatch JavaDoc latch = new CountDownLatch JavaDoc(1);
165         onStop(new Runnable JavaDoc() {
166             public void run() {
167                 latch.countDown();
168             }
169         });
170         if (!isStopped()) {
171             try {
172                 latch.await(time, unit);
173             }
174             catch (InterruptedException JavaDoc e) {
175                 Thread.currentThread().interrupt();
176             }
177         }
178         return isStopped();
179     }
180
181     // Implementation methods
182
// -------------------------------------------------------------------------
183
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 JavaDoc listener) {
200         if (introspector != null) {
201             Iterator JavaDoc<State> iter = introspector.iterator(this);
202             while (iter.hasNext()) {
203                 iter.next().addRunnable(listener);
204             }
205         }
206     }
207
208     protected void removeListeners(Runnable JavaDoc listener) {
209         if (introspector != null) {
210             Iterator JavaDoc<State> iter = introspector.iterator(this);
211             while (iter.hasNext()) {
212                 iter.next().removeRunnable(listener);
213             }
214         }
215     }
216 }
217
Popular Tags