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 TimerLimitAgent 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 TimerLimitAgent.limitAgents = new HashMap (); 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 String agentClass = agentClass=XMLUtil.getExtendedAttributeValue(session.getExtendedAttributesNameValuePairs(), "LimitAgentClassName"); 46 47 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 agentName) { 58 LimitAgent agent = null; 59 agent = (LimitAgent) limitAgents.get(agentName); 61 62 if (agent == null) { 64 Iterator i = TimerLimitAgent.limitAgents.values().iterator(); 65 while (i.hasNext() && agent == null) { 66 Object 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 cl) { 76 Properties props = cus.getProperties(); 77 Iterator it = props.entrySet().iterator(); 78 while (it.hasNext()) { 79 String name = null; 80 String className = null; 81 try { 82 Map.Entry me = (Map.Entry ) 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 ) 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 ex) { 94 cus.error("LimitAgentManager -> Creation of Limit Agent "+ name +" from class "+ className +" failed !!!"); 95 } 96 } 97 } 98 99 } 100 101 | Popular Tags |