You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Stefan Seifert (JIRA)" <ji...@apache.org> on 2017/12/07 15:44:00 UTC

[jira] [Commented] (SLING-7256) Sling Models injector for CAConfig

    [ https://issues.apache.org/jira/browse/SLING-7256?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16282020#comment-16282020 ] 

Stefan Seifert commented on SLING-7256:
---------------------------------------

i think this is a good idea.

some thoughts:
* annotation should be named with full name e.g. ContextAwareConfiguration
* not only support "Class", but also the other types supportd by the ConfigurationResolver - ValueMap, Resource, and collections of all three types
* do not throw an exception if nether request nor resource is given
* we do not want to introduce a hard dependency to caconfig in sling models impl (or vice versa) - one solution would be a separate bundle that depends on both. but this would be overkill for just one selector. we might put it into sling models impl with an optional dependency to the models SPI. if this is not present the injector is not active, but all other models impl features still work.

> Sling Models injector for CAConfig
> ----------------------------------
>
>                 Key: SLING-7256
>                 URL: https://issues.apache.org/jira/browse/SLING-7256
>             Project: Sling
>          Issue Type: New Feature
>    Affects Versions: Sling Models Impl 1.4.4
>            Reporter: Henry Kuijpers
>
> It would be great to have a Sling Models injector for CAConfig.
> An example could be:
> {code}
> @Component
> public class ContextAwareConfigurationInjector implements Injector {
>     public String getName() {
>         return "ca-config";
>     }
>     public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element,
>                            DisposalCallbackRegistry callbackRegistry) {
>         if (isConfigurationObject(declaredType)) {
>             final Resource resource;
>             if (adaptable instanceof Resource) {
>                 resource = (Resource) adaptable;
>             } else if (adaptable instanceof SlingHttpServletRequest) {
>                 // TODO: Is this always the correct resource? (Most often we want the one in /content)
>                 // So we do not want /conf/... for example
>                 final SlingHttpServletRequest request = (SlingHttpServletRequest)adaptable;
>                 final ResourceResolver resourceResolver = request.getResourceResolver();
>                 resource = resourceResolver.resolve(request, request.getRequestURI());
>             } else {
>                 throw new IllegalArgumentException("Either a resource or the request should be used");
>             }
>             final ConfigurationBuilder builder = resource.adaptTo(ConfigurationBuilder.class);
>             if (builder != null) {
>                 return builder.as((Class<?>) declaredType);
>             }
>         }
>         return null;
>     }
>     private static boolean isConfigurationObject(Type type) {
>         if (!(type instanceof Class<?>)) {
>             return false;
>         }
>         Class<?> clazz = (Class<?>) type;
>         return clazz.isAnnotation() && clazz.isAnnotationPresent(Configuration.class);
>     }
> }
> {code}
> + annotation:
> {code}
> @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
> @Retention(RetentionPolicy.RUNTIME)
> @InjectAnnotation
> @Source("ca-config")
> @Qualifier
> public @interface CaConfig {
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)