1 45 package org.openejb.client; 46 47 import java.io.IOException ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectInputStream ; 50 import java.io.ObjectOutput ; 51 import java.io.ObjectOutputStream ; 52 import java.io.OutputStream ; 53 import java.rmi.RemoteException ; 54 55 60 public class Client { 61 62 public static Response request(Request req, Response res, ServerMetaData server) throws RemoteException { 63 if ( server == null ) throw new IllegalArgumentException ("Client request error: ServerMetaData cannot be null"); 64 65 OutputStream out = null; 66 ObjectOutput objectOut = null; 67 ObjectInput objectIn = null; 68 Connection conn = null; 69 70 try{ 71 72 73 74 try{ 75 conn = ConnectionManager.getConnection( server ); 76 } catch (IOException e){ 77 throw new RemoteException ("Cannot access server: "+server.getHost()+":"+server.getPort()+" Exception: ", e ); 78 } catch (Throwable e){ 79 throw new RemoteException ("Cannot access server: "+server.getHost()+":"+server.getPort()+" due to an unkown exception in the OpenEJB client: ", e ); 80 } 81 82 83 84 85 try{ 86 87 out = conn.getOuputStream(); 88 89 } catch (IOException e){ 90 throw new RemoteException ("Cannot open output stream to server: " , e ); 91 92 } catch (Throwable e){ 93 throw new RemoteException ("Cannot open output stream to server: " , e ); 94 } 95 96 97 98 99 try{ 100 101 out.write( req.getRequestType() ); 102 103 } catch (IOException e){ 104 throw new RemoteException ("Cannot write the request type to the server: " , e ); 105 106 } catch (Throwable e){ 107 throw new RemoteException ("Cannot write the request type to the server: " , e ); 108 } 109 110 111 112 113 try{ 114 115 objectOut = new ObjectOutputStream ( out ); 116 117 } catch (IOException e){ 118 throw new RemoteException ("Cannot open object output stream to server: " , e ); 119 120 } catch (Throwable e){ 121 throw new RemoteException ("Cannot open object output stream to server: " , e ); 122 } 123 124 125 126 127 128 try{ 129 130 req.writeExternal( objectOut ); 132 objectOut.flush(); 133 134 } catch (java.io.NotSerializableException e){ 135 throw new IllegalArgumentException ("Object is not serializable: "+ e.getMessage()); 138 139 } catch (IOException e){ 140 throw new RemoteException ("Cannot write the request to the server: " , e ); 141 142 } catch (Throwable e){ 143 throw new RemoteException ("Cannot write the request to the server: " , e ); 144 } 145 146 147 148 149 try{ 150 151 objectIn = new ObjectInputStream (conn.getInputStream()); 152 } catch (IOException e){ 153 throw new RemoteException ("Cannot open object input stream to server: " , e ); 154 155 } catch (Throwable e){ 156 throw new RemoteException ("Cannot open object input stream to server: " , e ); 157 } 158 159 160 161 162 try{ 163 res.readExternal( objectIn ); 165 } catch (ClassNotFoundException e){ 166 throw new RemoteException ("Cannot read the response from the server. The class for an object being returned is not located in this system:" , e ); 167 168 } catch (IOException e){ 169 throw new RemoteException ("Cannot read the response from the server." , e ); 170 171 } catch (Throwable e){ 172 throw new RemoteException ("Error reading response from server: " , e ); 173 } 174 175 } catch ( Throwable error ) { 176 throw new RemoteException ("Error while communicating with server: " , error ); 177 178 } finally { 179 try { 180 conn.close(); 181 } catch (Throwable t){ 182 System.out.println("Error closing connection with server: "+t.getMessage() ); 184 } 185 } 186 return res; 187 } 188 189 } 190 191 192 | Popular Tags |