sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1626854 - in /sling/trunk/bundles/extensions/adapter: ./ src/main/java/org/apache/sling/adapter/internal/ src/main/resources/res/ui/ src/test/java/org/apache/sling/adapter/internal/
Date Mon, 22 Sep 2014 17:37:25 GMT
Author: justin
Date: Mon Sep 22 17:37:24 2014
New Revision: 1626854

URL: http://svn.apache.org/r1626854
Log:
SLING-3960 - adding validation to adapters and adaptables, both at registration time and in
the web console.

Modified:
    sling/trunk/bundles/extensions/adapter/pom.xml
    sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java
    sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java
    sling/trunk/bundles/extensions/adapter/src/main/resources/res/ui/adapters.css
    sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java

Modified: sling/trunk/bundles/extensions/adapter/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/adapter/pom.xml?rev=1626854&r1=1626853&r2=1626854&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/adapter/pom.xml (original)
+++ sling/trunk/bundles/extensions/adapter/pom.xml Mon Sep 22 17:37:24 2014
@@ -153,5 +153,11 @@
             <version>2.5</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>junit-addons</groupId>
+            <artifactId>junit-addons</artifactId>
+            <version>1.4</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java?rev=1626854&r1=1626853&r2=1626854&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java
(original)
+++ sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java
Mon Sep 22 17:37:24 2014
@@ -48,6 +48,7 @@ import org.osgi.framework.ServiceReferen
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventAdmin;
+import org.osgi.service.packageadmin.PackageAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -111,6 +112,9 @@ public class AdapterManagerImpl implemen
     @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
     private volatile EventAdmin eventAdmin;
 
+    @Reference
+    private PackageAdmin packageAdmin;
+
     // ---------- AdapterManager interface -------------------------------------
 
     /**
@@ -247,6 +251,18 @@ public class AdapterManagerImpl implemen
             return;
         }
 
+        for (String clazz : adaptables) {
+            if (!checkPackage(packageAdmin, clazz)) {
+                log.warn("Adaptable class {} in factory service {} is not in an exported
package.", clazz, reference.getProperty(Constants.SERVICE_ID));
+            }
+        }
+
+        for (String clazz : adapters) {
+            if (!checkPackage(packageAdmin, clazz)) {
+                log.warn("Adapter class {} in factory service {} is not in an exported package.",
clazz, reference.getProperty(Constants.SERVICE_ID));
+            }
+        }
+
         final AdapterFactoryDescriptor factoryDesc = new AdapterFactoryDescriptor(context,
                 reference, adapters);
 
@@ -279,6 +295,22 @@ public class AdapterManagerImpl implemen
     }
 
     /**
+     * Check that the package containing the class is exported or is a java.*
+     * class.
+     * 
+     * @param packageAdmin the PackageAdmin service
+     * @param clazz the class name
+     * @return true if the package is exported
+     */
+    static boolean checkPackage(PackageAdmin packageAdmin, String clazz) {
+        String packageName = clazz.substring(0, clazz.lastIndexOf('.'));
+        if (packageName.startsWith("java.")) {
+            return true;
+        }
+        return packageAdmin.getExportedPackage(packageName) != null;
+    }
+
+    /**
      * Unregisters the {@link AdapterFactory} referred to by the service
      * <code>reference</code> from the registry.
      */

Modified: sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java?rev=1626854&r1=1626853&r2=1626854&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java
(original)
+++ sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java
Mon Sep 22 17:37:24 2014
@@ -43,6 +43,7 @@ import org.apache.commons.lang.builder.C
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.adapter.AdapterFactory;
 import org.apache.sling.commons.json.JSONArray;
@@ -58,6 +59,7 @@ import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
+import org.osgi.service.packageadmin.PackageAdmin;
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
@@ -82,6 +84,9 @@ public class AdapterWebConsolePlugin ext
     private static final String ADAPTER_DEPRECATED = "adapter.deprecated";
 
     private final Logger logger = LoggerFactory.getLogger(AdapterWebConsolePlugin.class);
+    
+    @Reference
+    private PackageAdmin packageAdmin;
 
     private List<AdaptableDescription> allAdaptables;
     private Map<ServiceReference, List<AdaptableDescription>> adapterServiceReferences;
@@ -266,10 +271,26 @@ public class AdapterWebConsolePlugin ext
         writer.println("<thead><tr><th class=\"header\">${Adaptable Class}</th><th
class=\"header\">${Adapter Class}</th><th class=\"header\">${Condition}</th><th
class=\"header\">${Deprecated}</th><th class=\"header\">${Providing Bundle}</th></tr></thead>");
         String rowClass = "odd";
         for (final AdaptableDescription desc : allAdaptables) {
-            writer.printf("<tr class=\"%s ui-state-default\"><td>%s</td>",
rowClass, desc.adaptable);
+            writer.printf("<tr class=\"%s ui-state-default\"><td>", rowClass);
+            boolean packageExported = AdapterManagerImpl.checkPackage(packageAdmin, desc.adaptable);
+            if (!packageExported) {
+                writer.print("<span class='error'>");
+            }
+            writer.print(desc.adaptable);
+            if (!packageExported) {
+                writer.print("</span>");
+            }
+            writer.print("</td>");
             writer.print("<td>");
             for (final String adapter : desc.adapters) {
+                packageExported = AdapterManagerImpl.checkPackage(packageAdmin, adapter);
+                if (!packageExported) {
+                    writer.print("<span class='error'>");
+                }
                 writer.print(adapter);
+                if (!packageExported) {
+                    writer.print("</span>");
+                }
                 writer.print("<br/>");
             }
             writer.print("</td>");

Modified: sling/trunk/bundles/extensions/adapter/src/main/resources/res/ui/adapters.css
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/adapter/src/main/resources/res/ui/adapters.css?rev=1626854&r1=1626853&r2=1626854&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/adapter/src/main/resources/res/ui/adapters.css (original)
+++ sling/trunk/bundles/extensions/adapter/src/main/resources/res/ui/adapters.css Mon Sep
22 17:37:24 2014
@@ -16,5 +16,9 @@
  */
  
 table.adapters tr .header {
-	background-image: none;
+    background-image: none;
+}
+
+table.adapters tr .error {
+    color: red;
 }
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java?rev=1626854&r1=1626853&r2=1626854&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
(original)
+++ sling/trunk/bundles/extensions/adapter/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
Mon Sep 22 17:37:24 2014
@@ -35,9 +35,12 @@ import org.osgi.framework.Filter;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 import java.util.Map;
 
+import junitx.util.PrivateAccessor;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -50,8 +53,15 @@ public class AdapterManagerTest {
 
     protected final Mockery context = new JUnit4Mockery();
 
-    @org.junit.Before public void setUp() {
+    @org.junit.Before public void setUp() throws Exception {
         am = new AdapterManagerImpl();
+        final PackageAdmin pa = this.context.mock(PackageAdmin.class);
+        final ExportedPackage ep = this.context.mock(ExportedPackage.class);
+        this.context.checking(new Expectations(){{
+            allowing(pa).getExportedPackage(with(any(String.class)));
+            will(returnValue(ep));
+        }});
+        PrivateAccessor.setField(am, "packageAdmin", pa);
     }
 
     @org.junit.After public void tearDown() {



Mime
View raw message