- Timestamp:
- 07/05/08 22:16:05 (5 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/components/blitz/src/ome/services/blitz/impl/ServiceFactoryI.java
r2592 r2593 145 145 final Map<String, Ice.Object> interactiveSlots; 146 146 147 /** 148 * The CLIENT_UUIDs for which a close should be invoked on this session. 149 */ 150 final Map<String, Boolean> closeUuids; 151 147 152 final Principal principal; 148 153 … … 172 177 // Setting up in memory store. 173 178 Ehcache cache = manager.inMemoryCache(p.getName()); 174 activeServants = getServantMap(ACTIVE_SERVANTS, cache); 175 interactiveSlots = getServantMap(INTERACTIVE_SLOTS, cache); 179 // TODO the getMap() method could be put in our own Cache class 180 activeServants = getMap(ACTIVE_SERVANTS, cache); 181 interactiveSlots = getMap(INTERACTIVE_SLOTS, cache); 182 closeUuids = getMap(CLOSE_UUIDS, cache); 176 183 } 177 184 178 185 static String ACTIVE_SERVANTS = "activeServants"; 179 186 static String INTERACTIVE_SLOTS = "interactiveSlots"; 180 181 private Map<String, Ice.Object> getServantMap(String key, Ehcache cache) { 182 Map<String, Ice.Object> ids; 187 static String CLOSE_UUIDS = "closeUuids"; 188 189 @SuppressWarnings("unchecked") 190 private <T> Map<String, T> getMap(String key, Ehcache cache) { 191 Map<String, T> ids; 183 192 if (!cache.isKeyInCache(key)) { 184 ids = new ConcurrentHashMap<String, Ice.Object>();193 ids = new ConcurrentHashMap<String, T>(); 185 194 cache.put(new Element(key, ids)); 186 195 } else { 187 ids = (Map<String, Ice.Object>) cache.get(key).getObjectValue();196 ids = (Map<String, T>) cache.get(key).getObjectValue(); 188 197 } 189 198 return ids; … … 464 473 } 465 474 475 public void detachOnDestroy(Ice.Current current) { 476 String key = current.ctx.get(omero.constants.CLIENTUUID.value); 477 closeUuids.remove(key); 478 } 479 480 @Deprecated 466 481 public void close(Ice.Current current) { 467 468 if (log.isInfoEnabled()) { 469 log.info(String.format("Closing %s session", this)); 470 } 471 472 // Cleaning up resources 473 474 // INTERACTIVE 475 Set<String> ipIds = interactiveSlots.keySet(); 476 for (String id : ipIds) { 477 InteractiveProcessorI ip = (InteractiveProcessorI) interactiveSlots 478 .get(id); 479 ip.stop(); 480 current.adapter.remove(Ice.Util.stringToIdentity(id)); 481 interactiveSlots.remove(id); 482 } 483 484 // SERVANTS 485 // Here we call the "close()" method on all methods which require that 486 // logic allowing the IceMethodInvoker to raise the 487 // UnregisterServantEvent, otherwise there is a recursive call back 488 // to close. 489 List<String> ids = activeServices(current); 490 for (String idString : ids) { 491 Ice.Identity id = Ice.Util.stringToIdentity(idString); 492 Ice.Object obj = current.adapter.find(id); 493 try { 494 if (obj instanceof StatefulServiceInterface) { 495 Method m = obj.getClass().getMethod("close", 496 Ice.Current.class); 497 Ice.Current __curr = new Ice.Current(); 498 __curr.id = id; 499 __curr.adapter = current.adapter; 500 __curr.operation = "close"; 501 __curr.mode = current.mode; // FIXME due to bug 502 // http://www.zeroc.com/forums/bug-reports/3348-ice-current-hashcode-can-throw-npe-null-enum.html 503 m.invoke(obj, __curr); 504 } else { 505 unregisterServant(id, current); 506 } 507 } catch (Exception e) { 508 log.error("Failure to close: " + idString + "=" + obj, e); 509 } 510 } 511 512 // All resources cleaned up to the best of our ability, 513 // now we can remove the current session. If an exception if thrown, 514 // there's not much we can do. 515 try { 516 current.adapter.remove(ServiceFactoryI.sessionId(principal 517 .getName())); 518 sessionManager.close(this.principal.getName()); 519 } catch (Throwable t) { 520 // FIXME 521 InternalException ie = new InternalException(t.getMessage()); 522 ie.setStackTrace(t.getStackTrace()); 523 } 482 closeOnDestroy(current); 483 } 484 485 public void closeOnDestroy(Ice.Current current) { 486 String key = current.ctx.get(omero.constants.CLIENTUUID.value); 487 closeUuids.put(key, Boolean.TRUE); 524 488 } 525 489 … … 531 495 */ 532 496 public void destroy(Ice.Current current) { 533 sessionManager.detach(this.principal.getName()); 497 498 // First detach and get the reference count. 499 int ref = sessionManager.detach(this.principal.getName()); 500 501 // If we are supposed to close, do only so if the ref count 502 // is < 1. 503 String key = current.ctx.get(omero.constants.CLIENTUUID.value); 504 if (key != null && ref < 1) { 505 506 ref = sessionManager.close(this.principal.getName()); 507 508 // If someone has attached since we called detach() above, do not 509 // destroy this session. 510 if (ref > 0) { 511 return; 512 } 513 514 if (log.isInfoEnabled()) { 515 log.info(String.format("Closing %s session", this)); 516 } 517 518 // Cleaning up resources 519 520 // INTERACTIVE 521 Set<String> ipIds = interactiveSlots.keySet(); 522 for (String id : ipIds) { 523 InteractiveProcessorI ip = (InteractiveProcessorI) interactiveSlots 524 .get(id); 525 ip.stop(); 526 current.adapter.remove(Ice.Util.stringToIdentity(id)); 527 interactiveSlots.remove(id); 528 } 529 530 // SERVANTS 531 // Here we call the "close()" method on all methods which require 532 // that 533 // logic allowing the IceMethodInvoker to raise the 534 // UnregisterServantEvent, otherwise there is a recursive call back 535 // to close. 536 List<String> ids = activeServices(current); 537 for (String idString : ids) { 538 Ice.Identity id = Ice.Util.stringToIdentity(idString); 539 Ice.Object obj = current.adapter.find(id); 540 try { 541 if (obj instanceof StatefulServiceInterface) { 542 Method m = obj.getClass().getMethod("close", 543 Ice.Current.class); 544 Ice.Current __curr = new Ice.Current(); 545 __curr.id = id; 546 __curr.adapter = current.adapter; 547 __curr.operation = "close"; 548 __curr.mode = current.mode; // FIXME due to bug 549 // http://www.zeroc.com/forums/bug-reports/3348-ice-current-hashcode-can-throw-npe-null-enum.html 550 m.invoke(obj, __curr); 551 } else { 552 unregisterServant(id, current); 553 } 554 } catch (Exception e) { 555 log.error("Failure to close: " + idString + "=" + obj, e); 556 } 557 } 558 559 // All resources cleaned up to the best of our ability, 560 // now we can remove the current session. If an exception if thrown, 561 // there's not much we can do. 562 try { 563 current.adapter.remove(ServiceFactoryI.sessionId(principal 564 .getName())); 565 } catch (Throwable t) { 566 // FIXME 567 InternalException ie = new InternalException(t.getMessage()); 568 ie.setStackTrace(t.getStackTrace()); 569 } 570 } 534 571 } 535 572
