Author: marrs
Date: Mon Oct  6 15:25:07 2014
New Revision: 1629677

URL: http://svn.apache.org/r1629677
Log:
ACE-482 Implemented a fix and test for bundles with invalid metadata. They are 
now ignored.

Modified:
    
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/metadata/bindex/BIndexMetadataGenerator.java
    ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/bindex/Index.java
    
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/BundleInfo.java
    
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/Manifest.java
    
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java

Modified: 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/metadata/bindex/BIndexMetadataGenerator.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/metadata/bindex/BIndexMetadataGenerator.java?rev=1629677&r1=1629676&r2=1629677&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/metadata/bindex/BIndexMetadataGenerator.java
 (original)
+++ 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/metadata/bindex/BIndexMetadataGenerator.java
 Mon Oct  6 15:25:07 2014
@@ -42,6 +42,7 @@ public class BIndexMetadataGenerator imp
             File index = new File(directory, INDEX_FILENAME + INDEX_EXTENSION);
             try {
                 tempIndex = File.createTempFile("repo", INDEX_EXTENSION, 
directory);
+                Index.m_log = m_log;
                 Index.main(new String[] { "-q", "-a", "-r", 
tempIndex.getAbsolutePath(), directory.getAbsolutePath() });
                 renameFile(tempIndex, index);
             }

Modified: 
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/bindex/Index.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/bindex/Index.java?rev=1629677&r1=1629676&r2=1629677&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/bindex/Index.java 
(original)
+++ ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/bindex/Index.java Mon 
Oct  6 15:25:07 2014
@@ -46,6 +46,7 @@ import org.osgi.impl.bundle.obr.resource
 import org.osgi.impl.bundle.obr.resource.ResourceImpl;
 import org.osgi.impl.bundle.obr.resource.Tag;
 import org.osgi.impl.bundle.obr.resource.VersionRange;
+import org.osgi.service.log.LogService;
 
 /**
  * Iterate over a set of given bundles and convert them to resources. When -a 
is specified, other resources than bundles
@@ -68,6 +69,25 @@ public class Index
         .getAbsoluteFile();
     static RepositoryImpl repository;
     static String root;
+       public static LogService m_log;
+       
+       static void error(String message) {
+               if (m_log != null) {
+                       m_log.log(LogService.LOG_ERROR, message);
+               }
+               else {
+                       System.err.println(message);
+               }
+       }
+       static void error(String message, Throwable t) {
+               if (m_log != null) {
+                       m_log.log(LogService.LOG_ERROR, message, t);
+               }
+               else {
+                       System.err.println(message);
+                       t.printStackTrace(System.err);
+               }
+       }
 
     /**
      * Main entry. See -help for options.
@@ -76,9 +96,6 @@ public class Index
      * @throws Exception
      */
     public static void main(String args[]) throws Exception {
-        System.err.println("Bundle Indexer | v2.2");
-        System.err.println("(c) 2007 OSGi, All Rights Reserved");
-
         Set<ResourceImpl> resources = new HashSet<ResourceImpl>();
         root = rootFile.toURI().toURL().toString();
         repository = new RepositoryImpl(rootFile.toURI().toURL());
@@ -104,8 +121,7 @@ public class Index
                         args[++i]);
                 }
                 else if (args[i].startsWith("-help")) {
-                    System.err
-                        .println("bindex [-t \"%s\" symbolic name \"%v\" 
version \"%f\" filename \"%p\" dirpath ] [ -r repository.(xml|zip) ] [-help] 
[-l file:license.html ] [-quiet] [-all] <jar file>*");
+                    error("bindex [-t \"%s\" symbolic name \"%v\" version 
\"%f\" filename \"%p\" dirpath ] [ -r repository.(xml|zip) ] [-help] [-l 
file:license.html ] [-quiet] [-all] <jar file>*");
                 }
                 else if (args[i].startsWith("-a")) {
                     all = true;
@@ -115,9 +131,7 @@ public class Index
                 }
             }
             catch (Exception e) {
-                System.err.println("Error in " + args[i] + " : " +
-                    e.getMessage());
-                e.printStackTrace();
+               error("Error in " + args[i] + " : " + e.getMessage(), e);
             }
         }
 
@@ -197,24 +211,34 @@ public class Index
                 return;
             }
             if (path.getName().endsWith(".jar")) {
-                BundleInfo info = new BundleInfo(repository, path);
-                ResourceImpl resource = info.build();
-                if (urlTemplate != null) {
-                    doTemplate(path, resource);
-                }
-                else {
-                    resource.setURL(path.toURI().toURL());
-                }
-
-                resources.add(resource);
+               try {
+                       BundleInfo info = new BundleInfo(repository, path);
+                       ResourceImpl resource = info.build();
+                       if (urlTemplate != null) {
+                           doTemplate(path, resource);
+                       }
+                       else {
+                           resource.setURL(path.toURI().toURL());
+                       }
+       
+                       resources.add(resource);
+               }
+               catch (Exception e) {
+                       error("could not parse bundle " + path.getName() + ": " 
+ e.getMessage(), e);
+               }
             }
             else {
                 // this is some other resource, we might want to include it.
                 if (all) {
-                    ResourceMetaData metadata = 
ResourceMetaData.getArtifactMetaData(path.getName());
-                    ResourceImpl impl = new ResourceImpl(repository, 
metadata.getSymbolicName(), new VersionRange(metadata.getVersion()));
-                    impl.setURL(path.toURI().toURL());
-                    resources.add(impl);
+                       try {
+                           ResourceMetaData metadata = 
ResourceMetaData.getArtifactMetaData(path.getName());
+                           ResourceImpl impl = new ResourceImpl(repository, 
metadata.getSymbolicName(), new VersionRange(metadata.getVersion()));
+                           impl.setURL(path.toURI().toURL());
+                           resources.add(impl);
+                       }
+                       catch (Exception e) {
+                               error("could not parse resource " + 
path.getName() + ": " + e.getMessage(), e);
+                       }
                 }
             }
         }

