1 23 package fr.dyade.aaa.jndi2.client; 24 25 import java.net.*; 26 import java.io.*; 27 import java.util.*; 28 import javax.naming.*; 29 30 import org.objectweb.util.monolog.api.BasicLevel; 31 import org.objectweb.util.monolog.api.Logger; 32 33 import fr.dyade.aaa.jndi2.msg.*; 34 35 public class SimpleNamingConnection 36 implements NamingConnection { 37 38 private String hostName; 39 40 private int port; 41 42 private Hashtable env; 43 44 private IOControl ioCtrl; 45 46 public SimpleNamingConnection(String hostName, 47 int port, 48 Hashtable env) { 49 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 50 Trace.logger.log(BasicLevel.DEBUG, 51 "SimpleNamingConnection.<init>(" + 52 hostName + ',' + 53 port + ',' + 54 env + ')'); 55 this.hostName = hostName; 56 this.port = port; 57 this.env = env; 58 } 59 60 public final String getHostName() { 61 return hostName; 62 } 63 64 public final int getPort() { 65 return port; 66 } 67 68 75 public synchronized JndiReply invoke(JndiRequest request) throws NamingException { 76 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 77 Trace.logger.log(BasicLevel.DEBUG, 78 "NamingConnection.invoke(" + request + ')'); 79 open(); 80 try { 81 ioCtrl.writeObject(request); 82 return (JndiReply)ioCtrl.readObject(); 83 } catch (IOException ioe) { 84 if (Trace.logger.isLoggable(BasicLevel.ERROR)) 85 Trace.logger.log(BasicLevel.ERROR, "NamingConnection.receive()", ioe); 86 NamingException ne = new NamingException(ioe.getMessage()); 87 ne.setRootCause(ioe); 88 throw ne; 89 } catch (ClassNotFoundException cnfe) { 90 if (Trace.logger.isLoggable(BasicLevel.ERROR)) 91 Trace.logger.log(BasicLevel.ERROR, "NamingConnection.receive()", cnfe); 92 NamingException ne = new NamingException(cnfe.getMessage()); 93 ne.setRootCause(cnfe); 94 throw ne; 95 } finally { 96 ioCtrl.close(); 97 } 98 } 99 100 private static String TIMEOUT_PROPERTY = 101 "fr.dyade.aaa.jndi2.client.SimpleNamingConnection.timeout"; 102 103 private void open() throws NamingException { 104 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 105 Trace.logger.log(BasicLevel.DEBUG, 106 "SimpleNamingConnection.open()"); 107 try { 108 int timeout = Integer.getInteger(TIMEOUT_PROPERTY, 0).intValue(); 109 Socket socket = new Socket(); 110 socket.connect(new InetSocketAddress(hostName, port), timeout); 111 ioCtrl = new IOControl(socket); 112 } catch (IOException exc) { 113 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 114 Trace.logger.log(BasicLevel.DEBUG, "NamingConnection.open()", exc); 115 NamingException exc2 = new NamingException(exc.getMessage()); 116 exc2.setRootCause(exc); 117 throw exc2; 118 } 119 } 120 121 public Hashtable getEnvironment() { 122 return env; 123 } 124 125 public NamingConnection cloneConnection() { 126 return new SimpleNamingConnection(hostName, port, env); 127 } 128 129 public String toString() { 130 return '(' + super.toString() + 131 ",hostname=" + hostName + 132 ",port=" + port + 133 ",env=" + env + ')'; 134 } 135 } 136 | Popular Tags |