- Timestamp:
- 07/05/08 22:16:06 (5 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/components/blitz/src/ome/services/blitz/fire/SessionManagerI.java
r2382 r2596 11 11 import java.util.HashSet; 12 12 import java.util.List; 13 import java.util.Map; 13 14 import java.util.Set; 15 import java.util.concurrent.ConcurrentHashMap; 14 16 15 17 import ome.conditions.InternalException; … … 25 27 import ome.system.Principal; 26 28 import ome.system.Roles; 29 import omero.ApiUsageException; 27 30 import omero.constants.EVENT; 28 31 import omero.constants.GROUP; … … 71 74 protected final Set<String> sessionsForReaping = new HashSet<String>(); 72 75 76 /** 77 * An internal mapping to all {@link ServiceFactoryI} instances for a given 78 * session since there is no method on {@link Ice.ObjectAdapter} to retrieve 79 * all servants. 80 */ 81 protected final Map<String, Set<String>> sessionToClientIds = new ConcurrentHashMap<String, Set<String>>(); 82 73 83 public SessionManagerI(SecuritySystem secSys, 74 84 SessionManager sessionManager, Executor executor) { … … 102 112 103 113 try { 104 Ice.ObjectPrx _prx; 114 115 // Create the session for this ServiceFactory 105 116 Principal p = new Principal(userId, group, event); 106 117 ome.model.meta.Session s = sessionManager.create(p); 107 118 Principal sp = new Principal(s.getUuid(), group, event); 108 119 109 Ice.Identity id = ServiceFactoryI.sessionId(s.getUuid()); 110 Ice.Object servant = current.adapter.find(id); 111 if (servant == null) { 112 ServiceFactoryI session = new ServiceFactoryI(context, 113 sessionManager, executor, sp, CPTORS); 114 115 _prx = current.adapter.add(session, id); 116 if (log.isDebugEnabled()) { 117 log.debug(String.format("Created session %s for user %s", 118 id.name, userId)); 119 } 120 // Create the ServiceFactory 121 ServiceFactoryI session = new ServiceFactoryI(current, context, 122 sessionManager, executor, sp, CPTORS); 123 124 Ice.Identity id = session.sessionId(s.getUuid()); 125 Ice.ObjectPrx _prx = current.adapter.add(session, id); 126 if (!sessionToClientIds.containsKey(s.getUuid())) { 127 sessionToClientIds.put(s.getUuid(), new HashSet<String>()); 128 log.debug(String.format("Created session %s for user %s", 129 id.name, userId)); 120 130 } else { 131 sessionToClientIds.get(s.getUuid()).add(session.clientId); 121 132 if (log.isDebugEnabled()) { 122 133 log.debug(String.format("Rejoined session %s for user %s", 123 134 id.name, userId)); 124 135 } 125 _prx = current.adapter.createProxy(id); 136 126 137 } 127 138 return Glacier2.SessionPrxHelper.uncheckedCast(_prx); 128 139 129 } catch ( RuntimeException t) {140 } catch (Exception t) { 130 141 ConvertToBlitzExceptionMessage convert = new ConvertToBlitzExceptionMessage( 131 142 this, t); … … 139 150 if (convert.to instanceof CannotCreateSessionException) { 140 151 throw (CannotCreateSessionException) convert.to; 152 } else if (convert.to instanceof ApiUsageException) { 153 ApiUsageException aue = (ApiUsageException) convert.to; 154 throw new CannotCreateSessionException(aue.message); 141 155 } 142 156 … … 158 172 159 173 // And unregister the service if possible 160 Ice.Identity id = ServiceFactoryI.sessionId(curr.id.category); 174 Ice.Identity id; 175 try { 176 id = ServiceFactoryI.sessionId(curr, curr.id.category); 177 } catch (ApiUsageException e) { 178 throw new RuntimeException( 179 "Could not unregister servant: could not create session id"); 180 } 161 181 Ice.Object obj = curr.adapter.find(id); 162 182 if (obj instanceof ServiceFactoryI) { 163 183 ServiceFactoryI sf = (ServiceFactoryI) obj; 164 sf.unregisterServant(Ice.Util.stringToIdentity(key), curr); 184 sf.unregisterServant(Ice.Util.stringToIdentity(key), 185 curr.adapter); 165 186 } 166 187 } else if (event instanceof DestroySessionMessage) { … … 180 201 * Unfortunately, that message does not have an {@link Ice.Current} instance 181 202 * and so reaping must happen asynchronously. 203 * 204 * @param cantUseThisCurrent 205 * a current from another method invocation which is not usable 206 * for reaping the given sessions except to get the current 207 * {@link Ice.ObjectAdapter} 182 208 */ 183 private void reapSessions(Ice.Current current) { 209 private void reapSessions(Ice.Current cantUseThisCurrent) { 210 Ice.ObjectAdapter adapter = cantUseThisCurrent.adapter; 184 211 synchronized (sessionsForReaping) { 185 212 List<String> ids = new ArrayList<String>(sessionsForReaping); 186 213 for (String id : ids) { 187 try { 188 Ice.Identity iid = ServiceFactoryI.sessionId(id); 189 Ice.Object obj = current.adapter.find(iid); 190 if (obj == null) { 191 log.debug(id + " already removed."); 192 } else { 193 ServiceFactoryI sf = (ServiceFactoryI) obj; 194 sf.close(current); 214 for (String clientId : sessionToClientIds.get(id)) { 215 try { 216 Ice.Identity iid = ServiceFactoryI.sessionId(clientId, 217 id); 218 Ice.Object obj = adapter.find(iid); 219 if (obj == null) { 220 log.debug(id + " already removed."); 221 } else { 222 ServiceFactoryI sf = (ServiceFactoryI) obj; 223 sf.doDestroy(adapter); 224 } 225 sessionsForReaping.remove(id); 226 } catch (Exception e) { 227 log.error("Error reaping session " + id 228 + " from client " + clientId, e); 195 229 } 196 sessionsForReaping.remove(id);197 } catch (Exception e) {198 log.error("Error reaping session " + id, e);199 230 } 200 231 }
