/** * Convenience method to obtain the current {@link TenantResolver} made available as {@link Context} resource, using * default {@link ClassLoader}. * <p> * See {@link Context#resource(String, Class)} for details about context resources availability conditions. * </p> * @return Optional TenantResolver, empty if not available as context resource */ static Optional<TenantResolver> getCurrent() { return Context.get().resource(CONTEXT_KEY, TenantResolver.class); }
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 this PropertySet, i.e. binding this * PropertySet as {@link Context} resource to current Thread, and removing the binding after operation execution. * @param <V> Operation result type * @param operation Operation to execute * @return Operation result */ default <V> V execute(final Callable<V> operation) { return Context.get().executeThreadBound(CONTEXT_KEY, this, operation); }
public void objectMapperResource() { // tag::jaxrsor2[] final ObjectMapper mapper = JacksonConfiguration.mapper(); // additional ObjectMapper configuration // ... Context.get().classLoaderScope().map(s -> s.put(ObjectMapper.class.getName(), mapper)); // <1> // end::jaxrsor2[] }
/** * 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)); } }
public void getContext() { // tag::getctx[] Context currentContext = Context.get(); // <1> // end::getctx[] }
/** * Lookup a context resource of <code>resourceType</code> class using given <code>resourceKey</code> and the default * {@link ClassLoader}. * <p> * All registered context scopes are invoked, in the order defined by {@link ContextScope#getOrder()} precedence * value. The first not <code>null</code> resource instance obtained from a scope is the one returned to caller, if * any. * </p> * @param resourceKey Resource key (not null) * @param resourceType Required resource type (not null) * @param <T> Resource type * @return Optional resource instance, empty if not available from context * @throws TypeMismatchException If a context resource with given key was found but it is not of required type */ default <T> Optional<T> resource(String resourceKey, Class<T> resourceType) { return resource(resourceKey, resourceType, null); }
/** * Gets a registered {@link ContextScope} with the given <code>name</code> and bound to default {@link ClassLoader}. * @param name Scope name (not null) * @return The registered {@link ContextScope} with given name, or an empty Optional if a scope with such name is * not registered in context */ default Optional<ContextScope> scope(String name) { return scope(name, null); }
/** * Shortcut method to obtain the ClassLoader-bound context scope, i.e. the default {@link ContextScope} named * {@link #CLASSLOADER_SCOPE_NAME}, using the default {@link ClassLoader}. * @return The ClassLoader-bound context scope, or an empty Optional if scope is not registered */ default Optional<ContextScope> classLoaderScope() { return classLoaderScope(null); }
/** * 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 gsonResource() { // tag::jaxrsor2[] GsonBuilder builder = GsonConfiguration.builder(); // additional GsonBuilder configuration // ... final Gson gson = builder.create(); Context.get().classLoaderScope().map(s -> s.put(Gson.class.getName(), gson)); // <1> // end::jaxrsor2[] }
/** * 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)); } }
public void threadScope() { Object resourceInstance = new String(); // tag::threadscope[] Context.get().executeThreadBound("resourceKey", resourceInstance, () -> { // do something // <1> }); Context.get().executeThreadBound("resourceKey", resourceInstance, () -> { // do something // <2> return null; }); // end::threadscope[] }
/** * Shortcut method to obtain a {@link Context} resource using the <code>resourceType</code> class name as resource * key and given <code>classLoader</code>. * @param resourceType Resource type (not null) * @param classLoader ClassLoader to use. If <code>null</code>, the default ClassLoader is used. * @param <T> Resource type * @return Optional resource instance, empty if not available from context */ default <T> Optional<T> resource(Class<T> resourceType, ClassLoader classLoader) { ObjectUtils.argumentNotNull(resourceType, "Context resource type must be not null"); return resource(resourceType.getName(), resourceType, classLoader); }
/** * Shortcut method to obtain the thread-bound context scope, i.e. the default {@link ContextScope} named * {@link #THREAD_SCOPE_NAME}. * @param classLoader ClassLoader, if <code>null</code>, the default ClassLoader is used * @return The thread-bound context scope, or an empty Optional if scope is not registered */ default Optional<ContextScope> threadScope(ClassLoader classLoader) { return scope(THREAD_SCOPE_NAME, classLoader); }
/** * 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)); } }
/** * Convenience method to obtain the current {@link LocalizationContext} made available as {@link Context} resource, * using default {@link ClassLoader}. * <p> * See {@link Context#resource(String, Class)} for details about context resources availability conditions. * </p> * @return Optional LocalizationContext, empty if not available as context resource */ static Optional<LocalizationContext> getCurrent() { return Context.get().resource(CONTEXT_KEY, LocalizationContext.class); }
public void context() { // tag::context[] final Realm realm = Realm.builder().build(); Context.get().classLoaderScope().map(scope -> scope.put(Realm.CONTEXT_KEY, realm)); // <1> Optional<Realm> currentRealm = Realm.getCurrent(); // <2> Realm requiredRealm = Realm.require(); // <3> // end::context[] }