KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > timer > def > Alarm


1 package org.jbpm.bpel.timer.def;
2
3 import java.util.Date;
4
5 import org.apache.commons.lang.enum.Enum;
6
7 import org.jbpm.graph.def.Action;
8 import org.jbpm.graph.exe.ExecutionContext;
9 import org.jbpm.graph.exe.Token;
10 import org.jbpm.scheduler.exe.SchedulerInstance;
11 import org.jbpm.scheduler.exe.Timer;
12
13 import org.jbpm.bpel.data.def.Snippet;
14 import org.jbpm.bpel.def.Activity;
15 import org.jbpm.bpel.xml.util.DatatypeUtil;
16 import org.jbpm.bpel.xml.util.Duration;
17
18 /**
19  * @author Juan Cantú
20  * @version $Revision: 1.7 $ $Date: 2005/06/23 02:22:46 $
21  */

22 public class Alarm extends Action {
23   
24   private static final long serialVersionUID = 1L;
25
26   public static final String ALARM_NAME =":alarm";
27   
28   private long id;
29   private AlarmKind alarmKind;
30   private Snippet forExpr;
31   private Snippet untilExpr;
32   private Snippet repeatEveryExpr;
33   private Activity alarmActivity;
34   
35   public Alarm() {
36   }
37   
38   public void startTimer(Token token) {
39     Date dueDate = null;
40     Duration repeat = null;
41     
42     Timer timer = new Timer(token);
43     timer.setName(ALARM_NAME + id);
44
45     //calculate repeat every duration if not null
46
if(repeatEveryExpr != null) {
47       String repeatLiteral = (String) repeatEveryExpr.getScript().evaluate(token);
48       repeat = Duration.parseDuration(repeatLiteral);
49       timer.setRepeat((repeat.getTime().getTime() / 1000) + " seconds");
50     }
51     
52     //either for or until expression is calculated
53
if(forExpr != null) {
54       String durationLiteral = (String) forExpr.getScript().evaluate(token);
55       dueDate = Duration.parseDuration(durationLiteral).getTime();
56     }
57     else if(untilExpr != null) {
58       String deadlineLiteral = (String) untilExpr.getScript().evaluate(token);
59       dueDate = DatatypeUtil.parseDateTime(deadlineLiteral).getTime();
60     }
61     
62     timer.setDueDate(dueDate);
63     timer.setAction(this);
64     ExecutionContext executionContext = null;
65     executionContext.getSchedulerInstance().schedule(timer);
66     SchedulerInstance schedulerInstance =
67       (SchedulerInstance) token.getProcessInstance().getInstance(SchedulerInstance.class);
68     schedulerInstance.schedule(timer);
69   }
70   
71   public void cancelTimer(Token token) {
72     SchedulerInstance schedulerInstance =
73       (SchedulerInstance) token.getProcessInstance().getInstance(SchedulerInstance.class);
74     schedulerInstance.cancel(ALARM_NAME + id, token);
75   }
76   
77   public Alarm(Activity alarmActivity) {
78     setAlarmActivity(alarmActivity);
79   }
80   
81   public void onAlarm(Token token) {
82     ((AlarmListener) alarmActivity).alarmTriggered(token, this);
83   }
84   
85   public AlarmKind getAlarmKind() {
86     return alarmKind;
87   }
88   
89   public void setAlarmKind(AlarmKind alarmKind) {
90     this.alarmKind = alarmKind;
91   }
92   
93   public Snippet getFor() {
94     return forExpr;
95   }
96
97   public void setFor(Snippet forExpr) {
98     this.forExpr = forExpr;
99   }
100   
101   public Snippet getUntil() {
102     return untilExpr;
103   }
104  
105   public void setUntil(Snippet untilExpr) {
106     this.untilExpr = untilExpr;
107   }
108   
109   public Snippet getRepeatEvery() {
110     return repeatEveryExpr;
111   }
112
113   public void setRepeatEvery(Snippet repeatEveryExpr) {
114     this.repeatEveryExpr = repeatEveryExpr;
115   }
116   
117   public Activity getAlarmActivity() {
118     return alarmActivity;
119   }
120   
121   public void setAlarmActivity(Activity alarmActivity) {
122     if (!(alarmActivity instanceof AlarmListener))
123       throw new ClassCastException("the activity must implement " + AlarmListener.class.getName());
124     this.alarmActivity = alarmActivity;
125   }
126   
127   public static class AlarmKind extends Enum {
128     
129     private static final long serialVersionUID = 1L;
130     
131     public static final AlarmKind CYCLIC = new AlarmKind("cyclic");
132     public static final AlarmKind DEADLINE = new AlarmKind("duration");
133     public static final AlarmKind DURATION = new AlarmKind("deadline");
134     
135     /**
136      * Enumeration constructor.
137      * @param name the desired textual representation.
138      */

139     private AlarmKind(String kind) {
140       super(kind);
141     }
142   }
143 }
144
Popular Tags