1 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 host, int port) { 63 addresses.addElement(new ServerAddress(host, port)); 64 } 65 66 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 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 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 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 hostName; 176 int port; 177 178 public ServerAddress(String hostName, int port) { 179 this.hostName = hostName; 180 this.port = port; 181 } 182 183 public String toString() { 184 return '(' + super.toString() + 185 ",hostName=" + hostName + 186 ",port=" + port + ')'; 187 } 188 } 189 } 190 | Popular Tags |