Search in sources :

Example 1 with Exporter

use of org.apache.sling.models.annotations.Exporter in project sling by apache.

the class ModelPackageBundleListener method analyzeClass.

private void analyzeClass(Bundle bundle, String className, List<ServiceRegistration> regs) {
    try {
        Class<?> implType = bundle.loadClass(className);
        Model annotation = implType.getAnnotation(Model.class);
        if (annotation != null) {
            // get list of adapters from annotation - if not given use annotated class itself
            Class<?>[] adapterTypes = annotation.adapters();
            if (adapterTypes.length == 0) {
                adapterTypes = new Class<?>[] { implType };
            } else if (!ArrayUtils.contains(adapterTypes, implType)) {
                adapterTypes = (Class<?>[]) ArrayUtils.add(adapterTypes, implType);
            }
            // register adapter only if given adapters are valid
            if (validateAdapterClasses(implType, adapterTypes)) {
                if (adapterImplementations.addAll(implType, adapterTypes)) {
                    ServiceRegistration reg = registerAdapterFactory(adapterTypes, annotation.adaptables(), implType, annotation.condition());
                    regs.add(reg);
                    String[] resourceTypes = annotation.resourceType();
                    for (String resourceType : resourceTypes) {
                        if (StringUtils.isNotEmpty(resourceType)) {
                            for (Class<?> adaptable : annotation.adaptables()) {
                                adapterImplementations.registerModelToResourceType(bundle, resourceType, adaptable, implType);
                                ExportServlet.ExportedObjectAccessor accessor = null;
                                if (adaptable == Resource.class) {
                                    accessor = new ExportServlet.ResourceAccessor(implType);
                                } else if (adaptable == SlingHttpServletRequest.class) {
                                    accessor = new ExportServlet.RequestAccessor(implType);
                                }
                                Exporter exporterAnnotation = implType.getAnnotation(Exporter.class);
                                if (exporterAnnotation != null) {
                                    registerExporter(bundle, implType, resourceType, exporterAnnotation, regs, accessor);
                                }
                                Exporters exportersAnnotation = implType.getAnnotation(Exporters.class);
                                if (exportersAnnotation != null) {
                                    for (Exporter ann : exportersAnnotation.value()) {
                                        registerExporter(bundle, implType, resourceType, ann, regs, accessor);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (ClassNotFoundException e) {
        log.warn("Unable to load class", e);
    }
}
Also used : Exporter(org.apache.sling.models.annotations.Exporter) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Exporters(org.apache.sling.models.annotations.Exporters) Model(org.apache.sling.models.annotations.Model) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 Exporter (org.apache.sling.models.annotations.Exporter)1 Exporters (org.apache.sling.models.annotations.Exporters)1 Model (org.apache.sling.models.annotations.Model)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1