1 // 2 // Copyright (c) ZeroC, Inc. All rights reserved. 3 // 4 5 package IceInternal; 6 7 class IncomingBase 8 { 9 protected IncomingBase(Instance instance, ResponseHandler handler, Ice.ConnectionI connection, Ice.ObjectAdapter adapter, boolean response, byte compress, int requestId)10 IncomingBase(Instance instance, ResponseHandler handler, Ice.ConnectionI connection, Ice.ObjectAdapter adapter, 11 boolean response, byte compress, int requestId) 12 { 13 _instance = instance; 14 _responseHandler = handler; 15 _response = response; 16 _compress = compress; 17 _format = Ice.FormatType.DefaultFormat; 18 19 _current = new Ice.Current(); 20 _current.id = new Ice.Identity(); 21 _current.adapter = adapter; 22 _current.con = connection; 23 _current.requestId = requestId; 24 } 25 26 protected IncomingBase(IncomingBase other)27 IncomingBase(IncomingBase other) 28 { 29 // 30 // We don't change _current as it's exposed by Ice::Request 31 // 32 _current = other._current; 33 34 _instance = other._instance; 35 _servant = other._servant; 36 _locator = other._locator; 37 _response = other._response; 38 _compress = other._compress; 39 _format = other._format; 40 _responseHandler = other._responseHandler; 41 42 // Adopt observer and cookie 43 _observer = other._observer; 44 other._observer = null; 45 _cookie = other._cookie; 46 other._cookie = null; 47 48 // 49 // Deep copy 50 // 51 if(other._interceptorCBs != null) 52 { 53 // 54 // Copy, not just reference 55 // 56 _interceptorCBs = 57 new java.util.LinkedList<Ice.DispatchInterceptorAsyncCallback>(other._interceptorCBs); 58 } 59 } 60 61 public Ice.OutputStream startWriteParams()62 startWriteParams() 63 { 64 if(!_response) 65 { 66 throw new Ice.MarshalException("can't marshal out parameters for oneway dispatch"); 67 } 68 69 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 70 _os.writeBlob(Protocol.replyHdr); 71 _os.writeInt(_current.requestId); 72 _os.writeByte(ReplyStatus.replyOK); 73 _os.startEncapsulation(_current.encoding, _format); 74 return _os; 75 } 76 77 public void endWriteParams()78 endWriteParams() 79 { 80 if(_response) 81 { 82 _os.endEncapsulation(); 83 } 84 } 85 86 public void writeEmptyParams()87 writeEmptyParams() 88 { 89 if(_response) 90 { 91 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 92 _os.writeBlob(Protocol.replyHdr); 93 _os.writeInt(_current.requestId); 94 _os.writeByte(ReplyStatus.replyOK); 95 _os.writeEmptyEncapsulation(_current.encoding); 96 } 97 } 98 99 public void writeParamEncaps(byte[] v, boolean ok)100 writeParamEncaps(byte[] v, boolean ok) 101 { 102 if(!ok && _observer != null) 103 { 104 _observer.userException(); 105 } 106 107 if(_response) 108 { 109 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 110 _os.writeBlob(Protocol.replyHdr); 111 _os.writeInt(_current.requestId); 112 _os.writeByte(ok ? ReplyStatus.replyOK : ReplyStatus.replyUserException); 113 if(v == null || v.length == 0) 114 { 115 _os.writeEmptyEncapsulation(_current.encoding); 116 } 117 else 118 { 119 _os.writeEncapsulation(v); 120 } 121 } 122 } 123 124 // 125 // These functions allow this object to be reused, rather than reallocated. 126 // 127 public void reset(Instance instance, ResponseHandler handler, Ice.ConnectionI connection, Ice.ObjectAdapter adapter, boolean response, byte compress, int requestId)128 reset(Instance instance, ResponseHandler handler, Ice.ConnectionI connection, Ice.ObjectAdapter adapter, 129 boolean response, byte compress, int requestId) 130 { 131 _instance = instance; 132 _responseHandler = handler; 133 _response = response; 134 _compress = compress; 135 136 // 137 // Don't recycle the Current object, because servants may keep a reference to it. 138 // 139 _current = new Ice.Current(); 140 _current.id = new Ice.Identity(); 141 _current.adapter = adapter; 142 _current.con = connection; 143 _current.requestId = requestId; 144 145 _interceptorCBs = null; 146 } 147 148 public void reclaim()149 reclaim() 150 { 151 _current = null; 152 _servant = null; 153 _locator = null; 154 155 if(_cookie != null) 156 { 157 _cookie.value = null; 158 } 159 160 //_observer = null; 161 assert(_observer == null); 162 163 _os = null; 164 165 _responseHandler = null; 166 167 _interceptorCBs = null; 168 } 169 170 final protected void response(boolean amd)171 response(boolean amd) 172 { 173 try 174 { 175 if(_locator != null && !servantLocatorFinished(amd)) 176 { 177 return; 178 } 179 180 assert(_responseHandler != null); 181 if(_response) 182 { 183 if(_observer != null) 184 { 185 _observer.reply(_os.size() - Protocol.headerSize - 4); 186 } 187 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 188 } 189 else 190 { 191 _responseHandler.sendNoResponse(); 192 } 193 } 194 catch(Ice.LocalException ex) 195 { 196 _responseHandler.invokeException(_current.requestId, ex, 1, amd); // Fatal invocation exception 197 } 198 199 if(_observer != null) 200 { 201 _observer.detach(); 202 _observer = null; 203 } 204 _responseHandler = null; 205 } 206 207 final protected void exception(java.lang.Throwable exc, boolean amd)208 exception(java.lang.Throwable exc, boolean amd) 209 { 210 try 211 { 212 if(_locator != null && !servantLocatorFinished(amd)) 213 { 214 return; 215 } 216 handleException(exc, amd); 217 } 218 catch(Ice.LocalException ex) 219 { 220 _responseHandler.invokeException(_current.requestId, ex, 1, amd); // Fatal invocation exception 221 } 222 } 223 224 final protected void warning(java.lang.Throwable ex)225 warning(java.lang.Throwable ex) 226 { 227 assert(_instance != null); 228 229 java.io.StringWriter sw = new java.io.StringWriter(); 230 java.io.PrintWriter pw = new java.io.PrintWriter(sw); 231 IceUtilInternal.OutputBase out = new IceUtilInternal.OutputBase(pw); 232 out.setUseTab(false); 233 out.print("dispatch exception:"); 234 out.print("\nidentity: " + Ice.Util.identityToString(_current.id, _instance.toStringMode())); 235 out.print("\nfacet: " + IceUtilInternal.StringUtil.escapeString(_current.facet, "", _instance.toStringMode())); 236 out.print("\noperation: " + _current.operation); 237 if(_current.con != null) 238 { 239 try 240 { 241 for(Ice.ConnectionInfo connInfo = _current.con.getInfo(); connInfo != null; connInfo = connInfo.underlying) 242 { 243 if(connInfo instanceof Ice.IPConnectionInfo) 244 { 245 Ice.IPConnectionInfo ipConnInfo = (Ice.IPConnectionInfo)connInfo; 246 out.print("\nremote host: " + ipConnInfo.remoteAddress + " remote port: " + ipConnInfo.remotePort); 247 } 248 } 249 } 250 catch(Ice.LocalException exc) 251 { 252 // Ignore. 253 } 254 } 255 out.print("\n"); 256 ex.printStackTrace(pw); 257 pw.flush(); 258 _instance.initializationData().logger.warning(sw.toString()); 259 } 260 261 final protected boolean servantLocatorFinished(boolean amd)262 servantLocatorFinished(boolean amd) 263 { 264 assert(_locator != null && _servant != null); 265 try 266 { 267 assert(_cookie != null); 268 _locator.finished(_current, _servant, _cookie.value); 269 return true; 270 } 271 catch(java.lang.Throwable ex) 272 { 273 handleException(ex, amd); 274 } 275 return false; 276 } 277 278 final protected void handleException(java.lang.Throwable exc, boolean amd)279 handleException(java.lang.Throwable exc, boolean amd) 280 { 281 assert(_responseHandler != null); 282 283 if(exc instanceof Ice.SystemException) 284 { 285 if(_responseHandler.systemException(_current.requestId, (Ice.SystemException)exc, amd)) 286 { 287 return; 288 } 289 } 290 291 try 292 { 293 throw exc; 294 } 295 catch(Ice.RequestFailedException ex) 296 { 297 if(ex.id == null || ex.id.name == null || ex.id.name.isEmpty()) 298 { 299 ex.id = _current.id; 300 } 301 302 if(ex.facet == null || ex.facet.isEmpty()) 303 { 304 ex.facet = _current.facet; 305 } 306 307 if(ex.operation == null || ex.operation.length() == 0) 308 { 309 ex.operation = _current.operation; 310 } 311 312 if(_instance.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 1) 313 { 314 warning(ex); 315 } 316 317 if(_observer != null) 318 { 319 _observer.failed(ex.ice_id()); 320 } 321 322 if(_response) 323 { 324 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 325 _os.writeBlob(Protocol.replyHdr); 326 _os.writeInt(_current.requestId); 327 if(ex instanceof Ice.ObjectNotExistException) 328 { 329 _os.writeByte(ReplyStatus.replyObjectNotExist); 330 } 331 else if(ex instanceof Ice.FacetNotExistException) 332 { 333 _os.writeByte(ReplyStatus.replyFacetNotExist); 334 } 335 else if(ex instanceof Ice.OperationNotExistException) 336 { 337 _os.writeByte(ReplyStatus.replyOperationNotExist); 338 } 339 else 340 { 341 assert(false); 342 } 343 ex.id.ice_writeMembers(_os); 344 345 // 346 // For compatibility with the old FacetPath. 347 // 348 if(ex.facet == null || ex.facet.length() == 0) 349 { 350 _os.writeStringSeq(null); 351 } 352 else 353 { 354 String[] facetPath2 = { ex.facet }; 355 _os.writeStringSeq(facetPath2); 356 } 357 358 _os.writeString(ex.operation); 359 360 if(_observer != null) 361 { 362 _observer.reply(_os.size() - Protocol.headerSize - 4); 363 } 364 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 365 } 366 else 367 { 368 _responseHandler.sendNoResponse(); 369 } 370 } 371 catch(Ice.UnknownLocalException ex) 372 { 373 if(_instance.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 0) 374 { 375 warning(ex); 376 } 377 378 if(_observer != null) 379 { 380 _observer.failed(ex.ice_id()); 381 } 382 383 if(_response) 384 { 385 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 386 _os.writeBlob(Protocol.replyHdr); 387 _os.writeInt(_current.requestId); 388 _os.writeByte(ReplyStatus.replyUnknownLocalException); 389 _os.writeString(ex.unknown); 390 if(_observer != null) 391 { 392 _observer.reply(_os.size() - Protocol.headerSize - 4); 393 } 394 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 395 } 396 else 397 { 398 _responseHandler.sendNoResponse(); 399 } 400 } 401 catch(Ice.UnknownUserException ex) 402 { 403 if(_instance.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 0) 404 { 405 warning(ex); 406 } 407 408 if(_observer != null) 409 { 410 _observer.failed(ex.ice_id()); 411 } 412 413 if(_response) 414 { 415 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 416 _os.writeBlob(Protocol.replyHdr); 417 _os.writeInt(_current.requestId); 418 _os.writeByte(ReplyStatus.replyUnknownUserException); 419 _os.writeString(ex.unknown); 420 if(_observer != null) 421 { 422 _observer.reply(_os.size() - Protocol.headerSize - 4); 423 } 424 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 425 } 426 else 427 { 428 _responseHandler.sendNoResponse(); 429 } 430 } 431 catch(Ice.UnknownException ex) 432 { 433 if(_instance.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 0) 434 { 435 warning(ex); 436 } 437 438 if(_observer != null) 439 { 440 _observer.failed(ex.ice_id()); 441 } 442 443 if(_response) 444 { 445 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 446 _os.writeBlob(Protocol.replyHdr); 447 _os.writeInt(_current.requestId); 448 _os.writeByte(ReplyStatus.replyUnknownException); 449 _os.writeString(ex.unknown); 450 if(_observer != null) 451 { 452 _observer.reply(_os.size() - Protocol.headerSize - 4); 453 } 454 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 455 } 456 else 457 { 458 _responseHandler.sendNoResponse(); 459 } 460 } 461 catch(Ice.Exception ex) 462 { 463 if(_instance.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 0) 464 { 465 warning(ex); 466 } 467 468 if(_observer != null) 469 { 470 _observer.failed(ex.ice_id()); 471 } 472 473 if(_response) 474 { 475 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 476 _os.writeBlob(Protocol.replyHdr); 477 _os.writeInt(_current.requestId); 478 _os.writeByte(ReplyStatus.replyUnknownLocalException); 479 //_os.writeString(ex.toString()); 480 java.io.StringWriter sw = new java.io.StringWriter(); 481 sw.write(ex.ice_id() + "\n"); 482 java.io.PrintWriter pw = new java.io.PrintWriter(sw); 483 ex.printStackTrace(pw); 484 pw.flush(); 485 _os.writeString(sw.toString()); 486 if(_observer != null) 487 { 488 _observer.reply(_os.size() - Protocol.headerSize - 4); 489 } 490 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 491 } 492 else 493 { 494 _responseHandler.sendNoResponse(); 495 } 496 } 497 catch(Ice.UserException ex) 498 { 499 if(_observer != null) 500 { 501 _observer.userException(); 502 } 503 504 if(_response) 505 { 506 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 507 _os.writeBlob(Protocol.replyHdr); 508 _os.writeInt(_current.requestId); 509 _os.writeByte(ReplyStatus.replyUserException); 510 _os.startEncapsulation(_current.encoding, _format); 511 _os.writeException(ex); 512 _os.endEncapsulation(); 513 if(_observer != null) 514 { 515 _observer.reply(_os.size() - Protocol.headerSize - 4); 516 } 517 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 518 } 519 else 520 { 521 _responseHandler.sendNoResponse(); 522 } 523 } 524 catch(java.lang.Throwable ex) 525 { 526 if(_instance.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 0) 527 { 528 warning(ex); 529 } 530 531 if(_observer != null) 532 { 533 _observer.failed(ex.getClass().getName()); 534 } 535 536 if(_response) 537 { 538 _os = new Ice.OutputStream(_instance, Protocol.currentProtocolEncoding); 539 _os.writeBlob(Protocol.replyHdr); 540 _os.writeInt(_current.requestId); 541 _os.writeByte(ReplyStatus.replyUnknownException); 542 //_os.writeString(ex.toString()); 543 java.io.StringWriter sw = new java.io.StringWriter(); 544 java.io.PrintWriter pw = new java.io.PrintWriter(sw); 545 ex.printStackTrace(pw); 546 pw.flush(); 547 _os.writeString(sw.toString()); 548 if(_observer != null) 549 { 550 _observer.reply(_os.size() - Protocol.headerSize - 4); 551 } 552 _responseHandler.sendResponse(_current.requestId, _os, _compress, amd); 553 } 554 else 555 { 556 _responseHandler.sendNoResponse(); 557 } 558 559 if(_observer != null) 560 { 561 _observer.detach(); 562 _observer = null; 563 } 564 _responseHandler = null; 565 566 if(!amd && ex instanceof java.lang.Error) 567 { 568 throw new ServantError((java.lang.Error)ex); 569 } 570 } 571 572 if(_observer != null) 573 { 574 _observer.detach(); 575 _observer = null; 576 } 577 _responseHandler = null; 578 } 579 580 protected Instance _instance; 581 protected Ice.Current _current; 582 protected Ice.Object _servant; 583 protected Ice.ServantLocator _locator; 584 protected Ice.LocalObjectHolder _cookie; 585 protected Ice.Instrumentation.DispatchObserver _observer; 586 587 protected boolean _response; 588 protected byte _compress; 589 protected Ice.FormatType _format; 590 591 protected Ice.OutputStream _os; 592 593 protected ResponseHandler _responseHandler; 594 595 protected java.util.LinkedList<Ice.DispatchInterceptorAsyncCallback> _interceptorCBs; 596 } 597