• Views
  • Iteration Report
  • My Iteration Report
  •  
OMERO.server
  • Login
  • Help/Guide
  • About Trac
  • Preferences
  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • View Tickets
  • Search

Context Navigation

  • ← Previous Change
  • Next Change →

Changeset 2596 for trunk/components/blitz/src/ome/services/blitz/impl/ServiceFactoryI.java

Show
Ignore:
Timestamp:
07/05/08 22:16:06 (5 months ago)
Author:
jmoore
Message:

ticket:1018 - Working solution (needs concurrency testing)

Each ServiceFactory instance now keeps track of its clientId.

This solution uses a managed map in SessionManagerI to
make up for the missing ObjectAdapter.findAll() method.

Files:
1 modified

  • trunk/components/blitz/src/ome/services/blitz/impl/ServiceFactoryI.java (modified) (10 diffs)

Legend:

Unmodified
Added
Removed
  • trunk/components/blitz/src/ome/services/blitz/impl/ServiceFactoryI.java

    r2593 r2596  
    126126public final class ServiceFactoryI extends _ServiceFactoryDisp { 
    127127 
     128    // STATIC 
     129 
    128130    private final static Log log = LogFactory.getLog(ServiceFactoryI.class); 
     131 
     132    // PRIVATE STATE 
     133 
     134    boolean doClose = false; 
     135 
     136    public final String clientId; 
     137 
     138    // SHARED STATE 
    129139 
    130140    final SessionManager sessionManager; 
    … …  
    145155    final Map<String, Ice.Object> interactiveSlots; 
    146156 
    147     /** 
    148      * The CLIENT_UUIDs for which a close should be invoked on this session. 
    149      */ 
    150     final Map<String, Boolean> closeUuids; 
    151  
    152157    final Principal principal; 
    153158 
    … …  
    163168    // ========================================================================= 
    164169 
    165     public ServiceFactoryI(OmeroContext context, SessionManager manager, 
    166             Executor executor, Principal p, 
    167             List<HardWiredInterceptor> interceptors) { 
     170    public ServiceFactoryI(Ice.Current current, OmeroContext context, 
     171            SessionManager manager, Executor executor, Principal p, 
     172            List<HardWiredInterceptor> interceptors) throws ApiUsageException { 
     173        this.clientId = clientId(current); 
    168174        this.context = context; 
    169175        this.sessionManager = manager; 
    … …  
    180186        activeServants = getMap(ACTIVE_SERVANTS, cache); 
    181187        interactiveSlots = getMap(INTERACTIVE_SLOTS, cache); 
    182         closeUuids = getMap(CLOSE_UUIDS, cache); 
    183188    } 
    184189 
    185190    static String ACTIVE_SERVANTS = "activeServants"; 
    186191    static String INTERACTIVE_SLOTS = "interactiveSlots"; 
    187     static String CLOSE_UUIDS = "closeUuids"; 
    188192 
    189193    @SuppressWarnings("unchecked") 
    … …  
    474478 
    475479    public void detachOnDestroy(Ice.Current current) { 
    476         String key = current.ctx.get(omero.constants.CLIENTUUID.value); 
    477         closeUuids.remove(key); 
     480        doClose = false; 
    478481    } 
    479482 
    480483    @Deprecated 
    481484    public void close(Ice.Current current) { 
    482         closeOnDestroy(current); 
     485        doClose = false; 
    483486    } 
    484487 
    485488    public void closeOnDestroy(Ice.Current current) { 
    486         String key = current.ctx.get(omero.constants.CLIENTUUID.value); 
    487         closeUuids.put(key, Boolean.TRUE); 
     489        doClose = true; 
    488490    } 
    489491 
    … …  
    501503        // If we are supposed to close, do only so if the ref count 
    502504        // is < 1. 
    503         String key = current.ctx.get(omero.constants.CLIENTUUID.value); 
    504         if (key != null && ref < 1) { 
     505        if (doClose && ref < 1) { 
    505506 
    506507            ref = sessionManager.close(this.principal.getName()); 
    … …  
    512513            } 
    513514 
    514             if (log.isInfoEnabled()) { 
    515                 log.info(String.format("Closing %s session", this)); 
     515            doDestroy(current.adapter); 
     516 
     517        } 
     518    } 
     519 
     520    /** 
     521     * Performs the actual cleanup operation. Since {@link #destroy()} is called 
     522     * non-discriminantly by the router, even when a client has just died, we 
     523     * have this internal method for handling the actual closing of resources. 
     524     */ 
     525    public void doDestroy(Ice.ObjectAdapter adapter) { 
     526 
     527        if (log.isInfoEnabled()) { 
     528            log.info(String.format("Closing %s session", this)); 
     529        } 
     530 
     531        // Cleaning up resources 
     532 
     533        // INTERACTIVE 
     534        Set<String> ipIds = interactiveSlots.keySet(); 
     535        for (String id : ipIds) { 
     536            InteractiveProcessorI ip = (InteractiveProcessorI) interactiveSlots 
     537                    .get(id); 
     538            ip.stop(); 
     539            adapter.remove(Ice.Util.stringToIdentity(id)); 
     540            interactiveSlots.remove(id); 
     541        } 
     542 
     543        // SERVANTS 
     544        // Here we call the "close()" method on all methods which require 
     545        // that 
     546        // logic allowing the IceMethodInvoker to raise the 
     547        // UnregisterServantEvent, otherwise there is a recursive call back 
     548        // to close. 
     549        List<String> ids = activeServices(); 
     550        for (String idString : ids) { 
     551            Ice.Identity id = Ice.Util.stringToIdentity(idString); 
     552            Ice.Object obj = adapter.find(id); 
     553            try { 
     554                if (obj instanceof StatefulServiceInterface) { 
     555                    Method m = obj.getClass().getMethod("close", 
     556                            Ice.Current.class); 
     557                    Ice.Current __curr = new Ice.Current(); 
     558                    __curr.id = id; 
     559                    __curr.adapter = adapter; 
     560                    __curr.operation = "close"; 
     561                    m.invoke(obj, __curr); 
     562                } else { 
     563                    unregisterServant(id, adapter); 
     564                } 
     565            } catch (Exception e) { 
     566                log.error("Failure to close: " + idString + "=" + obj, e); 
    516567            } 
    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             } 
     568        } 
     569 
     570        // All resources cleaned up to the best of our ability, 
     571        // now we can remove the current session. If an exception if thrown, 
     572        // there's not much we can do. 
     573        try { 
     574            adapter.remove(sessionId(principal.getName())); 
     575        } catch (Throwable t) { 
     576            // FIXME 
     577            InternalException ie = new InternalException(t.getMessage()); 
     578            ie.setStackTrace(t.getStackTrace()); 
    570579        } 
    571580    } 
    … …  
    723732     * to an {@link UnregisterServantMessage} 
    724733     */ 
    725     public void unregisterServant(Ice.Identity id, Ice.Current current) { 
     734    public void unregisterServant(Ice.Identity id, Ice.ObjectAdapter adapter) { 
    726735 
    727736        // Here we assume that if the "close()" call is required, that it has 
    … …  
    730739        // onApplicationEvent(). 
    731740        // Otherwise, it is being called directly by SF.close(). 
    732         Ice.Object obj = current.adapter.remove(id); 
     741        Ice.Object obj = adapter.remove(id); 
    733742        Object removed = activeServants.remove(Ice.Util.identityToString(id)); 
    734743        if (removed == null) { 
    … …  
    748757    } 
    749758 
    750     public static Ice.Identity sessionId(String uuid) { 
     759    // Id Helpers 
     760    // ========================================================================= 
     761    // Used for naming service factory instances and creating Ice.Identities 
     762    // from Ice.Currents, etc. 
     763 
     764    public static Ice.Identity sessionId(Ice.Current current, String uuid) 
     765            throws ApiUsageException { 
     766        String clientId = clientId(current); 
     767        return sessionId(clientId, uuid); 
     768    } 
     769 
     770    public static Ice.Identity sessionId(String clientId, String uuid) { 
    751771        Ice.Identity id = new Ice.Identity(); 
    752         id.category = "session"; 
     772        id.category = "session-" + clientId; 
    753773        id.name = uuid; 
    754774        return id; 
     775 
     776    } 
     777 
     778    public Ice.Identity sessionId(String uuid) { 
     779        return sessionId(clientId, uuid); 
     780    } 
     781 
     782    public static String clientId(Ice.Current current) throws ApiUsageException { 
     783        String clientId = null; 
     784        if (current.ctx != null) { 
     785            clientId = current.ctx.get(omero.constants.CLIENTUUID.value); 
     786        } 
     787        if (clientId == null) { 
     788            throw new ApiUsageException(null, null, "No " 
     789                    + omero.constants.CLIENTUUID.value 
     790                    + " key provided in context."); 
     791        } 
     792        return clientId; 
    755793    } 
    756794 

Download in other formats:

  • Unified Diff
  • Zip Archive

Trac Powered

Powered by Trac 0.11
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/