Search in sources :

Example 1 with InjectAnnotationProcessorFactory2

use of org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2 in project sling by apache.

the class ModelAdapterFactory method injectElement.

@CheckForNull
private RuntimeException injectElement(final InjectableElement element, final Object adaptable, @Nonnull final DisposalCallbackRegistry registry, final InjectCallback callback, @Nonnull final Map<ValuePreparer, Object> preparedValues) {
    InjectAnnotationProcessor annotationProcessor = null;
    String source = element.getSource();
    boolean wasInjectionSuccessful = false;
    // find an appropriate annotation processor
    for (InjectAnnotationProcessorFactory2 factory : injectAnnotationProcessorFactories2) {
        annotationProcessor = factory.createAnnotationProcessor(adaptable, element.getAnnotatedElement());
        if (annotationProcessor != null) {
            break;
        }
    }
    if (annotationProcessor == null) {
        for (InjectAnnotationProcessorFactory factory : injectAnnotationProcessorFactories) {
            annotationProcessor = factory.createAnnotationProcessor(adaptable, element.getAnnotatedElement());
            if (annotationProcessor != null) {
                break;
            }
        }
    }
    String name = getName(element, annotationProcessor);
    final Object injectionAdaptable = getAdaptable(adaptable, element, annotationProcessor);
    RuntimeException lastInjectionException = null;
    if (injectionAdaptable != null) {
        // prepare the set of injectors to process. if a source is given only use injectors with this name.
        final RankedServices<Injector> injectorsToProcess;
        if (StringUtils.isEmpty(source)) {
            injectorsToProcess = sortedInjectors;
        } else {
            injectorsToProcess = injectors.get(source);
            if (injectorsToProcess == null) {
                throw new IllegalArgumentException("No Sling Models Injector registered for source '" + source + "'.");
            }
        }
        // find the right injector
        for (Injector injector : injectorsToProcess) {
            if (name != null || injector instanceof AcceptsNullName) {
                Object preparedValue = injectionAdaptable;
                // only do the ValuePreparer optimization for the original adaptable
                if (injector instanceof ValuePreparer && adaptable == injectionAdaptable) {
                    final ValuePreparer preparer = (ValuePreparer) injector;
                    Object fromMap = preparedValues.get(preparer);
                    if (fromMap != null) {
                        preparedValue = fromMap;
                    } else {
                        preparedValue = preparer.prepareValue(injectionAdaptable);
                        preparedValues.put(preparer, preparedValue);
                    }
                }
                Object value = injector.getValue(preparedValue, name, element.getType(), element.getAnnotatedElement(), registry);
                if (value != null) {
                    lastInjectionException = callback.inject(element, value);
                    if (lastInjectionException == null) {
                        wasInjectionSuccessful = true;
                        break;
                    }
                }
            }
        }
    }
    // if injection failed, use default
    if (!wasInjectionSuccessful) {
        Result<Boolean> defaultInjectionResult = injectDefaultValue(element, annotationProcessor, callback);
        if (defaultInjectionResult.wasSuccessful()) {
            wasInjectionSuccessful = defaultInjectionResult.getValue();
            // log previous injection error, if there was any
            if (lastInjectionException != null && wasInjectionSuccessful) {
                log.debug("Although falling back to default value worked, injection into {} failed because of: " + lastInjectionException.getMessage(), element.getAnnotatedElement(), lastInjectionException);
            }
        } else {
            return defaultInjectionResult.getThrowable();
        }
    }
    // if default is not set, check if mandatory
    if (!wasInjectionSuccessful) {
        if (element.isOptional(annotationProcessor)) {
            // log previous injection error, if there was any
            if (lastInjectionException != null) {
                log.debug("Injection into optional element {} failed because of: " + lastInjectionException.getMessage(), element.getAnnotatedElement(), lastInjectionException);
            }
            if (element.isPrimitive()) {
                RuntimeException throwable = injectPrimitiveInitialValue(element, callback);
                if (throwable != null) {
                    return throwable;
                }
            }
        } else {
            if (lastInjectionException != null) {
                return lastInjectionException;
            } else {
                return new ModelClassException("No injector returned a non-null value!");
            }
        }
    }
    return null;
}
Also used : InjectAnnotationProcessorFactory2(org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2) ValuePreparer(org.apache.sling.models.spi.ValuePreparer) InjectAnnotationProcessor(org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessor) Injector(org.apache.sling.models.spi.Injector) ModelClassException(org.apache.sling.models.factory.ModelClassException) AcceptsNullName(org.apache.sling.models.spi.AcceptsNullName) InjectAnnotationProcessorFactory(org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory) StaticInjectAnnotationProcessorFactory(org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory) CheckForNull(javax.annotation.CheckForNull)

Example 2 with InjectAnnotationProcessorFactory2

use of org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2 in project sling by apache.

the class ModelConfigurationPrinter method printConfiguration.

