KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > ha > HARequestManager


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2003 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): David Feliot
21  */

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 JavaDoc {
38
39   public static final int IDEMPOTENT = -2;
40   public static final int NOT_IDEMPOTENT = -1;
41
42   public static final String JavaDoc 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 JavaDoc {
55     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
56       Trace.logger.log(BasicLevel.DEBUG,
57                        "HARequestManager.agentInitialize(" + firstTime + ')');
58     Integer JavaDoc counter = (Integer JavaDoc)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     // Notice that the counter is actually saved
83
// at the end of the reaction. If a failure occurs
84
// before the end of the reaction, the id may have been
85
// delivered whereas the counter is not saved. So after
86
// a recovery, the same identifier may be given again.
87
// But a HA server cannot be recovered (it is transient).
88
// If it is recoverable then the counter storage must
89
// be synchronous.
90
not.Return(id);
91   }
92
93   void doReact(TcpRequestNot not) throws Exception JavaDoc {
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 JavaDoc reqId = new Integer JavaDoc(reqCtx.getId());
108       HARequestContext recoveredReqCtx =
109         (HARequestContext)requests.get(reqId);
110       if (recoveredReqCtx == null) {
111         requests.put(new Integer JavaDoc(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 JavaDoc(id));
128   }
129
130   private void saveRequestCounter() {
131     try {
132       AgentServer.getTransaction().save(
133         new Integer JavaDoc(requestCounter), HA_REQUEST_COUNTER);
134     } catch (IOException exc) {
135       throw new Error JavaDoc(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 JavaDoc {
148     requestCounter = in.readInt();
149     requests = (Hashtable)in.readObject();
150     manager.readBag(in);
151   }
152 }
153
Popular Tags