1 22 package org.objectweb.petals.kernel.registry.jndi; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.Serializable ; 28 import java.net.InetSocketAddress ; 29 import java.net.Socket ; 30 31 import javax.naming.NamingException ; 32 import javax.naming.ServiceUnavailableException ; 33 34 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest; 35 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse; 36 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse.ResponseType; 37 38 43 public class JNDIConnection implements Serializable { 44 45 private static final long serialVersionUID = 1L; 46 47 50 protected int port; 51 52 55 protected String host; 56 57 public JNDIConnection(int port, String host) { 58 super(); 59 this.port = port; 60 this.host = host; 61 } 62 63 72 public Object send(RegistryRequest request) throws NamingException { 73 Object out = null; 74 Socket socket = new Socket (); 75 try { 76 socket.connect(new InetSocketAddress (host, port), 1000); 77 ObjectOutputStream objectOutputStream = new ObjectOutputStream ( 78 socket.getOutputStream()); 79 objectOutputStream.writeObject(request); 80 objectOutputStream.flush(); 81 82 ObjectInputStream objectInputStream = new ObjectInputStream (socket 83 .getInputStream()); 84 RegistryResponse response = (RegistryResponse) objectInputStream 85 .readObject(); 86 87 if (response.getType() == ResponseType.exception) { 88 throw (NamingException ) response.getArg1(); 89 } 90 out = response.getArg1(); 91 } catch (IOException e) { 92 throw new ServiceUnavailableException ( 93 "Problem while sending request to server " + host + ":" + port); 94 } catch (ClassNotFoundException e) { 95 throw new ServiceUnavailableException ( 96 "Problem while sending request to server " + host + ":" + port); 97 } 98 return out; 99 } 100 101 } 102 | Popular Tags |