Author: bdelacretaz
Date: Thu Aug 27 17:30:34 2009
New Revision: 808547

URL: http://svn.apache.org/viewvc?rev=808547&view=rev
Log:
SLING-1078 - OsgiInstaller ignores invalid and non-bundle jars

Added:
    
sling/trunk/installer/osgi/it/src/test/java/org/apache/sling/osgi/installer/it/InvalidBundlesTest.java
   (with props)
    sling/trunk/installer/osgi/it/src/test/resources/
    sling/trunk/installer/osgi/it/src/test/resources/invalid-jar.jar
Modified:
    
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MockOsgiInstaller.java
    
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/OsgiInstaller.java
    
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
    
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerThread.java
    
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceComparator.java
    
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java
    
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
    sling/trunk/installer/osgi/it/pom.xml

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MockOsgiInstaller.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MockOsgiInstaller.java?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MockOsgiInstaller.java
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MockOsgiInstaller.java
 Thu Aug 27 17:30:34 2009
@@ -47,7 +47,7 @@
     /** Keep track of registered URLS */
     private final Set<String> urls = new HashSet<String>();
     
-    public void addResource(InstallableResource d) throws IOException {
+    public void addResource(InstallableResource d) {
        urls.add(d.getUrl());
         recordCall("add", d);
     }
@@ -56,7 +56,7 @@
         return counters;
     }
 
-    public void registerResources(Collection<InstallableResource> data, String 
urlScheme) throws IOException {
+    public void registerResources(Collection<InstallableResource> data, String 
urlScheme) {
         // Sort the data to allow comparing the recorded calls reliably
         final List<InstallableResource> sorted = new 
LinkedList<InstallableResource>();
         sorted.addAll(data);
@@ -67,7 +67,7 @@
         }
     }
 
