KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.limitagent;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
8 import org.enhydra.shark.api.RootException;
9 import org.enhydra.shark.api.SharkTransaction;
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 Sasa Bojanic
17  */

18 public class StandardLimitAgentManager implements LimitAgentManager {
19
20    protected CallbackUtilities cus;
21    protected Map JavaDoc registeredProcesses;
22    protected Map JavaDoc registeredActivities;
23    protected Map JavaDoc activitiesOfProcess;
24
25    public void configure(CallbackUtilities cus) throws RootException {
26       this.registeredProcesses = new HashMap JavaDoc();
27       this.registeredActivities = new HashMap JavaDoc();
28       this.activitiesOfProcess = new HashMap JavaDoc();
29       this.cus = cus;
30    }
31
32    public void checkLimits (SharkTransaction t) throws LimitAgentException {
33       StandardLimitAgent sla=new StandardLimitAgent();
34       try {
35          sla.configure(cus);
36       } catch (RootException e) {
37          cus.error("Unable to configure the LimitAgent", e);
38       }
39       Iterator JavaDoc processes=registeredProcesses.values().iterator();
40       while (processes.hasNext()) {
41          sla.invoke((LimitAgentSession)processes.next());
42       }
43       Iterator JavaDoc activities=registeredActivities.values().iterator();
44       while (activities.hasNext()) {
45          sla.invoke((LimitAgentSession)activities.next());
46       }
47    }
48
49    public void checkLimits (SharkTransaction t,String JavaDoc procId) throws LimitAgentException {
50       StandardLimitAgent sla=new StandardLimitAgent();
51       try {
52          sla.configure(cus);
53       } catch (RootException e) {
54          cus.error("Unable to configure the LimitAgent", e);
55       }
56       LimitAgentSession las=(LimitAgentSession)registeredProcesses.get(procId);
57       if (las!=null) {
58          sla.invoke(las);
59       }
60       Set JavaDoc s=(Set JavaDoc)activitiesOfProcess.get(procId);
61       if (s!=null) {
62          Iterator JavaDoc activities=s.iterator();
63          while (activities.hasNext()) {
64             sla.invoke((LimitAgentSession)registeredActivities.get(activities.next()));
65          }
66       }
67    }
68
69    public void checkProcessLimit (SharkTransaction t,String JavaDoc procId) throws LimitAgentException {
70       StandardLimitAgent sla=new StandardLimitAgent();
71       try {
72          sla.configure(cus);
73       } catch (RootException e) {
74          cus.error("Unable to configure the LimitAgent", e);
75       }
76       LimitAgentSession las=(LimitAgentSession)registeredProcesses.get(procId);
77       if (las!=null) {
78          sla.invoke(las);
79       }
80    }
81
82    public void checkActivityLimit (SharkTransaction t,String JavaDoc procId,String JavaDoc actId) throws LimitAgentException {
83       StandardLimitAgent sla=new StandardLimitAgent();
84       try {
85          sla.configure(cus);
86       } catch (RootException e) {
87          cus.error("Unable to configure the LimitAgent", e);
88       }
89       LimitAgentSession las=(LimitAgentSession)registeredActivities.get(actId);
90       if (las!=null) {
91          sla.invoke(las);
92       }
93    }
94
95    public void notifyStop(String JavaDoc procId,String JavaDoc actId) throws LimitAgentException {
96       if (procId == null && actId == null) {
97          cus.error("Cannot locate limit schedule for a null WfExecutionObject ID!");
98          throw new LimitAgentException("Invalid WfExecutionObject ID; cannot locate scheduled limit");
99       }
100
101       if (actId!=null && procId==null) {
102          cus.error("Cannot locate limit schedule for a null WfExecutionObject ID!");
103          throw new LimitAgentException("Invalid WfExecutionObject ID; cannot locate scheduled limit");
104       }
105
106       if (actId!=null) {
107          registeredActivities.remove(actId);
108          Set JavaDoc s=(Set JavaDoc)activitiesOfProcess.get(procId);
109          if (s!=null) {
110             s.remove(actId);
111             if (s.size()==0) {
112                registeredActivities.remove(procId);
113             }
114          }
115       } else {
116          registeredProcesses.remove(procId);
117       }
118    }
119
120    public void notifyStart(String JavaDoc procId,String JavaDoc actId, Map JavaDoc context, long runtime) throws LimitAgentException {
121       if (procId == null && actId == null) {
122          cus.error("Cannot create limit schedule for a null WfExecutionObject ID!");
123          throw new LimitAgentException("Invalid WfExecutionObject ID; cannot schedule limit");
124       }
125
126       if (actId!=null && procId==null) {
127          cus.error("Cannot locate limit schedule for a null WfExecutionObject ID!");
128          throw new LimitAgentException("Invalid WfExecutionObject ID; cannot locate scheduled limit");
129       }
130
131       // create the session
132
LimitAgentSession session = new StandardLimitAgentSession (procId, actId, context, runtime);
133
134       // register the timer with the in-memory map
135
if (actId!=null) {
136          registeredActivities.put(actId,session);
137          Set JavaDoc s=(Set JavaDoc)activitiesOfProcess.get(procId);
138          if (s==null) {
139             s=new HashSet JavaDoc();
140             activitiesOfProcess.put(procId,s);
141          }
142          s.add(actId);
143       } else {
144          registeredProcesses.put(procId,session);
145       }
146    }
147
148 }
149
Popular Tags