1 45 package org.openejb.client; 46 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.OutputStream ; 50 import java.io.StreamCorruptedException ; 51 import java.net.Socket ; 52 import java.util.Properties ; 53 54 58 public class SocketConnectionFactory implements ConnectionFactory{ 59 60 66 public void init(Properties props) { 67 } 68 69 70 76 public Connection getConnection(ServerMetaData server) throws java.io.IOException { 77 SocketConnection conn = new SocketConnection(); 78 conn.open( server ); 79 return conn; 80 } 81 82 86 class SocketConnection implements Connection{ 87 88 Socket socket = null; 89 OutputStream socketOut = null; 90 InputStream socketIn = null; 91 92 protected void open(ServerMetaData server) throws IOException { 93 94 95 96 try{ 97 socket = new Socket (server.getHost(), server.getPort()); 98 socket.setTcpNoDelay(true); 99 } catch (IOException e){ 100 throw new IOException ("Cannot access server: " + server.getHost() + ":" + server.getPort() + " Exception: " + e.getClass().getName() + " : " + e.getMessage()); 101 102 } catch (SecurityException e){ 103 throw new IOException ("Cannot access server: " + server.getHost() + ":" + server.getPort() + " due to security restrictions in the current VM: " + e.getClass().getName() + " : " + e.getMessage()); 104 105 } catch (Throwable e){ 106 throw new IOException ("Cannot access server: " + server.getHost() + ":" + server.getPort() + " due to an unkown exception in the OpenEJB client: " + e.getClass().getName() + " : " + e.getMessage()); 107 } 108 109 } 110 111 public void close() throws IOException { 112 try { 113 if (socketOut != null) socketOut.close(); 114 if (socketIn != null) socketIn.close(); 115 if (socket != null) socket.close(); 116 } catch (Throwable t){ 117 throw new IOException ("Error closing connection with server: "+t.getMessage() ); 118 } 119 } 120 121 public InputStream getInputStream() throws IOException { 122 123 124 125 try{ 126 socketIn = socket.getInputStream(); 127 } catch (StreamCorruptedException e){ 128 throw new IOException ("Cannot open input stream to server, the stream has been corrupted: " + e.getClass().getName() +" : "+ e.getMessage()); 129 130 } catch (IOException e){ 131 throw new IOException ("Cannot open input stream to server: " + e.getClass().getName() +" : "+ e.getMessage()); 132 133 } catch (Throwable e){ 134 throw new IOException ("Cannot open output stream to server: " + e.getClass().getName() +" : "+ e.getMessage()); 135 } 136 return socketIn; 137 } 138 139 public OutputStream getOuputStream() throws IOException { 140 141 142 143 try{ 144 socketOut = socket.getOutputStream(); 145 } catch (IOException e){ 146 throw new IOException ("Cannot open output stream to server: "+ e.getClass().getName() +" : "+ e.getMessage()); 147 148 } catch (Throwable e){ 149 throw new IOException ("Cannot open output stream to server: "+ e.getClass().getName() +" : "+ e.getMessage()); 150 } 151 return socketOut; 152 } 153 154 } 155 } 156 157 | Popular Tags |