Author: hibou
Date: Tue Apr 12 21:30:14 2011
New Revision: 1091577

URL: http://svn.apache.org/viewvc?rev=1091577&view=rev
Log:
Add support for a composite P2 repository

Added:
    ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java   
(with props)
Modified:
    ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/repo/RepoDescriptor.java
    
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoader.java
    
ant/ivy/core/trunk/test/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoaderTest.java

Added: ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java?rev=1091577&view=auto
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java 
(added)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java 
Tue Apr 12 21:30:14 2011
@@ -0,0 +1,120 @@
+/*
+ *  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.ivy.osgi.p2;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.ivy.osgi.util.DelegetingHandler;
+import org.apache.ivy.util.XMLHelper;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+public class P2CompositeParser implements XMLInputParser {
+
+    private List/* <String> */childLocations;
+
+    public List getChildLocations() {
+        return childLocations;
+    }
+
+    public void parse(InputStream in) throws ParseException, IOException, 
SAXException {
+        RepositoryHandler handler = new RepositoryHandler();
+        try {
+            XMLHelper.parse(in, null, handler, null);
+        } catch (ParserConfigurationException e) {
+            throw new SAXException(e);
+        }
+        childLocations = handler.childLocations;
+    }
+
+    static class RepositoryHandler extends DelegetingHandler {
+
+        private static final String REPOSITORY = "repository";
+
+        // private static final String NAME = "name";
+        //
+        // private static final String TYPE = "type";
+        //
+        // private static final String VERSION = "version";
+
+        List/* <String> */childLocations;
+
+        public RepositoryHandler() {
+            super(REPOSITORY);
+            addChild(new ChildrenHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    childLocations = ((ChildrenHandler) child).childLocations;
+                }
+            });
+        }
+        // protected void handleAttributes(Attributes atts) {
+        // String name = atts.getValue(NAME);
+        // String type = atts.getValue(TYPE);
+        // String version = atts.getValue(VERSION);
+        // }
+    }
+
+    static class ChildrenHandler extends DelegetingHandler {
+
+        private static final String CHILDREN = "children";
+
+        private static final String SIZE = "size";
+
+        List/* <String> */childLocations;
+
+        public ChildrenHandler() {
+            super(CHILDREN);
+            addChild(new ChildHandler(), new ChildElementHandler() {
+                public void childHanlded(DelegetingHandler child) {
+                    childLocations.add(((ChildHandler) child).location);
+                }
+            });
+        }
+
+        protected void handleAttributes(Attributes atts) {
+            int size = Integer.parseInt(atts.getValue(SIZE));
+            childLocations = new ArrayList(size);
+        }
+
+    }
+
+    static class ChildHandler extends DelegetingHandler {
+
+        private static final String CHILD = "child";
+
+        private static final String LOCATION = "location";
+
+        String location;
+
+        public ChildHandler() {
+            super(CHILD);
+        }
+
+        protected void handleAttributes(Attributes atts) {
+            location = atts.getValue(LOCATION);
+        }
+
+    }
+
+}

