You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2016/11/17 10:19:26 UTC

svn commit: r1770147 - in /sling/trunk/bundles/extensions/models: impl/src/main/java/org/apache/sling/models/impl/ integration-tests/src/main/java/org/apache/sling/models/it/exporter/

Author: justin
Date: Thu Nov 17 10:19:26 2016
New Revision: 1770147

URL: http://svn.apache.org/viewvc?rev=1770147&view=rev
Log:
SLING-6298 - register module using first adapter type rather than implementation type

Added:
    sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/Component.java
    sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ComponentImpl.java
Modified:
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java
    sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java

Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java?rev=1770147&r1=1770146&r2=1770147&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java Thu Nov 17 10:19:26 2016
@@ -121,29 +121,30 @@ public class ModelPackageBundleListener
                                 }
                                 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 = ExportServlet.RESOURCE;
-                                        } else if (adaptable == SlingHttpServletRequest.class) {
-                                            accessor = ExportServlet.REQUEST;
-                                        }
-                                        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);
+
+                                String[] resourceTypes = annotation.resourceType();
+                                for (String resourceType : resourceTypes) {
+                                    if (StringUtils.isNotEmpty(resourceType)) {
+                                        for (Class<?> adaptable : annotation.adaptables()) {
+                                            adapterImplementations.registerModelToResourceType(bundle, resourceType, adaptable, adapterTypes[0]);
+                                            ExportServlet.ExportedObjectAccessor accessor = null;
+                                            if (adaptable == Resource.class) {
+                                                accessor = ExportServlet.RESOURCE;
+                                            } else if (adaptable == SlingHttpServletRequest.class) {
+                                                accessor = ExportServlet.REQUEST;
+                                            }
+                                            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);
+                                                }
                                             }
-                                        }
 
+                                        }
                                     }
                                 }
                             }

Added: sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/Component.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/Component.java?rev=1770147&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/Component.java (added)
+++ sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/Component.java Thu Nov 17 10:19:26 2016
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.it.exporter;
+
+public interface Component {
+
+    String getId();
+
+    String getSampleValue();
+}

Added: sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ComponentImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ComponentImpl.java?rev=1770147&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ComponentImpl.java (added)
+++ sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ComponentImpl.java Thu Nov 17 10:19:26 2016
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.it.exporter;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Exporter;
+import org.apache.sling.models.annotations.Model;
+
+import javax.inject.Inject;
+
+@Model(adaptables = { Resource.class }, adapters = Component.class, resourceType = "sling/exp/interface")
+@Exporter(name = "jackson", extensions = "json")
+public class ComponentImpl implements Component {
+
+    private final Resource resource;
+
+    @Inject
+    private String sampleValue;
+
+    public ComponentImpl(Resource resource) {
+        this.resource = resource;
+    }
+
+    public String getId() {
+        return this.resource.getPath();
+    }
+
+    public String getSampleValue() {
+        return sampleValue;
+    }
+}

Modified: sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java?rev=1770147&r1=1770146&r2=1770147&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java (original)
+++ sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java Thu Nov 17 10:19:26 2016
@@ -56,6 +56,7 @@ public class ExporterTest {
     private final String baseComponentPath = "/content/exp/baseComponent";
     private final String childComponentPath = "/content/exp/childComponent";
     private final String extendedComponentPath = "/content/exp/extendedComponent";
+    private final String interfaceComponentPath = "/content/exp/interfaceComponent";
     private Calendar testDate;
 
     @Before
@@ -87,6 +88,14 @@ public class ExporterTest {
             testDate.set(2015, 6, 29);
             properties.put("date", testDate);
             ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false);
+            properties.clear();
+
+            properties.put("sampleValue", "interfaceTESTValue");
+            properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE,
+                    "sling/exp/interface");
+            ResourceUtil.getOrCreateResource(adminResolver, interfaceComponentPath, properties, null, false);
+            properties.clear();
+
             adminResolver.commit();
         } finally {
             if (adminResolver != null && adminResolver.isLive()) {
@@ -113,6 +122,13 @@ public class ExporterTest {
                     Collections.<String, String> emptyMap());
             Assert.assertTrue("JSON Data should contain the property value",
                     StringUtils.contains(jsonData, "extendedTESTValue"));
+
+            final Resource interfaceComponentResource = resolver.getResource(interfaceComponentPath);
+            Assert.assertNotNull(baseComponentResource);
+            jsonData = modelFactory.exportModelForResource(interfaceComponentResource, "jackson", String.class,
+                    Collections.<String, String> emptyMap());
+            Assert.assertTrue("JSON Data should contain the property value",
+                    StringUtils.contains(jsonData, "interfaceTESTValue"));
         } finally {
             if (resolver != null && resolver.isLive()) {
                 resolver.close();
@@ -155,6 +171,12 @@ public class ExporterTest {
             obj = new JSONObject(response.getStringWriter().toString());
             Assert.assertEquals(extendedComponentPath, obj.getString("id"));
             Assert.assertEquals(testDate.getTimeInMillis(), obj.getLong("date"));
+
+            response = new FakeResponse();
+            slingRequestProcessor.processRequest(new FakeRequest(interfaceComponentPath + ".model.json"), response, resolver);
+            obj = new JSONObject(response.getStringWriter().toString());
+            Assert.assertEquals(interfaceComponentPath, obj.getString("id"));
+            Assert.assertEquals("interfaceTESTValue", obj.getString("sampleValue"));
         } finally {
             if (resolver != null && resolver.isLive()) {
                 resolver.close();