public void printConfiguration(PrintWriter printWriter) {
    // injectors
    printWriter.println("Sling Models Injectors:");
    Collection<Injector> injectors = modelAdapterFactory.getInjectors();
    if (injectors.isEmpty()) {
        printWriter.println("none");
    } else {
        for (Injector injector : injectors) {
            printWriter.printf("%s - %s", injector.getName(), injector.getClass().getName());
            printWriter.println();
        }
    }
    printWriter.println();
    // inject annotations processor factories
    printWriter.println("Sling Models Inject Annotation Processor Factories:");
    Collection<InjectAnnotationProcessorFactory> factories = modelAdapterFactory.getInjectAnnotationProcessorFactories();
    Collection<InjectAnnotationProcessorFactory2> factories2 = modelAdapterFactory.getInjectAnnotationProcessorFactories2();
    Collection<StaticInjectAnnotationProcessorFactory> staticFactories = modelAdapterFactory.getStaticInjectAnnotationProcessorFactories();
    if ((factories.isEmpty()) && (factories2.isEmpty()) && (staticFactories.isEmpty())) {
        printWriter.println("none");
    } else {
        for (StaticInjectAnnotationProcessorFactory factory : staticFactories) {
            printWriter.printf("%s", factory.getClass().getName());
            printWriter.println();
        }
        for (InjectAnnotationProcessorFactory2 factory : factories2) {
            printWriter.printf("%s", factory.getClass().getName());
            printWriter.println();
        }
        for (InjectAnnotationProcessorFactory factory : factories) {
            printWriter.printf("%s", factory.getClass().getName());
            printWriter.println();
        }
    }
    printWriter.println();
    // implementation pickers
    printWriter.println("Sling Models Implementation Pickers:");
    ImplementationPicker[] pickers = modelAdapterFactory.getImplementationPickers();
    if (pickers == null || pickers.length == 0) {
        printWriter.println("none");
    } else {
        for (ImplementationPicker picker : pickers) {
            printWriter.printf("%s", picker.getClass().getName());
            printWriter.println();
        }
    }
    printWriter.println();
    // implementation pickers
    printWriter.println("Sling Models Via Providers:");
    Map<Class<? extends ViaProviderType>, ViaProvider> viaProviders = modelAdapterFactory.getViaProviders();
    if (viaProviders == null || viaProviders.size() == 0) {
        printWriter.println("none");
    } else {
        for (Map.Entry<Class<? extends ViaProviderType>, ViaProvider> entry : viaProviders.entrySet()) {
            printWriter.printf("%s (Type: %s)", entry.getValue().getClass().getName(), entry.getKey().getName());
            printWriter.println();
        }
    }
    printWriter.println();
    // models bound to resource types
    printWriter.println("Sling Models Bound to Resource Types *For Resources*:");
    for (Map.Entry<String, Class<?>> entry : adapterImplementations.getResourceTypeMappingsForResources().entrySet()) {
        printWriter.print(entry.getValue().getName());
        printWriter.print(" - ");
        printWriter.println(entry.getKey());
    }
    printWriter.println();
    printWriter.println("Sling Models Bound to Resource Types *For Requests*:");
    for (Map.Entry<String, Class<?>> entry : adapterImplementations.getResourceTypeMappingsForRequests().entrySet()) {
        printWriter.print(entry.getValue().getName());
        printWriter.print(" - ");
        printWriter.println(entry.getKey());
    }
    printWriter.println();
    // registered exporter servlets
    printWriter.println("Sling Models Exporter Servlets:");
    try {
        ServiceReference[] servlets = bundleContext.getServiceReferences(Servlet.class.getName(), EXPORT_SERVLET_FILTER);
        if (servlets != null) {
            for (ServiceReference ref : servlets) {
                printWriter.print(ref.getProperty(ModelPackageBundleListener.PROP_EXPORTER_SERVLET_CLASS));
                printWriter.print(" exports '");
                printWriter.print(ref.getProperty("sling.servlet.resourceTypes"));
                printWriter.print("' with selector '");
                printWriter.print(ref.getProperty("sling.servlet.selectors"));
                printWriter.print("' and extension '");
                printWriter.print(ref.getProperty("sling.servlet.extensions"));
                printWriter.print("' with exporter '");
                printWriter.print(ref.getProperty(ModelPackageBundleListener.PROP_EXPORTER_SERVLET_NAME));
                printWriter.println("'");
            }
        }
    } catch (InvalidSyntaxException e) {
    // ignore
    }
}
Also used : ViaProvider(org.apache.sling.models.spi.ViaProvider) InjectAnnotationProcessorFactory2(org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2) ServiceReference(org.osgi.framework.ServiceReference) Injector(org.apache.sling.models.spi.Injector) Servlet(javax.servlet.Servlet) ViaProviderType(org.apache.sling.models.annotations.ViaProviderType) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InjectAnnotationProcessorFactory(org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory) StaticInjectAnnotationProcessorFactory(org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory) ImplementationPicker(org.apache.sling.models.spi.ImplementationPicker) Map(java.util.Map) StaticInjectAnnotationProcessorFactory(org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory)

Aggregations

Injector (org.apache.sling.models.spi.Injector)2 InjectAnnotationProcessorFactory (org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory)2 InjectAnnotationProcessorFactory2 (org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2)2 StaticInjectAnnotationProcessorFactory (org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory)2 Map (java.util.Map)1 CheckForNull (javax.annotation.CheckForNull)1 Servlet (javax.servlet.Servlet)1 ViaProviderType (org.apache.sling.models.annotations.ViaProviderType)1 ModelClassException (org.apache.sling.models.factory.ModelClassException)1 AcceptsNullName (org.apache.sling.models.spi.AcceptsNullName)1 ImplementationPicker (org.apache.sling.models.spi.ImplementationPicker)1 ValuePreparer (org.apache.sling.models.spi.ValuePreparer)1 ViaProvider (org.apache.sling.models.spi.ViaProvider)1 InjectAnnotationProcessor (org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessor)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1