SLING-6652 - allow for multiple classes to export the same resourceType
authorJustin Edelson <justin@apache.org>
Fri, 17 Mar 2017 13:56:20 +0000 (13:56 +0000)
committerJustin Edelson <justin@apache.org>
Fri, 17 Mar 2017 13:56:20 +0000 (13:56 +0000)
This closes #207

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1787388 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/ExportServlet.java
src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java

index ac528bd..6fceed2 100644 (file)
@@ -182,17 +182,33 @@ class ExportServlet extends SlingSafeMethodsServlet {
         String getExportedString(SlingHttpServletRequest request, Map<String, String> options, ModelFactory modelFactory, String exporterName) throws ExportException, MissingExporterException;
     }
 
-    public static final ExportedObjectAccessor RESOURCE_ACCESSOR = new ExportedObjectAccessor() {
+    public static final class ResourceAccessor implements ExportedObjectAccessor {
+
+        private final Class<?> adapterClass;
+
+        public ResourceAccessor(Class<?> adapterClass) {
+            this.adapterClass = adapterClass;
+        }
+
         @Override
         public String getExportedString(SlingHttpServletRequest request, Map<String, String> options, ModelFactory modelFactory, String exporterName) throws ExportException, MissingExporterException {
-            return modelFactory.exportModelForResource(request.getResource(), exporterName, String.class, options);
+            Object adapter = modelFactory.createModel(request.getResource(), adapterClass);
+            return modelFactory.exportModel(adapter, exporterName, String.class, options);
+        }
+    }
+
+    public static final class RequestAccessor implements ExportedObjectAccessor {
+
+        private final Class<?> adapterClass;
+
+        public RequestAccessor(Class<?> adapterClass) {
+            this.adapterClass = adapterClass;
         }
-    };
 
-    public static final ExportedObjectAccessor REQUEST_ACCESSOR = new ExportedObjectAccessor() {
         @Override
         public String getExportedString(SlingHttpServletRequest request, Map<String, String> options, ModelFactory modelFactory, String exporterName) throws ExportException, MissingExporterException {
-            return modelFactory.exportModelForRequest(request, exporterName, String.class, options);
+            Object adapter = modelFactory.createModel(request, adapterClass);
+            return modelFactory.exportModel(adapter, exporterName, String.class, options);
         }
     };
 
index 5e167de..ac6ef47 100644 (file)
@@ -162,9 +162,9 @@ public class ModelPackageBundleListener implements BundleTrackerCustomizer {
                                 adapterImplementations.registerModelToResourceType(bundle, resourceType, adaptable, implType);
                                 ExportServlet.ExportedObjectAccessor accessor = null;
                                 if (adaptable == Resource.class) {
-                                    accessor = ExportServlet.RESOURCE_ACCESSOR;
+                                    accessor = new ExportServlet.ResourceAccessor(implType);
                                 } else if (adaptable == SlingHttpServletRequest.class) {
-                                    accessor = ExportServlet.REQUEST_ACCESSOR;
+                                    accessor = new ExportServlet.RequestAccessor(implType);
                                 }
                                 Exporter exporterAnnotation = implType.getAnnotation(Exporter.class);
                                 if (exporterAnnotation != null) {