1 package org.enhydra.shark.limitagent; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Map ; 6 import java.util.Properties ; 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 24 public class StandardLimitAgent implements LimitAgent { 25 26 public static final String LIMIT_AGENT_MANAGER_PREFIX = "LimitAgentManager"; 27 public static final String LIMIT_AGENT_PREFIX = "LimitAgent"; 28 protected static Map limitAgents; 29 protected CallbackUtilities cus; 30 31 public void configure(CallbackUtilities cus) throws RootException { 32 StandardLimitAgent.limitAgents = new HashMap (); 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 String agentClass = agentClass=XMLUtil.getExtendedAttributeValue(session.getExtendedAttributesNameValuePairs(), "LimitAgentClassName"); 49 50 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 agentName) { 61 LimitAgent agent = null; 62 agent = (LimitAgent) limitAgents.get(agentName); 64 65 if (agent == null) { 67 Iterator i = StandardLimitAgent.limitAgents.values().iterator(); 68 while (i.hasNext() && agent == null) { 69 Object 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 cl) { 79 Properties props = cus.getProperties(); 80 Iterator it = props.entrySet().iterator(); 81 while (it.hasNext()) { 82 String name = null; 83 String className = null; 84 try { 85 Map.Entry me = (Map.Entry ) 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 ) 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 ex) { 97 cus.error("LimitAgentManager -> Creation of Limit Agent "+ name +" from class "+ className +" failed !!!"); 98 } 99 } 100 } 101 102 } 103 104 | Popular Tags |