/** * Shortcut method to obtain the thread-bound context scope, i.e. the default {@link ContextScope} named * {@link #THREAD_SCOPE_NAME}, using the default {@link ClassLoader}. * @return The thread-bound context scope, or an empty Optional if scope is not registered */ default Optional<ContextScope> threadScope() { return threadScope(null); }
public void defaultContextScopes() { ClassLoader aClassLoader = ClassLoader.getSystemClassLoader(); Object resourceInstance = new String(); // tag::dftscopes[] Optional<ContextScope> scope = Context.get().classLoaderScope(); // <1> scope = Context.get().classLoaderScope(aClassLoader); // <2> Optional<ContextScope> threadScope = Context.get().threadScope(); // <3> threadScope = Context.get().threadScope(aClassLoader); // <4> // end::dftscopes[] }
/** * Execute given {@link Callable} <code>operation</code> on behalf of given <code>tenantId</code>, binding to * current thread a static {@link TenantResolver} context resource with default {@link TenantResolver#CONTEXT_KEY} * key providing the specified tenant id. * @param <V> Operation result type * @param tenantId Tenant id * @param operation Operation to execute * @return Operation result * @throws RuntimeException Exception during operation execution */ static <V> V execute(final String tenantId, final Callable<V> operation) throws RuntimeException { ObjectUtils.argumentNotNull(operation, "Runnable operation must be not null"); try { Context.get().threadScope() .map(s -> s.put(TenantResolver.CONTEXT_KEY, TenantResolver.staticTenantResolver(tenantId))); return operation.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { Context.get().threadScope().map(s -> s.remove(TenantResolver.CONTEXT_KEY)); } }
/** * Execute given {@link Callable} <code>operation</code>, binding given {@link Context} resource key and value to * current Thread, and removing the binding after operation execution. * <p> * The {@link #THREAD_SCOPE_NAME} context scope is used. * </p> * @param resourceKey Context resource key (not null) * @param resource Context resource value to bind to current Thread (not null) * @param operation Operation to execute * @param <V> Operation result type * @return Operation result * @throws RuntimeException Exception during operation execution */ default <V> V executeThreadBound(final String resourceKey, final Object resource, final Callable<V> operation) throws RuntimeException { ObjectUtils.argumentNotNull(resourceKey, "Resource key must be not null"); ObjectUtils.argumentNotNull(resource, "Resource value must be not null"); ObjectUtils.argumentNotNull(operation, "Runnable operation must be not null"); try { threadScope().map((s) -> s.put(resourceKey, resource)); return operation.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { threadScope().map((s) -> s.remove(resourceKey)); } }
/** * Execute given {@link Runnable} <code>operation</code>, binding given {@link Context} resource key and value to * current Thread, and removing the binding after operation execution. * <p> * The {@link #THREAD_SCOPE_NAME} context scope is used. * </p> * @param resourceKey Context resource key (not null) * @param resource Context resource value to bind to current Thread (not null) * @param operation Operation to execute (not null) * @throws RuntimeException Exception during operation execution */ default void executeThreadBound(final String resourceKey, final Object resource, final Runnable operation) throws RuntimeException { ObjectUtils.argumentNotNull(resourceKey, "Resource key must be not null"); ObjectUtils.argumentNotNull(resource, "Resource value must be not null"); ObjectUtils.argumentNotNull(operation, "Runnable operation must be not null"); try { threadScope().map((s) -> s.put(resourceKey, resource)); operation.run(); } catch (Exception e) { throw new RuntimeException(e); } finally { threadScope().map((s) -> s.remove(resourceKey)); } }
/** * Execute given {@link Runnable} <code>operation</code> on behalf of given <code>tenantId</code>, binding to * current thread a static {@link TenantResolver} context resource with default {@link TenantResolver#CONTEXT_KEY} * key providing the specified tenant id. * @param tenantId Tenant id * @param operation Operation to execute (not null) * @throws RuntimeException Exception during operation execution */ static void execute(final String tenantId, final Runnable operation) throws RuntimeException { ObjectUtils.argumentNotNull(operation, "Runnable operation must be not null"); try { Context.get().threadScope() .map(s -> s.put(TenantResolver.CONTEXT_KEY, TenantResolver.staticTenantResolver(tenantId))); operation.run(); } catch (Exception e) { throw new RuntimeException(e); } finally { Context.get().threadScope().map(s -> s.remove(TenantResolver.CONTEXT_KEY)); } }
final Optional<ViewNavigator> previous = Context.get().threadScope() .flatMap(s -> s.get(ViewNavigator.CONTEXT_KEY, ViewNavigator.class)); try { Context.get().threadScope().map(s -> s.put(ViewNavigator.CONTEXT_KEY, navigator)); Context.get().threadScope().map(s -> s.remove(ViewNavigator.CONTEXT_KEY)); previous.ifPresent((n) -> Context.get().threadScope().map(s -> s.put(ViewNavigator.CONTEXT_KEY, n)));