1 22 package fr.dyade.aaa.jndi2.ha; 23 24 import java.util.*; 25 import java.io.*; 26 27 import javax.naming.*; 28 29 import fr.dyade.aaa.agent.*; 30 import fr.dyade.aaa.jndi2.server.*; 31 import fr.dyade.aaa.jndi2.msg.*; 32 33 import org.objectweb.util.monolog.api.BasicLevel; 34 import org.objectweb.util.monolog.api.Logger; 35 36 public class HARequestManager 37 implements LifeCycleListener, BagSerializer, java.io.Serializable { 38 39 public static final int IDEMPOTENT = -2; 40 public static final int NOT_IDEMPOTENT = -1; 41 42 public static final String HA_REQUEST_COUNTER = "haRequestCounter"; 43 44 private transient int requestCounter; 45 46 private transient Hashtable requests; 47 48 private RequestManager manager; 49 50 public void setRequestManager(RequestManager manager) { 51 this.manager = manager; 52 } 53 54 public void agentInitialize(boolean firstTime) throws Exception { 55 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 56 Trace.logger.log(BasicLevel.DEBUG, 57 "HARequestManager.agentInitialize(" + firstTime + ')'); 58 Integer counter = (Integer )AgentServer.getTransaction().load( 59 HA_REQUEST_COUNTER); 60 if (counter == null) { 61 requestCounter = 0; 62 } else { 63 requestCounter = counter.intValue(); 64 } 65 requests = new Hashtable(); 66 manager.agentInitialize(firstTime); 67 } 68 69 public void agentFinalize(boolean lastTime) { 70 manager.agentFinalize(lastTime); 71 } 72 73 void doReact(GetRequestIdNot not) { 74 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 75 Trace.logger.log(BasicLevel.DEBUG, 76 "HARequestManager.doReact((GetRequestIdNot)" + 77 not + ')'); 78 int id = requestCounter; 79 requestCounter++; 80 saveRequestCounter(); 81 82 not.Return(id); 91 } 92 93 void doReact(TcpRequestNot not) throws Exception { 94 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 95 Trace.logger.log(BasicLevel.DEBUG, 96 "HARequestManager.doReact((TcpRequestNot)" + 97 not + ')'); 98 HARequestContext reqCtx = 99 (HARequestContext)not.getRequestContext(); 100 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 101 Trace.logger.log(BasicLevel.DEBUG, 102 " -> request id = " + reqCtx.getId()); 103 if (reqCtx.getId() == IDEMPOTENT) { 104 JndiReply reply = manager.invoke(reqCtx); 105 reqCtx.reply(reply); 106 } else { 107 Integer reqId = new Integer (reqCtx.getId()); 108 HARequestContext recoveredReqCtx = 109 (HARequestContext)requests.get(reqId); 110 if (recoveredReqCtx == null) { 111 requests.put(new Integer (requestCounter), reqCtx); 112 JndiReply reply = manager.invoke(reqCtx); 113 reqCtx.reply(reply); 114 } else { 115 JndiReply reply = recoveredReqCtx.getReply(); 116 if (reply == null) { 117 recoveredReqCtx.recover( 118 reqCtx); 119 } else { 120 reqCtx.reply(reply); 121 } 122 } 123 } 124 } 125 126 void removeContext(int id) { 127 requests.remove(new Integer (id)); 128 } 129 130 private void saveRequestCounter() { 131 try { 132 AgentServer.getTransaction().save( 133 new Integer (requestCounter), HA_REQUEST_COUNTER); 134 } catch (IOException exc) { 135 throw new Error (exc.toString()); 136 } 137 } 138 139 public void writeBag(ObjectOutputStream out) 140 throws IOException { 141 out.writeInt(requestCounter); 142 out.writeObject(requests); 143 manager.writeBag(out); 144 } 145 146 public void readBag(ObjectInputStream in) 147 throws IOException, ClassNotFoundException { 148 requestCounter = in.readInt(); 149 requests = (Hashtable)in.readObject(); 150 manager.readBag(in); 151 } 152 } 153 | Popular Tags |