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

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

i think it fits better without @Via - introducing a new Injector annotation 
which supports the requirements listed 
[here|https://issues.apache.org/jira/browse/SLING-7256?focusedCommentId=16738346#comment-16738346]

use case examples:
{code:java}
// inject annotation class mapped for caconfig
// resource is taken from current request or resource this model was adapted 
from
// uses ConfigurationBuilder#as(...)
@ContextAwareConfiguration
MyConfigClass config;

// inject a model or other adaptable that can be adapted from a config resource
// uses ConfigurationBuilder#asAdaptable(...)
@ContextAwareConfiguration
MyAdaptableClass config;

// inject annotation class mapped for caconfig
// uses ConfigurationBuilder#asValueMap()
@ContextAwareConfiguration(configName="myconfigname")
ValueMap configProps;

// inject the "low-level" configuration resource
// uses ConfigurationResourceResolver
@ContextAwareConfiguration(configName="myconfigname")
Resource configResource;
{code}

additionally it the annotation has to support *collections* of all variations 
listed above.

this does not include the use case from 
https://github.com/apache/sling-org-apache-sling-caconfig-api/pull/1 with 
direclty injecting into a primitive value. we could support this as well for 
the primitive data types like:

{code:java}
// inject a property value from a context-aware configuration value map
// uses ConfigurationBuilder#asValueMap()
@ContextAwareConfiguration(configName="myconfigname")
String prop1;
{code}

> 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
>            Assignee: Stefan Seifert
>            Priority: Major
>
> 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
(v7.6.14#76016)

Reply via email to