1 18 package org.apache.geronimo.interop.rmi.iiop.server; 19 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.net.InetAddress ; 23 import java.net.Socket ; 24 25 import org.apache.geronimo.interop.SystemException; 26 import org.apache.geronimo.interop.adapter.Adapter; 27 import org.apache.geronimo.interop.adapter.AdapterManager; 28 import org.apache.geronimo.interop.naming.NameService; 29 import org.apache.geronimo.interop.rmi.iiop.BadMagicException; 30 import org.apache.geronimo.interop.rmi.iiop.CdrInputStream; 31 import org.apache.geronimo.interop.rmi.iiop.CdrOutputStream; 32 import org.apache.geronimo.interop.rmi.iiop.GiopMessage; 33 import org.apache.geronimo.interop.rmi.iiop.ObjectInputStream; 34 import org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream; 35 import org.apache.geronimo.interop.rmi.iiop.UnsupportedProtocolVersionException; 36 import org.apache.geronimo.interop.util.UTF8; 37 import org.omg.GIOP.KeyAddr; 38 import org.omg.GIOP.LocateReplyHeader_1_2; 39 import org.omg.GIOP.LocateRequestHeader_1_2; 40 import org.omg.GIOP.LocateStatusType_1_2; 41 import org.omg.GIOP.MsgType_1_1; 42 import org.omg.GIOP.ProfileAddr; 43 import org.omg.GIOP.ReferenceAddr; 44 import org.omg.GIOP.ReplyHeader_1_2; 45 import org.omg.GIOP.ReplyStatusType_1_2; 46 import org.omg.GIOP.RequestHeader_1_2; 47 import org.omg.GIOP.TargetAddress; 48 import org.omg.IOP.ServiceContext ; 49 50 public class MessageHandler { 51 52 private AdapterManager adapterManager; 53 private boolean simpleIDL; 54 private boolean writeSystemExceptionStackTrace; 55 private NameService nameService = NameService.getInstance(); 56 57 public MessageHandler( AdapterManager adapterManager, boolean simpleIDL, 58 boolean writeSystemExceptionStackTrace ) 59 { 60 this.adapterManager = adapterManager; 61 this.simpleIDL = simpleIDL; 62 this.writeSystemExceptionStackTrace = writeSystemExceptionStackTrace; 63 } 64 65 public void service(Socket socket) throws Exception { 66 67 InputStream in; 68 OutputStream out; 69 70 String clientHostName; 71 String clientHostAddress; 72 String clientInfo; 73 74 in = socket.getInputStream(); 75 out = socket.getOutputStream(); 76 77 InetAddress addr = socket.getInetAddress(); 78 clientHostName = addr.getHostName(); 79 clientHostAddress = addr.getHostAddress(); 80 clientInfo = clientHostName; 81 82 if (!clientHostAddress.equals(clientHostName)) { 83 clientInfo += " (" + clientHostAddress + ")"; 84 } 85 86 boolean firstMessage = true; 87 CdrInputStream input = CdrInputStream.getInstance(); 88 CdrOutputStream output = CdrOutputStream.getInstance(); 89 CdrOutputStream results = CdrOutputStream.getInstance(); 90 91 for (; ;) { 92 boolean sendResponse = true; 93 GiopMessage inputMessage; 94 95 try { 96 inputMessage = input.receive_message( in, clientInfo ); 97 firstMessage = false; 98 } catch (BadMagicException ex) { 99 if (firstMessage) { 100 warnBadMagic(clientInfo, ex); 101 } else { 102 warnBadMagicBadSize(clientInfo, ex); 103 } 104 closeStreams( in, out ); 105 return; 106 } catch (UnsupportedProtocolVersionException ex) { 107 warnGiopVersion( clientInfo, ex); 108 closeStreams( in, out ); 109 return; 110 } catch (Exception ex) { 111 if (input.getOffset() > 0) { 112 ex.printStackTrace(); 113 warnReceiveFailed( clientInfo, ex); 114 } 115 closeStreams( in, out ); 119 return; 120 } 121 122 output.setGiopVersion(input.getGiopVersion()); 123 124 switch (inputMessage.type) { 125 case MsgType_1_1._Request: 126 processRequest(input, output, results, inputMessage.request, clientInfo); 127 if ((inputMessage.request.response_flags & 1) == 0) { 128 sendResponse = false; } 130 break; 131 case MsgType_1_1._LocateRequest: 132 processLocateRequest(output, inputMessage.locateRequest); 133 break; 134 default: 135 throw new SystemException("TODO: message type = " + inputMessage.type); 136 } 137 138 if (sendResponse) { 139 try { 140 if(inputMessage.httpTunneling) 141 { 142 output.send_http_response( out, clientInfo ); 143 } 144 else 145 { 146 output.send_message( out, clientInfo ); 147 } 148 } catch (Exception ex) { 149 warnSendFailed(clientInfo, ex); 150 closeStreams( in, out ); 151 return; 152 } 153 } 154 155 input.reset(); 156 output.reset(); 157 results.reset(); 158 } 159 } 160 161 protected void closeStreams( InputStream in, OutputStream out ) { 162 try { 163 if (in != null) { 164 in.close(); 165 } 166 } catch (Exception ignore) { 167 } 168 169 try { 170 if (out != null) { 171 out.close(); 172 } 173 } catch (Exception ignore) { 174 } 175 } 176 177 protected byte[] getObjectKey(TargetAddress target) { 178 switch (target.discriminator()) { 179 case KeyAddr.value: 180 return target.object_key(); 181 case ProfileAddr.value: 182 case ReferenceAddr.value: 183 throw new SystemException("TODO"); 184 default: 185 throw new IllegalArgumentException ("target discriminator = " + target.discriminator()); 186 } 187 } 188 189 protected void processRequest(CdrInputStream parameters, 190 CdrOutputStream output, 191 CdrOutputStream results, 192 RequestHeader_1_2 request, 193 String clientInfo 194 ) { 195 byte[] objectKey = getObjectKey(request.target); 196 int keyLength = objectKey.length; 197 int keyType = keyLength == 0 ? 0 : objectKey[0]; 198 199 ReplyHeader_1_2 reply = new ReplyHeader_1_2(); 200 reply.request_id = request.request_id; 201 202 ObjectInputStream objectIn; 203 ObjectOutputStream objectOut; 204 205 if (simpleIDL || keyType == 'N' || keyType == 'J') { 206 objectIn = org.apache.geronimo.interop.rmi.iiop.SimpleObjectInputStream.getInstance(parameters); 208 objectOut = org.apache.geronimo.interop.rmi.iiop.SimpleObjectOutputStream.getInstance(results); 209 } else { 210 objectIn = org.apache.geronimo.interop.rmi.iiop.ObjectInputStream.getInstance(parameters); 212 objectOut = org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream.getInstance(results); 213 } 214 215 try { 216 String objectName = null; 217 for (int colonPos = 0; colonPos < keyLength; colonPos++) { 218 if (objectKey[colonPos] == ':') { 219 objectName = UTF8.toString(objectKey, 0, colonPos); 220 int newKeyLength = keyLength - colonPos - 1; 221 byte[] newObjectKey = new byte[newKeyLength]; 222 System.arraycopy(objectKey, colonPos + 1, newObjectKey, 0, newKeyLength); 223 objectKey = newObjectKey; 224 break; 225 } 226 } 227 228 if (objectName == null) { 229 objectName = UTF8.toString(objectKey); 230 } 231 232 processServiceContext(request); 233 234 Object object; 235 try 236 { 237 object = nameService.lookup(objectName); 238 } 239 catch (javax.naming.NameNotFoundException notFound) 240 { 241 warnLookupFailed(clientInfo, notFound); 242 throw new org.omg.CORBA.OBJECT_NOT_EXIST (objectName); 243 } 244 245 Adapter adapter = (Adapter)object; 246 if (adapter != null) 247 { 248 adapter.invoke(request.operation, objectKey, objectIn, objectOut); 249 250 if (objectOut.hasException()) { 251 reply.reply_status = ReplyStatusType_1_2.USER_EXCEPTION; 252 } else { 253 reply.reply_status = ReplyStatusType_1_2.NO_EXCEPTION; 254 } 255 256 output.write_reply(reply, results); 257 } else { 258 throw new org.omg.CORBA.OBJECT_NOT_EXIST (objectName); 259 } 260 } catch (Exception ex) { 261 warnSystemException(clientInfo, ex); 262 results = CdrOutputStream.getInstance(); results.write_SystemException(ex, writeSystemExceptionStackTrace); 264 reply.reply_status = ReplyStatusType_1_2.SYSTEM_EXCEPTION; 265 output.write_reply(reply, results); 266 } 267 } 268 269 protected void processLocateRequest(CdrOutputStream output, LocateRequestHeader_1_2 request) { 270 LocateReplyHeader_1_2 reply = new LocateReplyHeader_1_2(); 273 reply.request_id = request.request_id; 274 reply.locate_status = LocateStatusType_1_2.OBJECT_HERE; 275 output.write_reply(reply); 276 } 277 278 protected void processServiceContext(RequestHeader_1_2 request) { 279 ServiceContext [] contextList = request.service_context; 280 int n = contextList.length; 281 String username = null; 282 String password = null; 283 284 for (int i = 0; i < n; i++) { 285 ServiceContext context = contextList[i]; 286 int tag = context.context_id; 287 288 298 299 } 301 } 302 303 305 protected void warnBadMagic(String clientHost, Exception ex) { 306 System.out.println("MH.warnBadMagic: clientHost: " + clientHost + ", ex = " + ex); 307 } 308 309 protected void warnBadMagicBadSize(String clientHost, Exception ex) { 310 System.out.println("MH.warnBadMagicBadSize: clientHost: " + clientHost + ", ex = " + ex); 311 } 312 313 protected void warnGiopVersion(String clientHost, Exception ex) { 314 System.out.println("MH.warnGiopVersion: clientHost: " + clientHost + ", ex = " + ex); 315 } 316 317 protected void warnInvokeFailedNoRemoteInterface(String clientHost, Object object, Class type) { 318 System.out.println("MH.warnInvokeFailedNoRemoteInterface: clientHost: " + clientHost + ", object = " + object + ", type: " + type); 319 } 320 321 protected void warnLookupFailed(String clientHost, Exception ex) { 322 System.out.println("MH.warnLookupFailed: clientHost: " + clientHost + ", ex = " + ex); 323 } 324 325 protected void warnReceiveFailed(String clientHost, Exception ex) { 326 System.out.println("MH.warnReceiveFailed: clientHost: " + clientHost + ", ex = " + ex); 327 } 328 329 protected void warnSendFailed(String clientHost, Exception ex) { 330 System.out.println("MH.warnSendFailed: clientHost: " + clientHost + ", ex = " + ex); 331 } 332 333 protected void warnSystemException(String clientHost, Exception ex) { 334 System.out.println("MH.warnSystemException: clientHost: " + clientHost + ", ex = " + ex); 335 } 336 337 } 338 | Popular Tags |