Propchange: 
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: 
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/p2/P2CompositeParser.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/repo/RepoDescriptor.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/repo/RepoDescriptor.java?rev=1091577&r1=1091576&r2=1091577&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/repo/RepoDescriptor.java 
(original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/repo/RepoDescriptor.java 
Tue Apr 12 21:30:14 2011
@@ -43,7 +43,7 @@ public class RepoDescriptor {
         this.profileProvider = profileProvider;
     }
 
-    public Set getModules() {
+    public Set/* <ModuleDescriptor> */ getModules() {
         return modules;
     }
 

Modified: 
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoader.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoader.java?rev=1091577&r1=1091576&r2=1091577&view=diff
==============================================================================
--- 
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoader.java
 (original)
+++ 
ant/ivy/core/trunk/src/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoader.java
 Tue Apr 12 21:30:14 2011
@@ -28,6 +28,7 @@ import java.util.zip.ZipInputStream;
 
 import org.apache.ivy.osgi.core.ExecutionEnvironmentProfileProvider;
 import org.apache.ivy.osgi.p2.P2ArtifactParser;
+import org.apache.ivy.osgi.p2.P2CompositeParser;
 import org.apache.ivy.osgi.p2.P2Descriptor;
 import org.apache.ivy.osgi.p2.P2MetadataParser;
 import org.apache.ivy.osgi.p2.XMLInputParser;
@@ -51,6 +52,9 @@ public class UpdateSiteLoader {
         }
         // then try the old update site
         UpdateSite site = loadSite(url);
+        if (site == null) {
+            return null;
+        }
         repo = loadFromDigest(site);
         if (repo != null) {
             return repo;
@@ -61,16 +65,33 @@ public class UpdateSiteLoader {
     private P2Descriptor loadP2(String url) throws IOException, 
ParseException, SAXException {
         P2Descriptor p2Descriptor = new P2Descriptor(
                 ExecutionEnvironmentProfileProvider.getInstance());
-
-        if (!readJarOrXml(url, "artifacts", new 
P2ArtifactParser(p2Descriptor))) {
+        if (!populateP2Descriptor(url, p2Descriptor)) {
             return null;
         }
+        return p2Descriptor;
+    }
 
-        if (!readJarOrXml(url, "content", new P2MetadataParser(p2Descriptor))) 
{
-            return null;
+    private boolean populateP2Descriptor(String url, P2Descriptor 
p2Descriptor) throws IOException,
+            ParseException, SAXException {
+        P2CompositeParser p2CompositeParser = new P2CompositeParser();
+
+        if (readJarOrXml(url, "compositeContent", p2CompositeParser)) {
+            Iterator itChildLocation = 
p2CompositeParser.getChildLocations().iterator();
+            boolean populated = false;
+            while (itChildLocation.hasNext()) {
+                String childLocation = (String) itChildLocation.next();
+                populated |= populateP2Descriptor(url + childLocation + "/", 
p2Descriptor);
+            }
+            return populated;
         }
 
-        return p2Descriptor;
+        if (!readJarOrXml(url, "artifacts", new 
P2ArtifactParser(p2Descriptor))) {
+            return false;
+        }
+        if (!readJarOrXml(url, "content", new P2MetadataParser(p2Descriptor))) 
{
+            return false;
+        }
+        return true;
     }
 
     private boolean readJarOrXml(String url, String baseName, XMLInputParser 
reader)
@@ -83,12 +104,12 @@ public class UpdateSiteLoader {
             // no jar file, try the xml one
             contentUrl = new URL(url + baseName + ".xml");
             res = new URLResource(contentUrl);
-            
+
             if (!res.exists()) {
                 // no xml either
                 return false;
             }
-            
+
             // we will then read directly from that input stream
             readIn = res.openStream();
         } else {
@@ -105,22 +126,27 @@ public class UpdateSiteLoader {
                 in.close();
                 throw e;
             }
-            
+
         }
-        
+
         try {
             reader.parse(readIn);
         } finally {
             readIn.close();
         }
-        
+
         return true;
     }
 
     private UpdateSite loadSite(String url) throws IOException, 
ParseException, SAXException {
         String siteUrl = normalizeSiteUrl(url, null);
         URL u = new URL(siteUrl + "site.xml");
-        InputStream in = URLHandlerRegistry.getDefault().openStream(u);
+        InputStream in;
+        try {
+            in = URLHandlerRegistry.getDefault().openStream(u);
+        } catch (IOException e) {
+            return null;
+        }
         try {
             UpdateSite site = EclipseUpdateSiteParser.parse(in);
             site.setUrl(normalizeSiteUrl(site.getUrl(), siteUrl));

Modified: 
ant/ivy/core/trunk/test/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoaderTest.java
URL: 
http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoaderTest.java?rev=1091577&r1=1091576&r2=1091577&view=diff
==============================================================================
--- 
ant/ivy/core/trunk/test/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoaderTest.java
 (original)
+++ 
ant/ivy/core/trunk/test/java/org/apache/ivy/osgi/updatesite/UpdateSiteLoaderTest.java
 Tue Apr 12 21:30:14 2011
@@ -19,9 +19,11 @@ package org.apache.ivy.osgi.updatesite;
 
 import java.io.IOException;
 import java.text.ParseException;
+import java.util.Iterator;
 
 import junit.framework.TestCase;
 
+import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
 import org.apache.ivy.osgi.repo.RepoDescriptor;
 import org.xml.sax.SAXException;
 
@@ -33,4 +35,16 @@ public class UpdateSiteLoaderTest extend
         assertEquals(13, site.getModules().size());
     }
 
+    public void testM2Eclipse() throws IOException, ParseException, 
SAXException {
+        UpdateSiteLoader loader = new UpdateSiteLoader();
+        RepoDescriptor site = 
loader.load("http://m2eclipse.sonatype.org/sites/m2e/";);
+        assertTrue(site.getModules().size() > 70);
+        Iterator itModules = site.getModules().iterator();
+        while (itModules.hasNext()) {
+            ModuleDescriptor md = (ModuleDescriptor) itModules.next();
+            String name = md.getModuleRevisionId().getName();
+            assertTrue(name, name.indexOf("org.maven") != -1);
+        }
+    }
+
 }


Reply via email to