1 package org.enhydra.shark.limitagent; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 import java.util.Timer ; 6 import java.util.TimerTask ; 7 import org.enhydra.shark.api.RootException; 8 import org.enhydra.shark.api.SharkTransaction; 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.LimitAgentManager; 12 import org.enhydra.shark.api.internal.limitagent.LimitAgentSession; 13 import org.enhydra.shark.api.internal.working.CallbackUtilities; 14 15 19 public class TimerLimitAgentManager implements LimitAgentManager { 20 21 protected CallbackUtilities cus; 22 protected Map registeredObjects; 23 24 public void configure(CallbackUtilities cus) throws RootException { 25 this.registeredObjects = new HashMap (); 26 this.cus = cus; 27 } 28 29 public void checkLimits (SharkTransaction t) throws LimitAgentException { 30 throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager"); 31 } 32 33 public void checkLimits (SharkTransaction t,String procId) throws LimitAgentException { 34 throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager"); 35 } 36 37 public void checkProcessLimit (SharkTransaction t,String procId) throws LimitAgentException { 38 throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager"); 39 } 40 41 public void checkActivityLimit (SharkTransaction t,String procId,String actId) throws LimitAgentException { 42 throw new LimitAgentException("This method is not supported by the timer implementation of LimitAgentManager"); 43 } 44 45 public void notifyStop(String procId,String actId) throws LimitAgentException { 46 if (procId == null && actId == null) { 47 cus.error("Cannot locate limit schedule for a null WfExecutionObject ID!"); 48 throw new LimitAgentException("Invalid WfExecutionObject ID; cannot locate scheduled limit"); 49 } 50 51 Timer timer; 53 if (actId!=null) { 54 timer=(Timer ) registeredObjects.remove(actId); 55 } else { 56 timer=(Timer ) registeredObjects.remove(procId); 57 } 58 if (timer != null) { 59 timer.cancel(); 60 } 61 } 62 63 public void notifyStart(String procId,String actId, Map context, long runtime) throws LimitAgentException { 64 if (procId == null && actId == null) { 65 cus.error("Cannot create limit schedule for a null WfExecutionObject ID!"); 66 throw new LimitAgentException("Invalid WfExecutionObject ID; cannot schedule limit"); 67 } 68 69 72 LimitAgentSession session = new TimerLimitAgentSession(procId, actId, context, runtime); 74 75 long delay = runtime - System.currentTimeMillis(); 77 if (delay <= 0) { 78 cus.debug("Limit for this object has already been reached; running now"); 79 delay = 1; 80 } 81 Timer timer = new Timer (); 82 timer.schedule(new LimitTimer(session), delay); 83 cus.debug("Limit Timer scheduled in [" + delay + "ms]"); 84 85 if (actId!=null) { 87 registeredObjects.put(actId, timer); 88 } else { 89 registeredObjects.put(procId, timer); 90 } 91 } 92 93 class LimitTimer extends TimerTask { 94 95 private LimitAgentSession session; 96 97 private LimitTimer(LimitAgentSession session) { 98 this.session = session; 99 } 100 101 public void run() { 102 LimitAgent agent = new TimerLimitAgent(); 103 try { 104 agent.configure(cus); 105 } catch (RootException e) { 106 cus.error("Unable to configure the LimitAgent", e); 107 } 108 try { 109 agent.invoke(session); 110 } catch (LimitAgentException e) { 111 cus.error("Problem invoking limit notification agent", e); 112 } 113 } 114 } 115 } 116 | Popular Tags |