/** * 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; }
/** * Create child scope of room type, with the given name. * * @param name child scope name * @return * <pre> * true * </pre> * on success, * <pre> * false * </pre> * otherwise */ public boolean createChildScope(String name) { // quick lookup by name log.debug("createChildScope: {}", name); if (children.hasName(name)) { log.debug("Scope: {} already exists, children: {}", name, children.getNames()); } else { return addChildScope(new Builder(this, ScopeType.ROOM, name, false).build()); } return false; }
/** * Removes child scope * * @param scope Child scope to remove */ public void removeChildScope(IBasicScope scope) { log.debug("removeChildScope: {}", scope); if (children.containsKey(scope)) { // remove from parent children.remove(scope); if (scope instanceof Scope) { unregisterJMX(); } } }
/** * Return child scope by name * * @param name Scope name * @return Child scope with given name */ public IScope getScope(String name) { IBasicScope child = children.getBasicScope(ScopeType.UNDEFINED, name); log.debug("Child of {}: {}", this.name, child); if (child != null) { if (child instanceof IScope) { return (IScope) child; } log.warn("Requested scope: {} is not of IScope type: {}", name, child.getClass().getName()); } return null; }
// for debugging public void dump() { if (log.isTraceEnabled()) { log.trace("Scope: {} {}", this.getClass().getName(), this); log.trace("Running: {}", running); if (hasParent()) { log.trace("Parent: {}", parent); Set<String> names = parent.getBasicScopeNames(null); log.trace("Sibling count: {}", names.size()); for (String sib : names) { log.trace("Siblings - {}", sib); } names = null; } log.trace("Handler: {}", handler); log.trace("Child count: {}", children.size()); for (IBasicScope child : children.keySet()) { log.trace("Child: {}", child); } } }
/** * Return basic scope names matching given type * * @param type Scope type * @return set of scope names */ public Set<String> getBasicScopeNames(ScopeType type) { if (type != null) { Set<String> names = new HashSet<String>(); for (IBasicScope child : children.keySet()) { if (child.getType().equals(type)) { names.add(child.getName()); } } return names; } return getScopeNames(); }
/** 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(); } } }
/** * Check whether scope has child scope with given name and type * * @param type Child scope type * @param name Child scope name * @return * <pre> * true * </pre> * if scope has child node with given name and type, * <pre> * false * </pre> * otherwise */ public boolean hasChildScope(ScopeType type, String name) { log.debug("Has child scope? {} in {}", name, this); return children.getBasicScope(type, name) != null; }
/** * Check whether scope has child scope with given name * * @param name Child scope name * @return * <pre> * true * </pre> * if scope has child node with given name, * <pre> * false * </pre> * otherwise */ public boolean hasChildScope(String name) { log.debug("Has child scope? {} in {}", name, this); return children.hasName(name); }
/** * Return child scope names iterator * * @return Child scope names iterator */ public Set<String> getScopeNames() { log.debug("Children: {}", children); return children.getNames(); }
/** * Return base scope of given type with given name * * @param type Scope type * @param name Scope name * @return Basic scope object */ public IBasicScope getBasicScope(ScopeType type, String name) { return children.getBasicScope(type, name); }
/** * Return the broadcast scope for a given name * * @param name name * @return broadcast scope or null if not found */ public IBroadcastScope getBroadcastScope(String name) { return (IBroadcastScope) children.getBasicScope(ScopeType.BROADCAST, name); }
/** Removes all the child scopes */ public void removeChildren() { log.trace("removeChildren of {}", name); for (IBasicScope child : children.keySet()) { removeChildScope(child); } }