Modified: 
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/BundleInfo.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/BundleInfo.java?rev=1629677&r1=1629676&r2=1629677&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/BundleInfo.java
 (original)
+++ 
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/BundleInfo.java
 Mon Oct  6 15:25:07 2014
@@ -17,10 +17,21 @@
  */
 package org.osgi.impl.bundle.obr.resource;
 
-import java.io.*;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
-import java.util.*;
-import java.util.zip.*;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.StringTokenizer;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
 import org.osgi.service.obr.Resource;
 
@@ -78,8 +89,8 @@ public class BundleInfo {
                ResourceImpl resource;
                // Setup the manifest
                // and create a resource
-               resource = new ResourceImpl(repository, 
manifest.getSymbolicName(), manifest
-                               .getVersion());
+               resource = new ResourceImpl(repository, 
manifest.getSymbolicName(), new VersionRange(manifest
+                               .getVersion().toString()));
 
                try {
 

Modified: 
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/Manifest.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/Manifest.java?rev=1629677&r1=1629676&r2=1629677&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/Manifest.java
 (original)
+++ 
ace/trunk/org.apache.ace.obr/src/org/osgi/impl/bundle/obr/resource/Manifest.java
 Mon Oct  6 15:25:07 2014
@@ -17,8 +17,22 @@
  */
 package org.osgi.impl.bundle.obr.resource;
 
-import java.io.*;
-import java.util.*;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StreamTokenizer;
+import java.io.StringReader;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+import org.osgi.framework.Version;
 
 import aQute.bnd.annotation.ProviderType;
 
@@ -38,7 +52,7 @@ public class Manifest extends Hashtable 
        Vector                          duplicates      = new Vector();
        final static String     wordparts       = "~!@#$%^&*_:/?><.-+";
        ManifestEntry           bsn;
-       VersionRange                    version;
+       Version                         version;
        ManifestEntry           host;
        List                            require;
 
@@ -112,12 +126,10 @@ public class Manifest extends Hashtable 
                                }
                                if (header.equals("bundle-version")) {
                                        try {
-                                               version = new 
VersionRange(value.trim());
+                                               version = new 
Version(value.trim());
                                        }
                                        catch (Exception e) {
-                                               version = new VersionRange("0");
-                                               System.err.println("Invalid 
version attr for: " + bsn
-                                                               + " value is " 
+ value);
+                                               throw new IOException("Invalid 
version attr for: " + bsn + " value is " + value);
                                        }
                                }
                                if (header.equals("fragment-host"))
@@ -317,9 +329,10 @@ public class Manifest extends Hashtable 
                        return null;
        }
 
-       public VersionRange getVersion() {
-               if (version == null)
-                       return new VersionRange("0");
+       public Version getVersion() {
+               if (version == null) {
+                       throw new IllegalStateException("bundle version is 
null");
+               }
                return version;
        }
 

Modified: 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java?rev=1629677&r1=1629676&r2=1629677&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java
 (original)
+++ 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java
 Mon Oct  6 15:25:07 2014
@@ -36,6 +36,7 @@ public class BindexMetadataTest {
     private ArtifactData generateBundle(File file, String symbolicName, String 
version) throws Exception {
         // create a mock bundle, which is only used to generate the bundle on 
disk, and not used for anything else...
         ArtifactData bundle = new ArtifactDataImpl(file.getName(), 
symbolicName, -1L, version, file.toURI().toURL(), false);
+        System.out.println("GETVERSION: " + bundle.getVersion());
         BundleStreamGenerator.generateBundle(bundle);
         return bundle;
     }
@@ -65,7 +66,7 @@ public class BindexMetadataTest {
             }
         }
         in.close();
-        assert count == 3 : "Expected 3 resources in the repositoty index, 
found " + count + ".";
+        assert count == 3 : "Expected 3 resources in the repository index, 
found " + count + ".";
     }
 
     /**
@@ -94,6 +95,34 @@ public class BindexMetadataTest {
             }
         }
         in.close();
-        assert count == 0 : "Expected 0 resources in the repositoty index, 
found " + count + ".";
+        assert count == 0 : "Expected 0 resources in the repository index, 
found " + count + ".";
+    }
+
+    /**
+     * Generate metadata index with partially invalid contents, verify contents
+     */
+    @Test(groups = { UNIT })
+    public void generatePartiallyInvalidMetaData() throws Exception {
+        File dir = File.createTempFile("meta", "");
+        dir.delete();
+        dir.mkdir();
+        generateBundle(File.createTempFile("bundle", ".jar", dir), 
"bundle.symbolicname.1", "1.0.0");
+        generateBundle(File.createTempFile("bundle", ".jar", dir), 
"bundle.symbolicname.2", "1.0_0");
+        generateBundle(File.createTempFile("bundle", ".jar", dir), 
"bundle.symbolicname.3", "1.0.0");
+        MetadataGenerator meta = new BIndexMetadataGenerator();
+        meta.generateMetadata(dir);
+        File index = new File(dir, "repository.xml");
+        assert index.exists() : "No repository index was generated";
+        assert index.length() > 0 : "Repository index can not be size 0";
+        int count = 0;
+        String line;
+        BufferedReader in = new BufferedReader(new FileReader(index));
+        while ((line = in.readLine()) != null) {
+            if (line.contains("<resource")) {
+                count++;
+            }
+        }
+        in.close();
+        assert count == 2 : "Expected 2 resources in the repository index, 
found " + count + ".";
     }
 }


Reply via email to