-    public void removeResource(InstallableResource d) throws IOException {
+    public void removeResource(InstallableResource d) {
        if(!d.isEmpty()) {
                throw new IllegalArgumentException("InstallableResource must be 
empty for removeResource call");
        }

Modified: 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/OsgiInstaller.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/OsgiInstaller.java?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/OsgiInstaller.java
 (original)
+++ 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/OsgiInstaller.java
 Thu Aug 27 17:30:34 2009
@@ -41,20 +41,23 @@
         *      previously registered/added resources, compares with the new
         *      list and removes resources that have disappeared.
         * 
+     *  Invalid resources are ignored.
+        * 
         *      @param data the list of available resources
         *      @param urlScheme identifies the client. All URLs of the 
supplied data
         *              must use this scheme
         */
-       void registerResources(Collection<InstallableResource> data, String 
urlScheme) throws IOException;
+       void registerResources(Collection<InstallableResource> data, String 
urlScheme);
        
        /** Inform the installer that a resource is available for installation.
         *      also called if the resource has been modified since it was 
registered.
+        *  Invalid resources are ignored.
         */
-       void addResource(InstallableResource r) throws IOException;
+       void addResource(InstallableResource r);
        
        /** Inform the installer that a resource is no longer available 
         *      @param r an empty InstallableResource, isEmpty() must return 
true */
-       void removeResource(InstallableResource r) throws IOException;
+       void removeResource(InstallableResource r);
        
        /** Return counters used for statistics, console display, testing, etc. 
*/
        long [] getCounters();

Modified: 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
 (original)
+++ 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerImpl.java
 Thu Aug 27 17:30:34 2009
@@ -99,17 +99,29 @@
                return counters;
        }
 
-       public void addResource(InstallableResource r) throws IOException {
-           synchronized (installerThread) {
-               installerThread.addNewResource(new 
RegisteredResourceImpl(bundleContext, r));
+       public void addResource(InstallableResource r) {
+           RegisteredResource rr = null; 
+        try {
+            rr = new RegisteredResourceImpl(bundleContext, r);
+        } catch(IOException ioe) {
+            if(getLogService() != null) {
+                getLogService().log(
+                        LogService.LOG_WARNING,
+                        "Cannot create RegisteredResource (resource will be 
ignored):" + r, ioe);
+            }
+            return;
+        }
+        
+        synchronized (installerThread) {
+            installerThread.addNewResource(rr);
         }
        }
 
-       public void registerResources(Collection<InstallableResource> data, 
String urlScheme) throws IOException {
+       public void registerResources(Collection<InstallableResource> data, 
String urlScheme) {
         installerThread.addNewResources(data, urlScheme, bundleContext);
        }
 
-       public void removeResource(InstallableResource r) throws IOException {
+       public void removeResource(InstallableResource r) {
         synchronized (installerThread) {
             installerThread.removeResource(r);
         }

Modified: 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerThread.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerThread.java?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerThread.java
 (original)
+++ 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/OsgiInstallerThread.java
 Thu Aug 27 17:30:34 2009
@@ -164,11 +164,22 @@
     /** Register a number of new resources, and mark others having the same 
scheme as not installable.
      *  Used with {...@link OsgiInstaller.registerResources}
      */
-    void addNewResources(Collection<InstallableResource> data, String 
urlScheme, BundleContext bundleContext) throws IOException {
+    void addNewResources(Collection<InstallableResource> data, String 
urlScheme, BundleContext bundleContext) {
         // Check scheme, do nothing if at least one of them is wrong
         final SortedSet<RegisteredResource> toAdd = new 
TreeSet<RegisteredResource>(new RegisteredResourceComparator());
         for(InstallableResource r : data) {
-            final RegisteredResource rr = new 
RegisteredResourceImpl(bundleContext, r);
+            RegisteredResource rr =  null;
+            try {
+                rr = new RegisteredResourceImpl(bundleContext, r);
+            } catch(IOException ioe) {
+                if(ctx.getLogService() != null) {
+                    ctx.getLogService().log(
+                            LogService.LOG_WARNING,
+                            "Cannot create RegisteredResource (resource will 
be ignored):" + r, ioe);
+                }
+                continue;
+            }
+            
             if(!rr.getUrlScheme().equals(urlScheme)) {
                 throw new IllegalArgumentException(
                         "URL of all supplied InstallableResource must start 
with supplied scheme"

Modified: 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceComparator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceComparator.java?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceComparator.java
 (original)
+++ 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceComparator.java
 Thu Aug 27 17:30:34 2009
@@ -37,19 +37,24 @@
     int compareBundles(RegisteredResource a, RegisteredResource b) {
 
         boolean isSnapshot = false;
+        int result = 0;
         
         // Order first by symbolic name
         final String nameA = 
(String)a.getAttributes().get(Constants.BUNDLE_SYMBOLICNAME);
         final String nameB = 
(String)b.getAttributes().get(Constants.BUNDLE_SYMBOLICNAME);
-        int result = nameA.compareTo(nameB);
+        if(nameA != null) {
+            result = nameA.compareTo(nameB);
+        }
         
         // Then by version
         if(result == 0) {
             final Version va = 
(Version)a.getAttributes().get(Constants.BUNDLE_VERSION);
             final Version vb = 
(Version)b.getAttributes().get(Constants.BUNDLE_VERSION);
-            isSnapshot = 
va.toString().contains(BundleTaskCreator.MAVEN_SNAPSHOT_MARKER);
-            // higher version has more priority, must come first so invert 
comparison
-            result = vb.compareTo(va);
+            isSnapshot = va!= null && 
va.toString().contains(BundleTaskCreator.MAVEN_SNAPSHOT_MARKER);
+            if(va != null && vb != null) {
+                // higher version has more priority, must come first so invert 
comparison
+                result = vb.compareTo(va);
+            }
         }
         
         // Then by priority, higher values first
@@ -82,4 +87,4 @@
     int compareConfig(RegisteredResource a, RegisteredResource b) {
         return 0;
     }
-}
+}
\ No newline at end of file

Modified: 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java
 (original)
+++ 
sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java
 Thu Aug 27 17:30:34 2009
@@ -20,17 +20,12 @@
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.ObjectOutputStream;
 import java.io.OutputStream;
-import java.math.BigInteger;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Dictionary;
@@ -292,10 +287,23 @@
     
     private void setAttributesFromManifest() throws IOException {
        final Manifest m = getManifest(getInputStream());
+       if(m == null) {
+            throw new IOException("Cannot get manifest of bundle resource");
+       }
+       
+       final String sn = 
m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
+        if(sn == null) {
+            throw new IOException("Manifest does not supply " + 
Constants.BUNDLE_SYMBOLICNAME);
+        }
+       
+       final String v = 
m.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
+        if(v == null) {
+            throw new IOException("Manifest does not supply " + 
Constants.BUNDLE_VERSION);
+        }
+       
         if(m != null) {
-            attributes.put(Constants.BUNDLE_SYMBOLICNAME, 
m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME));
-            attributes.put(Constants.BUNDLE_VERSION, 
-                       new 
Version(m.getMainAttributes().getValue(Constants.BUNDLE_VERSION)));
+            attributes.put(Constants.BUNDLE_SYMBOLICNAME, sn);
+            attributes.put(Constants.BUNDLE_VERSION, new Version(v));
         }
     }
     

Modified: 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
 (original)
+++ 
sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
 Thu Aug 27 17:30:34 2009
@@ -72,7 +72,7 @@
        
     @org.junit.Test public void testResourceType() throws Exception {
         {
-            final InputStream s = new ByteArrayInputStream("Some 
data".getBytes());
+            final InputStream s = new 
FileInputStream(getTestBundle("testbundle-1.0.jar"));
             final RegisteredResource r = new LocalFileRegisteredResource(new 
InstallableResource("test:1.jar", s, "some digest"));
             assertEquals(".jar URL creates a BUNDLE resource", 
                     RegisteredResource.ResourceType.BUNDLE, 
r.getResourceType());
@@ -80,7 +80,7 @@
             assertNotNull("BUNDLE resource provides an InputStream", rs);
             rs.close();
             assertNull("BUNDLE resource does not provide a Dictionary", 
r.getDictionary());
-            assertEquals("RegisteredResource entity ID must match", 
"jar:test:1.jar", r.getEntityId());
+            assertEquals("RegisteredResource entity ID must match", 
"bundle:osgi-installer-testbundle", r.getEntityId());
         }
         
         {
@@ -113,11 +113,12 @@
     }
     
        @org.junit.Test public void testLocalFileCopy() throws Exception {
-               final String data = "This is some data";
-               final InputStream in = new 
ByteArrayInputStream(data.getBytes());
-               final LocalFileRegisteredResource r = new 
LocalFileRegisteredResource(new InstallableResource("test:1.jar", in, 
"somedigest"));
+           final File f = getTestBundle("testbundle-1.0.jar");
+        final InputStream s = new FileInputStream(f);
+               final LocalFileRegisteredResource r = new 
LocalFileRegisteredResource(new InstallableResource("test:1.jar", s, 
"somedigest"));
                assertTrue("Local file exists", r.getDataFile(null).exists());
-               assertEquals("Local file length matches our data", 
data.getBytes().length, r.getDataFile(null).length());
+               
+               assertEquals("Local file length matches our data", f.length(), 
r.getDataFile(null).length());
        }
        
     @org.junit.Test public void testMissingDigest() throws Exception {

Modified: sling/trunk/installer/osgi/it/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/it/pom.xml?rev=808547&r1=808546&r2=808547&view=diff
==============================================================================
--- sling/trunk/installer/osgi/it/pom.xml (original)
+++ sling/trunk/installer/osgi/it/pom.xml Thu Aug 27 17:30:34 2009
@@ -261,6 +261,16 @@
                             </archive>
                         </configuration>
                     </execution>
+                    <execution>
+                        <id>missing-headers</id>
+                        <phase>compile</phase>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                        <configuration>
+                            <classifier>notabundle</classifier>
+                        </configuration>
+                    </execution>
                 </executions>
             </plugin>
           </plugins>

Added: 
sling/trunk/installer/osgi/it/src/test/java/org/apache/sling/osgi/installer/it/InvalidBundlesTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/it/src/test/java/org/apache/sling/osgi/installer/it/InvalidBundlesTest.java?rev=808547&view=auto
==============================================================================
--- 
sling/trunk/installer/osgi/it/src/test/java/org/apache/sling/osgi/installer/it/InvalidBundlesTest.java
 (added)
+++ 
sling/trunk/installer/osgi/it/src/test/java/org/apache/sling/osgi/installer/it/InvalidBundlesTest.java
 Thu Aug 27 17:30:34 2009
@@ -0,0 +1,126 @@
+/*
+ * 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.osgi.installer.it;
+
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.sling.osgi.installer.InstallableResource;
+import org.apache.sling.osgi.installer.OsgiInstaller;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.Bundle;
+
+...@runwith(JUnit4TestRunner.class)
+public class InvalidBundlesTest extends OsgiInstallerTestBase {
+
+    @org.ops4j.pax.exam.junit.Configuration
+    public static Option[] configuration() {
+       return defaultConfiguration();
+    }
+    
+    @Before
+    public void setUp() {
+        setupInstaller();
+    }
+    
+    @After
+    public void tearDown() {
+        super.tearDown();
+    }
+    
+    @Test
+    public void testRegisterInvalidBundles() throws Exception {
+        final Collection<InstallableResource> data = new 
ArrayList<InstallableResource>();
+        data.add(getInstallableResource(
+                getTestBundle(BUNDLE_BASE_NAME + "-testbundle-1.1.jar")));
+        data.add(getInstallableResource(
+                getTestBundle(BUNDLE_BASE_NAME + "-notabundle.jar")));
+        data.add(getInstallableResource(
+                getTestBundle("test-classes/invalid-jar.jar")));
+        data.add(getInstallableResource(
+                getTestBundle(BUNDLE_BASE_NAME + "-testB-1.0.jar")));
+        
+        resetCounters();
+        installer.registerResources(data, URL_SCHEME);
+        
waitForInstallerAction(OsgiInstaller.WORKER_THREAD_BECOMES_IDLE_COUNTER, 1);
+        
+        final String info = "All valid bundles must be installed";
+        assertBundle(info, "osgi-installer-testbundle", "1.1", Bundle.ACTIVE);
+        assertBundle(info, "osgi-installer-testB", "1.0", Bundle.ACTIVE);
+        
+        assertNoOsgiTasks("At the end of test");
+    }
+
+    /**
+       @Test
+    public void testIndividualInvalidBundles() throws Exception {
+       final String symbolicName = "osgi-installer-testbundle";
+       int testIndex = 0;
+       
+       assertNull("Test bundle must not be present before test", 
findBundle(symbolicName));
+       
+       // Install first test bundle and check version
+       {
+            assertNull("Test bundle must be absent before installing", 
findBundle(symbolicName));
+           resetCounters();
+           installer.addResource(getInstallableResource(
+                   getTestBundle(BUNDLE_BASE_NAME + "-testbundle-1.1.jar")));
+            
waitForInstallerAction(OsgiInstaller.WORKER_THREAD_BECOMES_IDLE_COUNTER, 1);
+           assertBundle("After installing", symbolicName, "1.1", 
Bundle.ACTIVE);
+       }
+       
+       assertNoOsgiTasks("After test " + testIndex++);
+       
+       // Non-bundle must be ignored
+       {
+            resetCounters();
+            installer.addResource(getInstallableResource(
+                    getTestBundle(BUNDLE_BASE_NAME + "-notabundle.jar")));
+            assertNoOsgiTasks("After installing non-bundle jar");
+       }
+
+        assertNoOsgiTasks("After test " + testIndex++);
+
+        // Invalid archive must be ignored
+        {
+            resetCounters();
+            
installer.addResource(getInstallableResource(getTestBundle("test-classes/invalid-jar.jar")));
+            assertNoOsgiTasks("After installing invalid jar");
+        }
+
+        assertNoOsgiTasks("After test " + testIndex++);
+
+       // Make sure controller is not blocked, by testing an upgrade 
+       {
+           resetCounters();
+            installer.addResource(getInstallableResource(
+                    getTestBundle(BUNDLE_BASE_NAME + "-testbundle-1.2.jar")));
+            
waitForInstallerAction(OsgiInstaller.WORKER_THREAD_BECOMES_IDLE_COUNTER, 1);
+               assertBundle("After updating to 1.2", symbolicName, "1.2", 
Bundle.ACTIVE);
+       }
+
+       assertNoOsgiTasks("After test " + testIndex++);
+       }
+       */
+}
\ No newline at end of file

Propchange: 
sling/trunk/installer/osgi/it/src/test/java/org/apache/sling/osgi/installer/it/InvalidBundlesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/installer/osgi/it/src/test/java/org/apache/sling/osgi/installer/it/InvalidBundlesTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/trunk/installer/osgi/it/src/test/resources/invalid-jar.jar
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/osgi/it/src/test/resources/invalid-jar.jar?rev=808547&view=auto
==============================================================================
    (empty)


Reply via email to