KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > limitagent > TimerLimitAgentManager


1 package org.enhydra.shark.limitagent;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Timer JavaDoc;
6 import java.util.TimerTask JavaDoc;
7 import org.enhydra.shark.api.RootException;
8 import org.enhydra.shark.api.SharkTransaction;
9 import org.enhydra.shark.api.internal.limitagent.LimitAgent;
10 import org.enhydra.shark.api.internal.limitagent.LimitAgentException;
11 import org.enhydra.shark.api.internal.limitagent.LimitAgentManager;
12 import org.enhydra.shark.api.internal.limitagent.LimitAgentSession;
13 import org.enhydra.shark.api.internal.working.CallbackUtilities;
14
15 /**
16  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
17  * @since Mar 24, 2004
18  */

19 public class TimerLimitAgentManager implements LimitAgentManager {
20
21    protected CallbackUtilities cus;
22    protected Map JavaDoc registeredObjects;
23
24    public void configure(CallbackUtilities cus) throws RootException {
25       this.registeredObjects = new HashMap JavaDoc();
26       this.cus = cus;
27    }
28
29    public void checkLimits (SharkTransaction t) throws LimitAgentException {
30       throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager");
31    }
32
33    public void checkLimits (SharkTransaction t,String JavaDoc procId) throws LimitAgentException {
34       throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager");
35    }
36
37    public void checkProcessLimit (SharkTransaction t,String JavaDoc procId) throws LimitAgentException {
38       throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager");
39    }
40
41    public void checkActivityLimit (SharkTransaction t,String JavaDoc procId,String JavaDoc actId) throws LimitAgentException {
42       throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager");
43    }
44
45    public void notifyStop(String JavaDoc procId,String JavaDoc actId) throws LimitAgentException {
46       if (procId == null && actId == null) {
47          cus.error("Cannot locate limit schedule for a null WfExecutionObject ID!");
48          throw new LimitAgentException("Invalid WfExecutionObject ID; cannot locate scheduled limit");
49       }
50
51       // get the timer
52
Timer JavaDoc timer;
53       if (actId!=null) {
54          timer=(Timer JavaDoc) registeredObjects.remove(actId);
55       } else {
56          timer=(Timer JavaDoc) registeredObjects.remove(procId);
57       }
58       if (timer != null) {
59          timer.cancel();
60       }
61    }
62
63    public void notifyStart(String JavaDoc procId,String JavaDoc actId, Map JavaDoc context, long runtime) throws LimitAgentException {
64       if (procId == null && actId == null) {
65          cus.error("Cannot create limit schedule for a null WfExecutionObject ID!");
66          throw new LimitAgentException("Invalid WfExecutionObject ID; cannot schedule limit");
67       }
68
69       // parse the extended attributes
70
//ExtendedAttributes attrs = this.readExtendedAttributes(extAttrStr);
71

72       // create the session
73
LimitAgentSession session = new TimerLimitAgentSession(procId, actId, context, runtime);
74
75       // create the timer
76
long delay = runtime - System.currentTimeMillis();
77       if (delay <= 0) {
78          cus.debug("Limit for this object has already been reached; running now");
79          delay = 1;
80       }
81       Timer JavaDoc timer = new Timer JavaDoc();
82       timer.schedule(new LimitTimer(session), delay);
83       cus.debug("Limit Timer scheduled in [" + delay + "ms]");
84
85       // register the timer with the in-memory map
86
if (actId!=null) {
87          registeredObjects.put(actId, timer);
88       } else {
89          registeredObjects.put(procId, timer);
90       }
91    }
92
93    class LimitTimer extends TimerTask JavaDoc {
94
95       private LimitAgentSession session;
96
97       private LimitTimer(LimitAgentSession session) {
98          this.session = session;
99       }
100
101       public void run() {
102          LimitAgent agent = new TimerLimitAgent();
103          try {
104             agent.configure(cus);
105          } catch (RootException e) {
106             cus.error("Unable to configure the LimitAgent", e);
107          }
108          try {
109             agent.invoke(session);
110          } catch (LimitAgentException e) {
111             cus.error("Problem invoking limit notification agent", e);
112          }
113       }
114    }
115 }
116
Popular Tags