public void getResource() { ClassLoader aClassLoader = ClassLoader.getSystemClassLoader(); // tag::get[] Optional<ResourceType> resource = Context.get().resource("resourceKey", ResourceType.class); // <1> resource = Context.get().resource("resourceKey", ResourceType.class, aClassLoader); // <2> resource = Context.get().resource(ResourceType.class); // <3> // end::get[] }
/** * 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); }
/** * 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[] }
/** * 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); }
/** * Get the Vaadin Session scope from current context. * @return The Vaadin Session scope, if available. */ static Optional<ContextScope> get() { return Context.get().scope(NAME); }
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[] }
/** * Get the current {@link ViewNavigator}, if available as {@link Context} resource or from current UI. * @return Current ViewNavigator, or an empty Optinal if not available */ static Optional<ViewNavigator> getCurrent() { return Optional.ofNullable(Context.get().resource(ViewNavigator.CONTEXT_KEY, ViewNavigator.class) .orElse(ViewNavigationUtils.getCurrentUIViewNavigator())); }
/** * Gets the current {@link BeanIntrospector} instance. * @return The {@link Context}-bound BeanIntrospector instance, if available using {@link #CONTEXT_KEY} as context * key, or the default instance for the default ClassLoader obtained through {@link #getDefault()}. */ static BeanIntrospector get() { return Context.get().resource(CONTEXT_KEY, BeanIntrospector.class).orElse(getDefault()); }
/** * Gets the current {@link PropertyValuePresenterRegistry} instance. * @return The {@link Context}-bound PropertyValuePresenterRegistry instance, if available using * {@link #CONTEXT_KEY} as context key, or the default instance for the default ClassLoader obtained through * {@link #getDefault()}. */ static PropertyValuePresenterRegistry get() { return Context.get().resource(CONTEXT_KEY, PropertyValuePresenterRegistry.class).orElse(getDefault()); }
/** * Gets the current {@link PropertySetRefIntrospector} instance. * @return The {@link Context}-bound PropertySetRefIntrospector instance, if available using {@link #CONTEXT_KEY} as * context key, or the default instance for the default ClassLoader obtained through {@link #getDefault()}. */ static PropertySetRefIntrospector get() { return Context.get().resource(CONTEXT_KEY, PropertySetRefIntrospector.class).orElse(getDefault()); }
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[] }
public void registerResource() { // tag::register[] Context.get().scope("myscope") // <1> .ifPresent(scope -> scope.put("myResourceKey", new ResourceType())); // <2> // end::register[] }
@Override public RestClient create(ClassLoader classLoader) throws RestClientCreationException { // Try to obtain a RestTemplate Optional<RestTemplateBuilder> restTemplateBuilder = Context.get().resource("restTemplateBuilder", RestTemplateBuilder.class, classLoader); if (restTemplateBuilder.isPresent()) { return new RestTemplateRestClient(restTemplateBuilder.get().build()); } LOGGER.debug(() -> "No RestTemplateBuilder type Context resource available - RestClient creation skipped"); return null; }
public void scope1() { // tag::scope1[] Optional<ContextScope> scope = Context.get().scope(VaadinSessionScope.NAME); // <1> scope = VaadinSessionScope.get(); // <2> ContextScope sessionScope = VaadinSessionScope.require(); // <3> // end::scope1[] }
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[] }
public void contextResourceRealm() { // tag::realm2[] Context.get().classLoaderScope() // <1> .map(s -> s.put(Realm.CONTEXT_KEY, Realm.builder().withResolver(AuthenticationToken.httpBasicResolver()) .withAuthenticator(Account.authenticator(getAccountProvider())).withDefaultAuthorizer() .build())); // end::realm2[] }
public void scope2() { // tag::scope2[] VaadinSession.getCurrent().setAttribute(LocalizationContext.class, // <1> LocalizationContext.builder().withInitialLocale(Locale.US).build()); Optional<LocalizationContext> localizationContext = Context.get().resource(LocalizationContext.class); // <2> localizationContext = LocalizationContext.getCurrent(); // <3> // end::scope2[] }