SLING-6366 - add new data to Sling Models Configuration Printer
authorJustin Edelson <justin@apache.org>
Mon, 5 Dec 2016 19:10:36 +0000 (19:10 +0000)
committerJustin Edelson <justin@apache.org>
Mon, 5 Dec 2016 19:10:36 +0000 (19:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1772744 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/AdapterImplementations.java
src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
src/main/java/org/apache/sling/models/impl/ModelConfigurationPrinter.java
src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java

index 43b4c74..6a13f35 100644 (file)
@@ -19,6 +19,7 @@
 package org.apache.sling.models.impl;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -329,4 +330,12 @@ final class AdapterImplementations {
         }
         return modelClass;
     }
+
+    Map<String, Class<?>> getResourceTypeMappingsForRequests() {
+        return Collections.unmodifiableMap(resourceTypeMappingsForRequests);
+    }
+
+    Map<String, Class<?>> getResourceTypeMappingsForResources() {
+        return Collections.unmodifiableMap(resourceTypeMappingsForResources);
+    }
 }
index 8d562f7..9582daa 100644 (file)
@@ -975,7 +975,7 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto
         printerProps.put("felix.webconsole.configprinter.modes", "always");
 
         this.configPrinterRegistration = bundleContext.registerService(Object.class.getName(),
-                new ModelConfigurationPrinter(this), printerProps);
+                new ModelConfigurationPrinter(this, bundleContext, adapterImplementations), printerProps);
     }
 
     @Deactivate
index 7d812f5..30fa93d 100644 (file)
@@ -18,20 +18,32 @@ package org.apache.sling.models.impl;
 
 import java.io.PrintWriter;
 import java.util.Collection;
+import java.util.Map;
 
 import org.apache.sling.models.spi.ImplementationPicker;
 import org.apache.sling.models.spi.Injector;
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory;
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory2;
 import org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+import javax.servlet.Servlet;
 
 @SuppressWarnings("deprecation")
 public class ModelConfigurationPrinter {
 
+    private static final String EXPORT_SERVLET_FILTER = "(" + ModelPackageBundleListener.PROP_EXPORTER_SERVLET_CLASS + "=*)";
+
     private final ModelAdapterFactory modelAdapterFactory;
+    private final BundleContext bundleContext;
+    private final AdapterImplementations adapterImplementations;
 
-    ModelConfigurationPrinter(ModelAdapterFactory modelAdapterFactory) {
+    ModelConfigurationPrinter(ModelAdapterFactory modelAdapterFactory, BundleContext bundleContext, AdapterImplementations adapterImplementations) {
         this.modelAdapterFactory = modelAdapterFactory;
+        this.bundleContext = bundleContext;
+        this.adapterImplementations = adapterImplementations;
     }
 
     public void printConfiguration(PrintWriter printWriter) {
@@ -85,6 +97,48 @@ public class ModelConfigurationPrinter {
                 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
+        }
     }
 
 }
\ No newline at end of file
index 02058d4..c3404fc 100644 (file)
@@ -53,6 +53,9 @@ public class ModelPackageBundleListener implements BundleTrackerCustomizer {
 
     static final String PACKAGE_HEADER = "Sling-Model-Packages";
     static final String CLASSES_HEADER = "Sling-Model-Classes";
+
+    static final String PROP_EXPORTER_SERVLET_CLASS = "sling.models.exporter.servlet.class";
+    static final String PROP_EXPORTER_SERVLET_NAME = "sling.models.exporter.servlet.name";
     
     /**
      * Service registration property for the adapter condition.
@@ -274,6 +277,8 @@ public class ModelPackageBundleListener implements BundleTrackerCustomizer {
             registrationProps.put("sling.servlet.resourceTypes", resourceType);
             registrationProps.put("sling.servlet.selectors", exporterAnnotation.selector());
             registrationProps.put("sling.servlet.extensions", exporterAnnotation.extensions());
+            registrationProps.put(PROP_EXPORTER_SERVLET_CLASS, annotatedClass.getName());
+            registrationProps.put(PROP_EXPORTER_SERVLET_NAME, exporterAnnotation.name());
 
             log.info("registering servlet for {}, {}, {}", new Object[]{resourceType, exporterAnnotation.selector(), exporterAnnotation.extensions()});