Search in sources :

Example 1 with ImplementationPicker

use of org.apache.sling.models.spi.ImplementationPicker in project sling by apache.

the class AdapterImplementations method lookup.

/**
     * Lookup the best-matching implementation for the given adapter type by enquiring the {@link ImplementationPicker} services.
     * @param adapterType Adapter type
     * @param adaptable Adaptable for reference
     * @return Implementation type or null if none detected
     */
@SuppressWarnings("unchecked")
public <ModelType> ModelClass<ModelType> lookup(Class<ModelType> adapterType, Object adaptable) {
    String key = adapterType.getName();
    // lookup in cache for models without adapter classes
    ModelClass<ModelType> modelClass = (ModelClass<ModelType>) modelClasses.get(key);
    if (modelClass != null) {
        return modelClass;
    }
    // not found? look in cache with adapter classes
    ConcurrentNavigableMap<String, ModelClass<?>> implementations = adapterImplementations.get(key);
    if (implementations == null || implementations.isEmpty()) {
        return null;
    }
    Collection<ModelClass<?>> implementationsCollection = implementations.values();
    ModelClass<?>[] implementationWrappersArray = implementationsCollection.toArray(new ModelClass<?>[implementationsCollection.size()]);
    // prepare array for implementation picker
    Class<?>[] implementationsArray = new Class<?>[implementationsCollection.size()];
    for (int i = 0; i < implementationWrappersArray.length; i++) {
        implementationsArray[i] = implementationWrappersArray[i].getType();
    }
    for (ImplementationPicker picker : this.sortedImplementationPickers) {
        Class<?> implementation = picker.pick(adapterType, implementationsArray, adaptable);
        if (implementation != null) {
            for (int i = 0; i < implementationWrappersArray.length; i++) {
                if (implementation == implementationWrappersArray[i].getType()) {
                    return (ModelClass<ModelType>) implementationWrappersArray[i];
                }
            }
        }
    }
    return null;
}
Also used : ModelClass(org.apache.sling.models.impl.model.ModelClass) ModelClass(org.apache.sling.models.impl.model.ModelClass) ImplementationPicker(org.apache.sling.models.spi.ImplementationPicker)

Example 2 with ImplementationPicker

use of org.apache.sling.models.spi.ImplementationPicker 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

ImplementationPicker (org.apache.sling.models.spi.ImplementationPicker)2 Map (java.util.Map)1 Servlet (javax.servlet.Servlet)1 ViaProviderType (org.apache.sling.models.annotations.ViaProviderType)1 ModelClass (org.apache.sling.models.impl.model.ModelClass)1 Injector (org.apache.sling.models.spi.Injector)1 ViaProvider (org.apache.sling.models.spi.ViaProvider)1 InjectAnnotationProcessorFactory (org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory)1 InjectAnnotationProcessorFactory2 (org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2)1 StaticInjectAnnotationProcessorFactory (org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1