KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > haclient > HANamingConnection


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

23 package fr.dyade.aaa.jndi2.haclient;
24
25 import java.net.*;
26 import java.io.*;
27 import java.util.*;
28 import javax.naming.*;
29
30 import fr.dyade.aaa.jndi2.client.*;
31
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.util.monolog.api.Logger;
34
35 import fr.dyade.aaa.jndi2.msg.*;
36
37 public class HANamingConnection implements NamingConnection {
38
39   public static final int IDEMPOTENT = -2;
40   public static final int NOT_IDEMPOTENT = -1;
41
42   public static boolean isIdempotent(JndiRequest request) {
43     if (request instanceof JndiReadRequest) return true;
44     if (request instanceof BindRequest) {
45       BindRequest br = (BindRequest)request;
46       return br.isRebind();
47     }
48     return false;
49   }
50
51   private Vector addresses;
52
53   private IOControl ioCtrl;
54
55   private int id;
56
57   public HANamingConnection() {
58     addresses = new Vector();
59     id = -1;
60   }
61
62   public void addServerAddress(String JavaDoc host, int port) {
63     addresses.addElement(new ServerAddress(host, port));
64   }
65
66   /**
67    * An invoke opens a connection and closes it
68    * when the result has been returned. The overhead
69    * of the connection opening could be avoided
70    * if the server could close connections. Such a
71    * protocol would change the client as well.
72    */

73   public synchronized JndiReply invoke(JndiRequest request) throws NamingException {
74     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
75       Trace.logger.log(BasicLevel.DEBUG,
76                        "HANamingConnection.invoke(" + request + ')');
77     while (true) {
78       open();
79       try {
80         if (id < 0) {
81           if (isIdempotent(request)) {
82             if (Trace.logger.isLoggable(BasicLevel.DEBUG))
83               Trace.logger.log(BasicLevel.DEBUG,
84                                " -> write idempotent");
85             ioCtrl.writeInt(IDEMPOTENT);
86           } else {
87             if (Trace.logger.isLoggable(BasicLevel.DEBUG))
88               Trace.logger.log(BasicLevel.DEBUG,
89                                " -> write not idempotent");
90             ioCtrl.writeInt(NOT_IDEMPOTENT);
91             id = ioCtrl.readInt();
92             if (Trace.logger.isLoggable(BasicLevel.DEBUG))
93               Trace.logger.log(BasicLevel.DEBUG,
94                                " -> receive new request id = " + id);
95           }
96         } else {
97           ioCtrl.writeInt(id);
98         }
99         if (Trace.logger.isLoggable(BasicLevel.DEBUG))
100           Trace.logger.log(BasicLevel.DEBUG,
101                            " -> send request");
102         ioCtrl.writeObject(request);
103         return (JndiReply)ioCtrl.readObject();
104       } catch (IOException ioe) {
105         if (Trace.logger.isLoggable(BasicLevel.ERROR))
106           Trace.logger.log(BasicLevel.ERROR, "NamingConnection.receive()", ioe);
107         NamingException ne = new NamingException(ioe.getMessage());
108         ne.setRootCause(ioe);
109         throw ne;
110       } catch (ClassNotFoundException JavaDoc cnfe) {
111         if (Trace.logger.isLoggable(BasicLevel.ERROR))
112           Trace.logger.log(BasicLevel.ERROR, "NamingConnection.receive()", cnfe);
113         NamingException ne = new NamingException(cnfe.getMessage());
114         ne.setRootCause(cnfe);
115         throw ne;
116       } finally {
117         close();
118       }
119     }
120   }
121
122   private void open() throws NamingException {
123     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
124       Trace.logger.log(BasicLevel.DEBUG,
125                        "HANamingConnection.open()");
126     int i = 0;
127     while (i < addresses.size()) {
128       ServerAddress sa = (ServerAddress)addresses.elementAt(0);
129       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
130         Trace.logger.log(BasicLevel.DEBUG,
131                          " -> try connection " + sa);
132       try {
133         Socket socket = new Socket(sa.hostName, sa.port);
134         ioCtrl = new IOControl(socket);
135         return;
136       } catch (IOException exc) {
137         if (Trace.logger.isLoggable(BasicLevel.ERROR))
138           Trace.logger.log(BasicLevel.ERROR, "NamingConnection.open()", exc);
139         // Put the faulty address at the end of the list
140
addresses.removeElementAt(0);
141         addresses.addElement(sa);
142       }
143       i++;
144     }
145     NamingException exc2 =
146       new NamingException("Connection failed with all replicas: " +
147                           addresses);
148     throw exc2;
149   }
150
151   private void close() throws NamingException {
152     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
153       Trace.logger.log(BasicLevel.DEBUG,
154                        "HANamingConnection.close()");
155     ioCtrl.close();
156   }
157
158   public NamingConnection cloneConnection() {
159     HANamingConnection clone = new HANamingConnection();
160     clone.addresses = (Vector)addresses.clone();
161     return clone;
162   }
163
164   public String JavaDoc toString() {
165     return '(' + super.toString() +
166       ",addresses=" + addresses + ')';
167   }
168
169   public Hashtable getEnvironment() {
170     Hashtable env = new Hashtable();
171     return env;
172   }
173
174   static class ServerAddress {
175     String JavaDoc hostName;
176     int port;
177
178     public ServerAddress(String JavaDoc hostName, int port) {
179       this.hostName = hostName;
180       this.port = port;
181     }
182
183     public String JavaDoc toString() {
184       return '(' + super.toString() +
185         ",hostName=" + hostName +
186         ",port=" + port + ')';
187     }
188   }
189 }
190
Popular Tags