@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 <T, K extends SharedResourceKey> T getSharedResource(SharedResourceFactory<T, K, S> factory, K key) throws NotConfiguredException { try { return this.brokerCache.getAutoScoped(factory, key, this); } catch (ExecutionException ee) { Throwable cause = ee.getCause(); if (cause instanceof NotConfiguredException) { throw (NotConfiguredException) cause; } throw new RuntimeException(cause); } }
@Override public <T, K extends SharedResourceKey> T getSharedResourceAtScope(SharedResourceFactory<T, K, S> factory, K key, S scope) throws NotConfiguredException, NoSuchScopeException { try { return this.brokerCache.getScoped(factory, key, getWrappedScope(scope), this); } catch (ExecutionException ee) { throw new RuntimeException(ee); } }
ScopeWrapper<S> currentScope = scope; Object obj = getScopedFromCache(currentFactory, currentKey, currentScope, broker); obj = getScopedFromCache(resourceCoordinate.getFactory(), resourceCoordinate.getKey(), broker.getWrappedScope(resourceCoordinate.getScope()), broker); } catch (NoSuchScopeException nsse) { lock.lock(); RawJobBrokerKey fullKey = new RawJobBrokerKey(currentScope, currentFactory.getName(), currentKey); safeInvalidate(fullKey); obj = getScopedFromCache(currentFactory, currentKey, currentScope, broker); } finally { lock.unlock();
@Override public <T, K extends SharedResourceKey> void bindSharedResourceAtScope(SharedResourceFactory<T, K, S> factory, K key, S scopeType, T instance) throws NoSuchScopeException { this.brokerCache.put(factory, key, getWrappedScope(scopeType), instance); }
/** * 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)); }
ScopeWrapper<S> currentScope = scope; Object obj = getScopedFromCache(currentFactory, currentKey, currentScope, broker); obj = getScopedFromCache(resourceCoordinate.getFactory(), resourceCoordinate.getKey(), broker.getWrappedScope(resourceCoordinate.getScope()), broker); } catch (NoSuchScopeException nsse) { lock.lock(); RawJobBrokerKey fullKey = new RawJobBrokerKey(currentScope, currentFactory.getName(), currentKey); safeInvalidate(fullKey); obj = getScopedFromCache(currentFactory, currentKey, currentScope, broker); } finally { lock.unlock();
@Override public <T, K extends SharedResourceKey> void bindSharedResourceAtScope(SharedResourceFactory<T, K, S> factory, K key, S scopeType, T instance) throws NoSuchScopeException { this.brokerCache.put(factory, key, getWrappedScope(scopeType), instance); }
/** * 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)); }
/** * Get an object for the specified factory, key, and broker at the scope selected by the factory. {@link DefaultBrokerCache} * guarantees that calling this method from brokers with the same leaf scope will return the same object. */ @SuppressWarnings(value = "unchecked") <T, K extends SharedResourceKey> T getAutoScoped(final SharedResourceFactory<T, K, S> factory, final K key, final SharedResourcesBrokerImpl<S> broker) throws ExecutionException { // figure out auto scope RawJobBrokerKey autoscopeCacheKey = new RawJobBrokerKey(broker.getWrappedSelfScope(), factory.getName(), key); ScopeWrapper<S> selectedScope = this.autoScopeCache.get(autoscopeCacheKey, new Callable<ScopeWrapper<S>>() { @Override public ScopeWrapper<S> call() throws Exception { return broker.getWrappedScope(factory.getAutoScope(broker, broker.getConfigView(null, key, factory.getName()))); } }); // get actual object return getScoped(factory, key, selectedScope, broker); }
@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 <T, K extends SharedResourceKey> T getSharedResource(SharedResourceFactory<T, K, S> factory, K key) throws NotConfiguredException { try { return this.brokerCache.getAutoScoped(factory, key, this); } catch (ExecutionException ee) { Throwable cause = ee.getCause(); if (cause instanceof NotConfiguredException) { throw (NotConfiguredException) cause; } throw new RuntimeException(cause); } }
@Override public <T, K extends SharedResourceKey> T getSharedResourceAtScope(SharedResourceFactory<T, K, S> factory, K key, S scope) throws NotConfiguredException, NoSuchScopeException { try { return this.brokerCache.getScoped(factory, key, getWrappedScope(scope), this); } catch (ExecutionException ee) { throw new RuntimeException(ee); } }
/** * Get an object for the specified factory, key, and broker at the scope selected by the factory. {@link DefaultBrokerCache} * guarantees that calling this method from brokers with the same leaf scope will return the same object. */ @SuppressWarnings(value = "unchecked") <T, K extends SharedResourceKey> T getAutoScoped(final SharedResourceFactory<T, K, S> factory, final K key, final SharedResourcesBrokerImpl<S> broker) throws ExecutionException { // figure out auto scope RawJobBrokerKey autoscopeCacheKey = new RawJobBrokerKey(broker.getWrappedSelfScope(), factory.getName(), key); ScopeWrapper<S> selectedScope = this.autoScopeCache.get(autoscopeCacheKey, new Callable<ScopeWrapper<S>>() { @Override public ScopeWrapper<S> call() throws Exception { return broker.getWrappedScope(factory.getAutoScope(broker, broker.getConfigView(null, key, factory.getName()))); } }); // get actual object return getScoped(factory, key, selectedScope, broker); }