KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.limitagent;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import org.enhydra.shark.api.RootException;
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.LimitAgentSession;
12 import org.enhydra.shark.api.internal.working.CallbackUtilities;
13 import org.enhydra.shark.xpdl.XMLUtil;
14 import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
15 import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
16
17
18 /**
19  *
20  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
21  * @author Sasa Bojanic
22  * @since Mar 24, 2004
23  */

24 public class TimerLimitAgent implements LimitAgent {
25
26    public static final String JavaDoc LIMIT_AGENT_MANAGER_PREFIX = "LimitAgentManager";
27    public static final String JavaDoc LIMIT_AGENT_PREFIX = "LimitAgent";
28    protected static Map JavaDoc limitAgents;
29    protected CallbackUtilities cus;
30
31    public void configure(CallbackUtilities cus) throws RootException {
32       TimerLimitAgent.limitAgents = new HashMap JavaDoc();
33       this.cus = cus;
34       loadLimitAgents(cus, getClass().getClassLoader());
35    }
36
37    public void invoke(LimitAgentSession session) throws LimitAgentException {
38       if (session.getActivityId()!=null) {
39          cus.info("-----> WfActivity : " + session.getActivityId() + " has reached its limit");
40       } else {
41          cus.info("-----> WfProcess : " + session.getProcessId() + " has reached its limit");
42       }
43
44       // the external limit agent
45
String JavaDoc agentClass = agentClass=XMLUtil.getExtendedAttributeValue(session.getExtendedAttributesNameValuePairs(), "LimitAgentClassName");
46
47       // check for the agent class
48
// if we have an agent to use invoke it
49
if (agentClass != null) {
50          LimitAgent agent = this.getLimitAgent(agentClass);
51          if (agent != null) {
52             agent.invoke(session);
53          }
54       }
55    }
56
57    private LimitAgent getLimitAgent(String JavaDoc agentName) {
58       LimitAgent agent = null;
59       // first check if the name is the value in the properties file
60
agent = (LimitAgent) limitAgents.get(agentName);
61
62       // next check the class name
63
if (agent == null) {
64          Iterator JavaDoc i = TimerLimitAgent.limitAgents.values().iterator();
65          while (i.hasNext() && agent == null) {
66             Object JavaDoc obj = i.next();
67             if (obj.getClass().getName().equals(agentName)) {
68                agent = (LimitAgent) obj;
69             }
70          }
71       }
72       return agent;
73    }
74
75    private static void loadLimitAgents(CallbackUtilities cus, ClassLoader JavaDoc cl) {
76       Properties JavaDoc props = cus.getProperties();
77       Iterator JavaDoc it = props.entrySet().iterator();
78       while (it.hasNext()) {
79          String JavaDoc name = null;
80          String JavaDoc className = null;
81          try {
82             Map.Entry JavaDoc me = (Map.Entry JavaDoc) it.next();
83             name = me.getKey().toString();
84             if (name.startsWith(LIMIT_AGENT_PREFIX) && !name.startsWith(LIMIT_AGENT_MANAGER_PREFIX)) {
85                name = name.substring(LIMIT_AGENT_PREFIX.length());
86                className = (String JavaDoc) me.getValue();
87                LimitAgent agent = (LimitAgent) cl.loadClass(className).newInstance();
88                if (agent != null) {
89                   agent.configure(cus);
90                }
91                TimerLimitAgent.limitAgents.put(name, agent);
92             }
93          } catch (Throwable JavaDoc ex) {
94             cus.error("LimitAgentManager -> Creation of Limit Agent "+ name +" from class "+ className +" failed !!!");
95          }
96       }
97    }
98
99 }
100
101
Popular Tags