/** * Determine if a {@link ScopeWrapper} is an ancestor of another {@link ScopeWrapper}. */ static <S extends ScopeType<S>> boolean isScopeAncestor(ScopeWrapper<S> scope, ScopeWrapper<S> possibleAncestor) { Queue<ScopeWrapper<S>> ancestors = new LinkedList<>(); ancestors.add(scope); while (true) { if (ancestors.isEmpty()) { return false; } if (ancestors.peek().equals(possibleAncestor)) { return true; } ancestors.addAll(ancestors.poll().getParentScopes()); } }
@Override public ScopeInstance<S> selfScope() { return this.selfScopeWrapper.getScope(); }
@Override public int hashCode() { int result = brokerCache.hashCode(); result = 31 * result + ancestorScopesByType.hashCode(); result = 31 * result + (selfScopeWrapper != null ? selfScopeWrapper.hashCode() : 0); return result; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } SharedResourcesBrokerImpl<?> that = (SharedResourcesBrokerImpl<?>) o; if (!brokerCache.equals(that.brokerCache)) { return false; } if (!ancestorScopesByType.equals(that.ancestorScopesByType)) { return false; } return selfScopeWrapper != null ? selfScopeWrapper.equals(that.selfScopeWrapper) : that.selfScopeWrapper == null; }
/** * Create a root {@link SharedResourcesBroker}. Subscoped brokers should be built using * {@link SharedResourcesBroker#newSubscopedBuilder(ScopeInstance)}. * * In general, this method will be called only once per application, and all other brokers will Nbe children of the root * application broker. * * @param config The global configuration of the broker. * @param globalScope The scope of the root broker. * @param <S> The {@link ScopeType} DAG used for this broker tree. */ public static <S extends ScopeType<S>> SharedResourcesBrokerImpl<S> createDefaultTopLevelBroker(Config config, ScopeInstance<S> globalScope) { if (!globalScope.getType().equals(globalScope.getType().rootScope())) { throw new IllegalArgumentException(String.format("The top level broker must be created at the root scope type. " + "%s is not a root scope type.", globalScope.getType())); } ScopeWrapper<S> scopeWrapper = new ScopeWrapper<>(globalScope.getType(), globalScope, Lists.<ScopeWrapper<S>>newArrayList()); return new SharedResourcesBrokerImpl<>(new DefaultBrokerCache<S>(), scopeWrapper, Lists.newArrayList(new SharedResourcesBrokerImpl.ScopedConfig<>(globalScope.getType(), getBrokerConfig(addSystemConfigurationToConfig(config)))), ImmutableMap.of(globalScope.getType(), scopeWrapper)); }
@Override public int hashCode() { int result = brokerCache.hashCode(); result = 31 * result + ancestorScopesByType.hashCode(); result = 31 * result + (selfScopeWrapper != null ? selfScopeWrapper.hashCode() : 0); return result; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } SharedResourcesBrokerImpl<?> that = (SharedResourcesBrokerImpl<?>) o; if (!brokerCache.equals(that.brokerCache)) { return false; } if (!ancestorScopesByType.equals(that.ancestorScopesByType)) { return false; } return selfScopeWrapper != null ? selfScopeWrapper.equals(that.selfScopeWrapper) : that.selfScopeWrapper == null; }
/** * Create a root {@link SharedResourcesBroker}. Subscoped brokers should be built using * {@link SharedResourcesBroker#newSubscopedBuilder(ScopeInstance)}. * * In general, this method will be called only once per application, and all other brokers will Nbe children of the root * application broker. * * @param config The global configuration of the broker. * @param globalScope The scope of the root broker. * @param <S> The {@link ScopeType} DAG used for this broker tree. */ public static <S extends ScopeType<S>> SharedResourcesBrokerImpl<S> createDefaultTopLevelBroker(Config config, ScopeInstance<S> globalScope) { if (!globalScope.getType().equals(globalScope.getType().rootScope())) { throw new IllegalArgumentException(String.format("The top level broker must be created at the root scope type. " + "%s is not a root scope type.", globalScope.getType())); } ScopeWrapper<S> scopeWrapper = new ScopeWrapper<>(globalScope.getType(), globalScope, Lists.<ScopeWrapper<S>>newArrayList()); return new SharedResourcesBrokerImpl<>(new DefaultBrokerCache<S>(), scopeWrapper, Lists.newArrayList(new SharedResourcesBrokerImpl.ScopedConfig<>(globalScope.getType(), getBrokerConfig(addSystemConfigurationToConfig(config)))), ImmutableMap.of(globalScope.getType(), scopeWrapper)); }
/** * Determine if a {@link ScopeWrapper} is an ancestor of another {@link ScopeWrapper}. */ static <S extends ScopeType<S>> boolean isScopeAncestor(ScopeWrapper<S> scope, ScopeWrapper<S> possibleAncestor) { Queue<ScopeWrapper<S>> ancestors = new LinkedList<>(); ancestors.add(scope); while (true) { if (ancestors.isEmpty()) { return false; } if (ancestors.peek().equals(possibleAncestor)) { return true; } ancestors.addAll(ancestors.poll().getParentScopes()); } }
@Override public ScopeInstance<S> getScope(S scopeType) throws NoSuchScopeException { return getWrappedScope(scopeType).getScope(); }
if (obj instanceof ResourceCoordinate) { ResourceCoordinate<T, K, S> resourceCoordinate = (ResourceCoordinate<T, K, S>) obj; if (!SharedResourcesBrokerUtils.isScopeTypeAncestor((ScopeType) currentScope.getType(), ((ResourceCoordinate) obj).getScope())) { throw new RuntimeException(String .format("%s returned an invalid coordinate: scope %s is not an ancestor of %s.", currentFactory.getName(), ((ResourceCoordinate) obj).getScope(), currentScope.getType()));
@Override public void close() throws IOException { ScopeInstance<S> scope = this.selfScopeWrapper.getScope(); log.info("Closing broker with scope {} of id {}.", scope.getType().toString(), scope.getScopeId()); this.brokerCache.close(this.selfScopeWrapper); } }
@Override public ScopeInstance<S> selfScope() { return this.selfScopeWrapper.getScope(); }
if (obj instanceof ResourceCoordinate) { ResourceCoordinate<T, K, S> resourceCoordinate = (ResourceCoordinate<T, K, S>) obj; if (!SharedResourcesBrokerUtils.isScopeTypeAncestor((ScopeType) currentScope.getType(), ((ResourceCoordinate) obj).getScope())) { throw new RuntimeException(String .format("%s returned an invalid coordinate: scope %s is not an ancestor of %s.", currentFactory.getName(), ((ResourceCoordinate) obj).getScope(), currentScope.getType()));
@Override public ScopeInstance<S> getScope(S scopeType) throws NoSuchScopeException { return getWrappedScope(scopeType).getScope(); }
@Override public void close() throws IOException { ScopeInstance<S> scope = this.selfScopeWrapper.getScope(); log.info("Closing broker with scope {} of id {}.", scope.getType().toString(), scope.getScopeId()); this.brokerCache.close(this.selfScopeWrapper); } }