1 19 package gcc.rmi.iiop.client; 20 21 import gcc.*; 22 import gcc.properties.*; 23 import gcc.org.omg.IOP.*; 24 import gcc.org.omg.GIOP.*; 25 import gcc.rmi.iiop.*; 26 import gcc.util.*; 27 import java.net.*; 28 29 public class Connection 30 { 31 public Connection() 32 { 33 } 34 35 public static Connection getInstance(String endpoint, ObjectRef objectRef, PropertyMap connProps) 36 { 37 Connection conn = new Connection(); 38 conn.init(endpoint, objectRef, connProps); 39 return conn; 40 } 41 42 46 public static final BooleanProperty simpleIDLProperty = 47 new BooleanProperty(SystemProperties.class, "gcc.simpleIDL"); 48 49 public static final IntProperty socketTimeoutProperty = 50 new IntProperty(Connection.class, "socketTimeout") 51 .defaultValue(SystemProperties.rmiSocketTimeoutProperty.getInt()); 52 53 57 private static final boolean SIMPLE_IDL = simpleIDLProperty.getBoolean(); 58 59 private ServiceContext[] EMPTY_SERVICE_CONTEXT = {}; 60 61 private String _url; 62 63 private boolean _ok; 64 65 private InstancePool _pool; 66 67 private String _serverHost; 68 69 private Socket _socket; 70 71 private gcc.rmi.iiop.ObjectInputStream _input; 72 73 private gcc.rmi.iiop.ObjectOutputStream _output; 74 75 private CdrOutputStream _parameters; 76 77 private CdrOutputStream _requestOut; 78 79 private CdrInputStream _results; 80 81 private String _exceptionType; 82 83 private Exception _exception; 84 85 private RequestHeader_1_2 _requestHeader; 86 87 private int _callForget; 88 89 93 protected java.io.InputStream _socketIn; 94 95 protected java.io.OutputStream _socketOut; 96 97 101 public String getInstanceName() 102 { 103 return _url; 104 } 105 106 public void close() 107 { 108 _parameters = null; 109 _input = null; 112 _output = null; 113 if (_ok) 114 { 115 _pool.put(this); 116 } 117 else 118 { 119 shutdown(); 120 } 121 } 122 123 public void beforeInvoke() 124 { 125 _ok = false; 126 _parameters = CdrOutputStream.getInstance(); 127 } 128 129 public void forget(Object requestKey) 130 { 131 if (_callForget != 0) 132 { 133 152 } 153 } 154 155 public void invoke(ObjectRef object, String method, Object requestKey, int retryCount) 156 { 157 _callForget = 0; 159 RequestHeader_1_2 request = _requestHeader; 160 161 request.request_id = 0; 162 request.response_flags = 3; 163 request.target = new TargetAddress(); 164 request.target.object_key(object.$getObjectKey()); 165 request.operation = method; 166 request.service_context = getServiceContext(object, requestKey, retryCount); 167 168 request.reserved = new byte[3]; 170 if (_requestOut == null) 171 { 172 _requestOut = CdrOutputStream.getInstance(); 173 } 174 _requestOut.write_request(request, _parameters); 175 176 try 177 { 178 _requestOut.send_message(_socketOut, _url); } 180 catch (RuntimeException ex) 181 { 182 throw ex; 187 } 188 189 _requestOut.reset(); 190 191 if (_results == null) 192 { 193 _results = CdrInputStream.getInstance(); 194 } 195 else 196 { 197 _results.reset(); 198 } 199 200 _results.setNamingContext(object.$getNamingContext()); 201 GiopMessage message; 202 try 203 { 204 message = _results.receive_message(_socketIn, _url); } 206 catch (BadMagicException ex) 207 { 208 throw new SystemException(ex); 209 } 210 catch (UnsupportedProtocolVersionException ex) 211 { 212 throw new SystemException(ex); 213 } 214 catch (RuntimeException ex) 215 { 216 throw new RetryInvokeException(ex); 217 } 218 219 switch (message.type) 220 { 221 case MsgType_1_1._Reply: 222 processReply(message.reply); 223 break; 224 225 default: 226 throw new SystemException("TODO: message type = " + message.type); 227 } 228 229 _ok = true; 230 } 231 232 public InstancePool getInstancePool() 233 { 234 return _pool; 235 } 236 237 public void setInstancePool(InstancePool pool) 238 { 239 _pool = pool; 240 } 241 242 public gcc.rmi.iiop.ObjectInputStream getInputStream() 243 { 244 if (SIMPLE_IDL) 245 { 246 return getSimpleInputStream(); 247 } 248 if (_input == null) 249 { 250 _input = gcc.rmi.iiop.ObjectInputStream.getInstance(_results); 251 } 252 return _input; 253 } 254 255 public gcc.rmi.iiop.ObjectOutputStream getOutputStream() 256 { 257 if (SIMPLE_IDL) 258 { 259 return getSimpleOutputStream(); 260 } 261 if (_output == null) 262 { 263 _output = gcc.rmi.iiop.ObjectOutputStream.getInstance(_parameters); 264 } 265 return _output; 266 } 267 268 public gcc.rmi.iiop.ObjectInputStream getSimpleInputStream() 269 { 270 if (_input == null) 271 { 272 _input = gcc.rmi.iiop.SimpleObjectInputStream.getInstance(_results); 273 } 274 return _input; 275 } 276 277 public gcc.rmi.iiop.ObjectOutputStream getSimpleOutputStream() 278 { 279 if (_output == null) 280 { 281 _output = gcc.rmi.iiop.SimpleObjectOutputStream.getInstance(_parameters); 282 } 283 return _output; 284 } 285 286 public String getExceptionType() 287 { 288 return _exceptionType; 289 } 290 291 public Exception getException() 292 { 293 if (_exception == null) 294 { 295 if (_exceptionType != null) 296 { 297 return new SystemException(_exceptionType, new org.omg.CORBA.UNKNOWN()); 298 } 299 else 300 { 301 throw new IllegalStateException("no exception"); 302 } 303 } 304 else 305 { 306 return _exception; 307 } 308 } 309 310 314 protected void init(String endpoint, ObjectRef objectRef, PropertyMap connProps) 316 { 317 _url = "iiop://" + endpoint; 318 UrlInfo urlInfo = UrlInfo.getInstance(_url); 319 String host = urlInfo.getHost(); 320 int port = urlInfo.getPort(); 321 int socketTimeout = socketTimeoutProperty.getInt(endpoint, connProps); 322 try 323 { 324 _socket = new Socket(host, port); 325 _socketIn = _socket.getInputStream(); 326 _socketOut = _socket.getOutputStream(); 327 _socket.setSoTimeout(1000 * socketTimeout); 328 _serverHost = host; 329 } 330 catch (Exception ex) 331 { 332 throw new SystemException(errorConnectFailed(host, port, ex)); 333 } 334 _requestHeader = new RequestHeader_1_2(); 335 } 336 337 public ServiceContext[] getServiceContext(ObjectRef object, Object requestKey, int retryCount) 338 { 339 String username; 340 String password; 341 SecurityInfo securityInfo = SecurityInfo.getCurrent(); 342 if (securityInfo == null) 343 { 344 ClientNamingContext namingContext = object.$getNamingContext(); 345 if (namingContext != null) 346 { 347 username = namingContext.getUsername(); 348 password = namingContext.getPassword(); 349 } 350 else 351 { 352 username = null; 353 password = null; 354 } 355 } 356 else 357 { 358 username = securityInfo.username; 359 password = securityInfo.password; 360 } 361 if (username != null && username.length() == 0) 362 { 363 username = null; } 365 if (password != null && password.length() == 0) 366 { 367 password = null; } 369 int count = 0; 370 if (username != null) 371 { 372 count++; 373 } 374 if (password != null) 375 { 376 count++; 377 } 378 if (requestKey != null) 379 { 380 count++; 381 } 382 if (count == 0) 383 { 384 return EMPTY_SERVICE_CONTEXT; } 386 ServiceContext[] context = new ServiceContext[count]; 387 int index = 0; 388 if (username != null) 389 { 390 context[index++] = new ServiceContext(SecurityInfo.TAG_USERNAME, SecurityInfo.encode(username)); 391 } 392 if (password != null) 393 { 394 context[index++] = new ServiceContext(SecurityInfo.TAG_PASSWORD, SecurityInfo.encode(password)); 395 } 396 if (requestKey != null) 397 { 398 if (retryCount == 0) 399 { 400 context[index++] = new ServiceContext(0xBFBFBFBF, UTF8.fromString((String)requestKey)); 402 } 403 else 404 { 405 context[index++] = new ServiceContext(0xAFAFAFAF, UTF8.fromString((String)requestKey)); 407 } 408 } 409 return context; 410 } 411 412 protected void processReply(ReplyHeader_1_2 reply) 413 { 414 processReplyServiceContext(reply); 415 int status = reply.reply_status.value(); 416 switch (status) 417 { 418 case ReplyStatusType_1_2._NO_EXCEPTION: 419 processNormalReply(reply); 420 break; 421 case ReplyStatusType_1_2._USER_EXCEPTION: 422 processUserException(reply); 423 break; 424 case ReplyStatusType_1_2._SYSTEM_EXCEPTION: 425 processSystemException(reply); 426 break; 427 case ReplyStatusType_1_2._LOCATION_FORWARD: 428 throw new SystemException("TODO"); 429 case ReplyStatusType_1_2._LOCATION_FORWARD_PERM: 430 throw new SystemException("TODO"); 431 case ReplyStatusType_1_2._NEEDS_ADDRESSING_MODE: 432 throw new SystemException("TODO"); 433 default: 434 throw new SystemException("reply status = " + status); 435 } 436 } 437 438 protected void processReplyServiceContext(ReplyHeader_1_2 reply) 439 { 440 ServiceContext[] list = reply.service_context; 441 int n = list.length; 442 for (int i = 0; i < n; i++) 443 { 444 ServiceContext sc = list[i]; 445 if (sc.context_id == 0xCFCFCFCF 446 || sc.context_id == 0xDFDFDFDF) 447 { 448 _callForget = sc.context_id; 451 } 452 } 453 } 454 455 protected void processNormalReply(ReplyHeader_1_2 reply) 456 { 457 } 459 460 protected void processUserException(ReplyHeader_1_2 reply) 461 { 462 _exception = null; 463 _exceptionType = _results.read_string(); 464 _ok = true; 465 } 466 467 protected void processSystemException(ReplyHeader_1_2 reply) 468 { 469 _exceptionType = "???"; 470 SystemExceptionReplyBody replyBody = SystemExceptionReplyBodyHelper.read(_results); 471 String id = replyBody.exception_id; 472 id = StringUtil.removePrefix(id, "IDL:omg.org/CORBA/"); 473 id = StringUtil.removeSuffix(id, ":1.0"); 474 String stackTrace = null; 475 if (_results.hasMoreData()) 476 { 477 stackTrace = _results.read_string() + ExceptionUtil.getDivider(); 478 } 479 _ok = true; 480 String exceptionClassName = "org.omg.CORBA." + id; 481 try 482 { 483 Class exceptionClass = ThreadContext.loadClass(exceptionClassName); 484 org.omg.CORBA.SystemException corbaException = (org.omg.CORBA.SystemException)exceptionClass.newInstance(); 485 corbaException.minor = replyBody.minor_code_value; 486 corbaException.completed = org.omg.CORBA.CompletionStatus.from_int(replyBody.completion_status); 487 _exception = new gcc.SystemException(stackTrace, corbaException); 488 } 489 catch (Exception ex) 490 { 491 _exception = new gcc.SystemException(stackTrace, 492 new org.omg.CORBA.UNKNOWN(replyBody.exception_id, 493 replyBody.minor_code_value, 494 org.omg.CORBA.CompletionStatus.from_int(replyBody.completion_status))); 495 } 496 } 497 498 public void shutdown() 499 { 500 if (_socketOut != null) 501 { 502 try 503 { 504 _socketOut.close(); 505 } 506 catch (Exception ignore) 507 { 508 } 509 _socketOut = null; 510 } 511 if (_socketIn != null) 512 { 513 try 514 { 515 _socketIn.close(); 516 } 517 catch (Exception ignore) 518 { 519 } 520 _socketIn = null; 521 } 522 if (_socket != null) 523 { 524 try 525 { 526 _socket.close(); 527 } 528 catch (Exception ignore) 529 { 530 } 531 _socket = null; 532 } 533 } 534 535 537 protected String errorConnectFailed(String host, int port, Exception ex) 538 { 539 String msg; 540 msg = "Error: errorConnectFailed: host=" + host + ", port=" + port + ", ex = " + ex; 541 return msg; 542 } 543 } 544 | Popular Tags |