Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-7256

Sling Models injector for CAConfig

    XMLWordPrintableJSON

Details

    Description

      It would be great to have a Sling Models injector for CAConfig.

      An example could be:

      @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);
          }
      }
      

      + annotation:

      @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
      @Retention(RetentionPolicy.RUNTIME)
      @InjectAnnotation
      @Source("ca-config")
      @Qualifier
      public @interface CaConfig {
      }
      

      Attachments

        Issue Links

          Activity

            People

              sseifert Stefan Seifert
              hkui Henry Kuijpers
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: