felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1797072 - in /felix/trunk/bundlerepository/src: main/java/org/apache/felix/bundlerepository/impl/ test/java/org/apache/felix/bundlerepository/impl/
Date Wed, 31 May 2017 16:44:34 GMT
Author: davidb
Date: Wed May 31 16:44:33 2017
New Revision: 1797072

URL: http://svn.apache.org/viewvc?rev=1797072&view=rev
Log:
FELIX-5644 Repository#getURI() is no longer unique in case of XML-based repositories

Patch applied on behalf of cfiehe with many thanks.
This closes https://github.com/apache/felix/pull/109

Modified:
    felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/DataModelHelperImpl.java
    felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/PullParser.java
    felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/RepositoryParser.java
    felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java
    felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/StaxParser.java
    felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java
    felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java

Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/DataModelHelperImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/DataModelHelperImpl.java?rev=1797072&r1=1797071&r2=1797072&view=diff
==============================================================================
--- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/DataModelHelperImpl.java
(original)
+++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/DataModelHelperImpl.java
Wed May 31 16:44:33 2017
@@ -91,7 +91,6 @@ public class DataModelHelperImpl impleme
     public Repository repository(final URL url) throws Exception
     {
         InputStream is = null;
-        BufferedReader br = null;
 
         try
         {
@@ -132,13 +131,19 @@ public class DataModelHelperImpl impleme
 
             if (is != null)
             {
-                String repostr = url.toExternalForm();
-                if (repostr.endsWith("zip")) {
-                    repostr = "jar:".concat(repostr).concat("!/");
-                } else if (repostr.endsWith(".xml")) {
-                    repostr = repostr.substring(0, repostr.lastIndexOf('/')+1);
+                String repositoryUri = url.toExternalForm();
+                String baseUri;
+                if (repositoryUri.endsWith(".zip")) {
+                    baseUri = new StringBuilder("jar:").append(repositoryUri).append("!/").toString();
+                } else if (repositoryUri.endsWith(".xml")) {
+                    baseUri = repositoryUri.substring(0, repositoryUri.lastIndexOf('/') +
1);
+                } else {
+                    baseUri = repositoryUri;
                 }
-                return repository(is, repostr);
+                RepositoryImpl repository = repository(is, URI.create(baseUri));
+                repository.setURI(repositoryUri);
+
+                return repository;
             }
             else
             {
@@ -162,10 +167,10 @@ public class DataModelHelperImpl impleme
         }
     }
 
-    public RepositoryImpl repository(InputStream is, String uri) throws Exception
+    public RepositoryImpl repository(InputStream is, URI baseURI) throws Exception
     {
         RepositoryParser parser = RepositoryParser.getParser();
-        RepositoryImpl repository = parser.parseRepository(is, uri);
+        RepositoryImpl repository = parser.parseRepository(is, baseURI);
         
         return repository;
     }

Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/PullParser.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/PullParser.java?rev=1797072&r1=1797071&r2=1797072&view=diff
==============================================================================
--- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/PullParser.java
(original)
+++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/PullParser.java
Wed May 31 16:44:33 2017
@@ -21,6 +21,7 @@ package org.apache.felix.bundlerepositor
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
+import java.net.URI;
 
 import org.kxml2.io.KXmlParser;
 import org.xmlpull.v1.XmlPullParser;
@@ -36,7 +37,7 @@ public class PullParser extends Reposito
     {
     }
 
-    public RepositoryImpl parseRepository(InputStream is, String repositoryURI) throws Exception
+    public RepositoryImpl parseRepository(InputStream is, URI baseUri) throws Exception
     {
         XmlPullParser reader = new KXmlParser();
 
@@ -51,16 +52,15 @@ public class PullParser extends Reposito
         }
 
         RepositoryImpl repo;
-        if ("http://www.osgi.org/xmlns/repository/v1.0.0".equals(reader.getNamespace()))
+        if ("http://www.osgi.org/xmlns/repository/v1.0.0".equals(reader.getNamespace()))
{
             // TODO there are a bunch of other methods here that create a parser, should
they be updated too?
             // at the very least they should be made namespace-aware too, so that parsing
is the same no matter
             // how its initiated.
-            return SpecXMLPullParser.parse(reader, repositoryURI);
-        else
+            return SpecXMLPullParser.parse(reader, baseUri);
+        } else {
             // We're parsing the old
-            repo = parse(reader);
-            repo.setURI(repositoryURI);
-            return repo;
+            return parse(reader);
+        }
     }
 
     public RepositoryImpl parseRepository(Reader r) throws Exception

Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/RepositoryParser.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/RepositoryParser.java?rev=1797072&r1=1797071&r2=1797072&view=diff
==============================================================================
--- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/RepositoryParser.java
(original)
+++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/RepositoryParser.java
Wed May 31 16:44:33 2017
@@ -20,6 +20,7 @@ package org.apache.felix.bundlerepositor
 
 import java.io.InputStream;
 import java.io.Reader;
+import java.net.URI;
 
 public abstract class RepositoryParser
 {
@@ -71,7 +72,7 @@ public abstract class RepositoryParser
     }
 
 
-    public abstract RepositoryImpl parseRepository(InputStream is, String repositoryURI)
throws Exception;
+    public abstract RepositoryImpl parseRepository(InputStream is, URI baseUri) throws Exception;
 
     public abstract RepositoryImpl parseRepository(Reader r) throws Exception;
 

Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java?rev=1797072&r1=1797071&r2=1797072&view=diff
==============================================================================
--- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java
(original)
+++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java
Wed May 31 16:44:33 2017
@@ -49,10 +49,9 @@ public class SpecXMLPullParser
     private static final String REQUIREMENT = "requirement";
     private static final String RESOURCE = "resource";
 
-    public static RepositoryImpl parse(XmlPullParser reader, String repositoryURI) throws
Exception
+    public static RepositoryImpl parse(XmlPullParser reader, URI baseUri) throws Exception
     {
         RepositoryImpl repository = new RepositoryImpl();
-        repository.setURI(repositoryURI);
         for (int i = 0, ac = reader.getAttributeCount(); i < ac; i++)
         {
             String name = reader.getAttributeName(i);
@@ -73,7 +72,7 @@ public class SpecXMLPullParser
             }
             else if (RESOURCE.equals(element))
             {
-                Resource resource = parseResource(reader, repositoryURI);
+                Resource resource = parseResource(reader, baseUri);
                 repository.addResource(resource);
             }
             else
@@ -86,7 +85,7 @@ public class SpecXMLPullParser
         return repository;
     }
 
-    private static Resource parseResource(XmlPullParser reader, String repositoryURI) throws
Exception
+    private static Resource parseResource(XmlPullParser reader, URI baseUri) throws Exception
     {
         ResourceImpl resource = new ResourceImpl();
         try
@@ -97,7 +96,7 @@ public class SpecXMLPullParser
                 String element = reader.getName();
                 if (CAPABILITY.equals(element))
                 {
-                    Capability capability = parseCapability(reader, resource, repositoryURI);
+                    Capability capability = parseCapability(reader, resource, baseUri);
                     if (capability != null)
                         resource.addCapability(capability);
                 }
@@ -123,7 +122,7 @@ public class SpecXMLPullParser
         }
     }
 
-    private static Capability parseCapability(XmlPullParser reader, ResourceImpl resource,
String repositoryURI) throws Exception
+    private static Capability parseCapability(XmlPullParser reader, ResourceImpl resource,
URI baseUri) throws Exception
     {
         String namespace = reader.getAttributeValue(null, NAMESPACE);
         if (IdentityNamespace.IDENTITY_NAMESPACE.equals(namespace))
@@ -135,7 +134,7 @@ public class SpecXMLPullParser
         {
             if (resource.getURI() == null)
             {
-                parseContentNamespace(reader, resource, repositoryURI);
+                parseContentNamespace(reader, resource, baseUri);
                 return null;
             }
             // if the URI is already set, this is a second osgi.content capability.
@@ -189,11 +188,10 @@ public class SpecXMLPullParser
         }
     }
 
-    private static void parseContentNamespace(XmlPullParser reader, ResourceImpl resource,
String repositoryURI) throws Exception
+    private static void parseContentNamespace(XmlPullParser reader, ResourceImpl resource,
URI baseUri) throws Exception
     {
         Map<String, Object> attributes = new HashMap<String, Object>();
         parseAttributesDirectives(reader, attributes, new HashMap<String, String>(),
CAPABILITY);
-        URI repo = URI.create(repositoryURI);
         
         for (Map.Entry<String, Object> entry : attributes.entrySet())
         {
@@ -202,9 +200,11 @@ public class SpecXMLPullParser
                 continue;
             else if (ContentNamespace.CAPABILITY_URL_ATTRIBUTE.equals(entry.getKey())) {
                 String value = (String) entry.getValue();
-                URI uri = URI.create(value); 
-                URI resourceURI = uri.isAbsolute()? uri:URI.create(repo.toString().concat(value));
-                resource.put(Resource.URI, resourceURI);
+                URI resourceUri = URI.create(value);
+                if (!resourceUri.isAbsolute()) {
+                    resourceUri = URI.create(new StringBuilder(baseUri.toString()).append(value).toString());
+                }
+                resource.put(Resource.URI, resourceUri);
             }
             else
                 resource.put(entry.getKey(), entry.getValue());

Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/StaxParser.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/StaxParser.java?rev=1797072&r1=1797071&r2=1797072&view=diff
==============================================================================
--- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/StaxParser.java
(original)
+++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/StaxParser.java
Wed May 31 16:44:33 2017
@@ -20,6 +20,8 @@ package org.apache.felix.bundlerepositor
 
 import java.io.InputStream;
 import java.io.Reader;
+import java.net.URI;
+
 import javax.xml.stream.Location;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamConstants;
@@ -69,7 +71,7 @@ public class StaxParser extends Reposito
         return false;
     }
 
-    public RepositoryImpl parseRepository(InputStream is, String repositoryURI) throws Exception
+    public RepositoryImpl parseRepository(InputStream is, URI baseUri) throws Exception
     {
         XMLStreamReader reader = getFactory().createXMLStreamReader(is);
         int event = reader.nextTag();
@@ -77,9 +79,7 @@ public class StaxParser extends Reposito
         {
             throw new Exception("Expected element 'repository' at the root of the document");
         }
-        RepositoryImpl repo = parseRepository(reader);
-        repo.setURI(repositoryURI);
-        return repo;
+        return parseRepository(reader);
     }
 
     public RepositoryImpl parseRepository(Reader r) throws Exception

Modified: felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java?rev=1797072&r1=1797071&r2=1797072&view=diff
==============================================================================
--- felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java
(original)
+++ felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryXMLTest.java
Wed May 31 16:44:33 2017
@@ -71,7 +71,7 @@ public class OSGiRepositoryXMLTest exten
     public void testIdentityCapabilityWithRelativePath() throws Exception {
         URL url = getClass().getResource("/spec_repository.xml");
         RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
-        repoAdmin.addRepository(url);
+        RepositoryImpl repository = (RepositoryImpl) repoAdmin.addRepository(url);
 
         Resolver resolver = repoAdmin.resolver();
 
@@ -86,15 +86,17 @@ public class OSGiRepositoryXMLTest exten
 
         org.apache.felix.bundlerepository.Resource[] resources = resolver.getAddedResources();
         assertNotNull(resources[0]);
-        String repostr = url.toExternalForm().substring(0, url.toExternalForm().lastIndexOf('/')+1);
-        assertEquals(repostr+"repo_files/test_file_6.jar", resources[0].getURI());
-   
+        
+        String repositoryUri = repository.getURI();
+        String baseUri = repositoryUri.substring(0, repositoryUri.lastIndexOf('/') + 1);
+        String resourceUri = new StringBuilder(baseUri).append("repo_files/test_file_6.jar").toString();
+        assertEquals(resourceUri, resources[0].getURI());
     }
 
     public void testIdentityCapabilityForZipWithRelativePath() throws Exception {
         URL url = getClass().getResource("/spec_repository.zip");
         RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
-        repoAdmin.addRepository(url);
+        RepositoryImpl repository = (RepositoryImpl) repoAdmin.addRepository(url);
 
         Resolver resolver = repoAdmin.resolver();
 
@@ -109,9 +111,11 @@ public class OSGiRepositoryXMLTest exten
 
         org.apache.felix.bundlerepository.Resource[] resources = resolver.getAddedResources();
         assertNotNull(resources[0]);
-        String repostr = url.toExternalForm().substring(0, url.toExternalForm().lastIndexOf('/')+1);
-        assertEquals("jar:"+ repostr+"spec_repository.zip!/repo_files/test_file_6.jar", resources[0].getURI());
-   
+        
+        String repositoryUri = repository.getURI();
+        String baseUri = new StringBuilder("jar:").append(repositoryUri).append("!/").toString();
+        String resourceUri = new StringBuilder(baseUri).append("repo_files/test_file_6.jar").toString();
+        assertEquals(resourceUri, resources[0].getURI());
     }
 
 

Modified: felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java?rev=1797072&r1=1797071&r2=1797072&view=diff
==============================================================================
--- felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java
(original)
+++ felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/RepositoryAdminTest.java
Wed May 31 16:44:33 2017
@@ -24,6 +24,7 @@ import java.util.Hashtable;
 
 import junit.framework.TestCase;
 
+import org.apache.felix.bundlerepository.Repository;
 import org.apache.felix.bundlerepository.Resource;
 import org.apache.felix.utils.filter.FilterImpl;
 import org.apache.felix.utils.log.Logger;
@@ -55,6 +56,22 @@ public class RepositoryAdminTest extends
         assertNotNull(resources);
         assertEquals(1, resources.length);
     }
+    
+    public void testRemoveRepository() throws Exception {
+        URL url = getClass().getResource("/repo_for_resolvertest.xml");
+
+        RepositoryAdminImpl repoAdmin = createRepositoryAdmin();
+        Repository repository = repoAdmin.addRepository(url);
+        assertNotNull(repository);
+
+        String repositoryUri = repository.getURI();
+        assertNotNull(repositoryUri);
+
+        assertTrue(repoAdmin.removeRepository(repositoryUri));
+        for (Repository repo : repoAdmin.listRepositories()) {
+            assertNotSame(repositoryUri, repo.getURI());
+        }
+    }
 
     private RepositoryAdminImpl createRepositoryAdmin() throws Exception
     {



Mime
View raw message