/** * 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); }
/** * 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); }
/** * 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); }
/** * Convenience method to obtain the current {@link JwtConfiguration} 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 JwtConfiguration, empty if not available as context resource */ static Optional<JwtConfiguration> getCurrent() { return Context.get().resource(CONTEXT_KEY, JwtConfiguration.class); }
/** * 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); }
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[] }
/** * Shortcut method to obtain a {@link Context} resource using the <code>resourceType</code> class name as resource * key. * @param resourceType Resource type (not null) * @param <T> Resource type * @return Optional resource instance, empty if not available from context */ default <T> Optional<T> resource(Class<T> resourceType) { ObjectUtils.argumentNotNull(resourceType, "Context resource type must be not null"); return resource(resourceType.getName(), resourceType, null); }
/** * 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 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()); }
@Override public PropertyBox deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // get property set final PropertySet<?> propertySet = Context.get().resource(PropertySet.CONTEXT_KEY, PropertySet.class) .orElseThrow(() -> new JsonParseException("Missing PropertySet instance to build a PropertyBox. " + "A PropertySet instance must be available as context resource to perform PropertyBox deserialization.")); try { // deserialize as PropertyBox return deserializePropertyBox(context, json, propertySet); } catch (JsonDeserializationException e) { throw new JsonParseException("Failed to deserialize JSON element as a PropertyBox", e); } }
/** * 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()); }
/** * Gets the current {@link PropertyRendererRegistry} instance. * @return The {@link Context}-bound PropertyRendererRegistry instance, if available using {@link #CONTEXT_KEY} as * context key, or the default instance for the default ClassLoader obtained through {@link #getDefault()}. */ static PropertyRendererRegistry get() { return Context.get().resource(CONTEXT_KEY, PropertyRendererRegistry.class).orElse(getDefault()); }
/** * 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()); }
@Override public ReactiveRestClient create(ClassLoader classLoader) throws RestClientCreationException { Optional<WebClient> webClient = Context.get().resource("webClient", WebClient.class, classLoader); if (webClient.isPresent()) { return SpringReactiveRestClient.create(webClient.get()); } LOGGER.debug(() -> "No WebClient type Context resource available - ReactiveRestClient creation skipped"); return null; }
/** * Try to obtain a {@link Json} implementation using given Classloader, either from {@link Context}, if available * using {@link #CONTEXT_KEY}, or relying on registered {@link JsonProvider}s and using the one with higher * priority. * @param classLoader ClassLoader to use * @return The {@link Json} implementation, if available */ static Optional<Json> get(ClassLoader classLoader) { // check in context Optional<Json> fromContext = Context.get().resource(CONTEXT_KEY, Json.class, classLoader); if (fromContext.isPresent()) { return fromContext; } // use providers return JsonProviders.getDefaultJsonProvider(classLoader).map(provider -> provider.provide()); }
@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; }
if (!com.holonplatform.core.Context.get().resource(PropertySet.CONTEXT_KEY, PropertySet.class) .isPresent()) { PropertySetRef propertySetRef = PropertySetRefIntrospector.getPropertySetRef(annotations).orElse(null);
/** * Lookup a resource of given <code>resourceType</code> type, either using a suitable {@link ContextResolver} if * <code>providers</code> is not <code>null</code> and a {@link ContextResolver} for given <code>resourceType</code> * is available, or trying to obtain the resource from {@link Context} using given <code>resourceType</code> as * context resource key. * @param <R> Resource type * @param caller Caller class * @param resourceType Resource type to lookup (not null) * @param providers JAX-RS {@link Providers}, if available * @return Resource instance, or an empty optional if not available */ public static <R> Optional<R> lookupResource(Class<?> caller, Class<R> resourceType, Providers providers) { ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null"); R resource = null; // try to use a ContextResolver, if available ContextResolver<R> resolver = providers.getContextResolver(resourceType, null); if (resolver != null) { resource = resolver.getContext(caller); } if (resource == null) { // lookup in context resource = Context.get() .resource(resourceType, (caller != null) ? caller.getClassLoader() : ClassUtils.getDefaultClassLoader()) .orElse(null); } return Optional.ofNullable(resource); }
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[] }