KickJava   Java API By Example, From Geeks To Geeks.

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


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