public boolean add(IBasicScope scope) { // log.trace("add - Hold counts - read: {} write: {} queued: {}", // internalLock.getReadHoldCount(), internalLock.getWriteHoldCount(), // internalLock.hasQueuedThreads()); boolean added = false; // check #1 if (!containsKey(scope)) { log.debug("Adding child scope: {} to {}", (((IBasicScope) scope).getName()), this); if (hasHandler()) { // get the handler for the scope to which we are adding this new scope IScopeHandler hdlr = getHandler(); // add the scope to the handler if (!hdlr.addChildScope(scope)) { log.warn("Failed to add child scope: {} to {}", scope, this); return false; } } else { log.debug("No handler found for {}", this); } try { // check #2 for entry if (!containsKey(scope)) { // add the entry // expected return from put is null; indicating this scope didn't already exist added = (super.put(scope, Boolean.TRUE) == null); if (added) { subscopeStats.increment(); } else { log.debug("Subscope was not added"); } } else { log.debug("Subscope already exists"); } } catch (Exception e) { log.warn("Exception on add", e); } if (added && scope instanceof Scope) { // cast it Scope scp = (Scope) scope; // start the scope if (scp.start()) { log.debug("Child scope started"); } else { log.debug("Failed to start child scope: {} in {}", scope, this); } } } return added; }
/** * Add child scope to this scope * * @param scope Child scope * @return * <pre> * true * </pre> * on success (if scope has handler and it accepts child scope addition), * <pre> * false * </pre> * otherwise */ public boolean addChildScope(IBasicScope scope) { log.debug("Add child: {}", scope); boolean added = false; if (scope.isValid()) { try { if (!children.containsKey(scope)) { log.debug("Adding child scope: {} to {}", scope, this); added = children.add(scope); } else { log.warn("Child scope already exists"); } } catch (Exception e) { log.warn("Exception on add subscope", e); } } else { log.warn("Invalid scope rejected: {}", scope); } if (added && scope.getStore() == null) { // if child scope has no persistence store, use same class as parent try { if (scope instanceof Scope) { ((Scope) scope).setPersistenceClass(persistenceClass); } } catch (Exception error) { log.error("Could not set persistence class", error); } } return added; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!super.equals(obj)) { return false; } if (getClass() != obj.getClass()) { return false; } Scope other = (Scope) obj; if (hashCode() != other.hashCode()) { return false; } return true; }
@Override public Boolean remove(Object scope) { log.debug("Remove child scope: {}", scope); // log.trace("remove - Hold counts - read: {} write: {} queued: {}", // internalLock.getReadHoldCount(), internalLock.getWriteHoldCount(), // internalLock.hasQueuedThreads()); if (hasHandler()) { IScopeHandler hdlr = getHandler(); log.debug("Removing child scope: {}", (((IBasicScope) scope).getName())); hdlr.removeChildScope((IBasicScope) scope); if (scope instanceof Scope) { // cast it Scope scp = (Scope) scope; // stop the scope scp.stop(); } } else { log.debug("No handler found for {}", this); } boolean removed = false; try { if (containsKey(scope)) { // remove the entry, ensure removed value is equal to the given object removed = super.remove(scope).equals(Boolean.TRUE); if (removed) { subscopeStats.decrement(); } else { log.debug("Subscope was not removed"); } } else { log.debug("Subscope was not removed, it was not found"); } } catch (Exception e) { log.warn("Exception on remove", e); } return removed; }
/** Uninitialize scope and unregister from parent. */ public void uninit() { log.debug("Un-init scope"); for (IBasicScope child : children.keySet()) { if (child instanceof Scope) { ((Scope) child).uninit(); } } stop(); setEnabled(false); if (hasParent()) { if (parent.hasChildScope(name)) { parent.removeChildScope(this); } } }
/** * Destroys scope * * @throws Exception on error */ public void destroy() throws Exception { log.debug("Destroy scope"); if (hasParent()) { parent.removeChildScope(this); } if (hasHandler()) { // Because handler can be null when there is a parent handler getHandler().stop(this); } // kill all child scopes for (IBasicScope child : children.keySet()) { removeChildScope(child); if (child instanceof Scope) { ((Scope) child).uninit(); } } }
/** {@inheritDoc} */ public boolean unregisterBroadcastStream(IScope scope, String name, IBroadcastStream bs) { if (log.isDebugEnabled()) { log.debug("Unregistering - name: {} stream: {} scope: {}", new Object[] {name, bs, scope}); ((Scope) scope).dump(); } IBroadcastScope broadcastScope = scope.getBroadcastScope(name); if (bs != null) { log.debug("Unsubscribing scope {} from provider {}", broadcastScope, bs.getProvider()); broadcastScope.unsubscribe(bs.getProvider()); } // if the scope has no listeners try to remove it if (!((BasicScope) broadcastScope).hasEventListeners()) { if (log.isDebugEnabled()) { log.debug("Scope has no event listeners attempting removal"); } scope.removeChildScope(broadcastScope); } // verify that scope was removed return scope.getBasicScope(ScopeType.BROADCAST, name) == null; }
/** {@inheritDoc} */ public boolean registerBroadcastStream(IScope scope, String name, IBroadcastStream bs) { if (log.isDebugEnabled()) { log.debug("Registering - name: {} stream: {} scope: {}", new Object[] {name, bs, scope}); ((Scope) scope).dump(); } IBroadcastScope broadcastScope = scope.getBroadcastScope(name); if (broadcastScope == null) { log.debug("Creating a new scope"); broadcastScope = new BroadcastScope(scope, name); if (scope.addChildScope(broadcastScope)) { log.debug("Broadcast scope added"); } else { log.warn("Broadcast scope was not added to {}", scope); } } // set the client broadcast stream if we have a broadcast scope if (broadcastScope != null && bs instanceof IClientBroadcastStream) { broadcastScope.setClientBroadcastStream((IClientBroadcastStream) bs); } if (log.isDebugEnabled()) { log.debug("Subscribing scope {} to provider {}", broadcastScope, bs.getProvider()); } return broadcastScope.subscribe(bs.getProvider(), null); }