Manage archives sources
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 20 Jun 2014 14:33:14 +0000 (14:33 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 20 Jun 2014 14:33:14 +0000 (14:33 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@7078 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveSourcesProvider.java [new file with mode: 0644]
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/SourcesProvider.java [new file with mode: 0644]
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/UriWrapper.java

diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveSourcesProvider.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveSourcesProvider.java
new file mode 100644 (file)
index 0000000..73a8cd7
--- /dev/null
@@ -0,0 +1,91 @@
+package org.argeo.slc.repo.osgi;
+
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.jar.JarEntry;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.Session;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.repo.OsgiFactory;
+
+public class ArchiveSourcesProvider implements SourcesProvider {
+       private final static Log log = LogFactory
+                       .getLog(ArchiveSourcesProvider.class);
+
+       private OsgiFactory osgiFactory;
+       private String uri;
+       private String base;
+
+       @Override
+       public void writeSources(List<String> packages, ZipOutputStream zout) {
+               Session distSession = null;
+               ZipInputStream zin = null;
+               try {
+                       distSession = osgiFactory.openDistSession();
+
+                       if (log.isDebugEnabled())
+                               log.debug("Wrapping " + uri);
+
+                       Node distNode = osgiFactory.getDist(distSession, uri);
+                       zin = new ZipInputStream(distNode.getNode(Node.JCR_CONTENT)
+                                       .getProperty(Property.JCR_DATA).getBinary().getStream());
+
+                       // prepare
+                       Set<String> directories = new TreeSet<String>();
+                       for (String pkg : packages)
+                               if (!pkg.equals("META-INF"))
+                                       directories.add(base + pkg.replace('.', '/') + '/');
+
+                       ZipEntry zentry = null;
+                       entries: while ((zentry = zin.getNextEntry()) != null) {
+                               String name = zentry.getName();
+                               if (!name.startsWith(base))
+                                       continue entries;
+
+                               String dirPath = FilenameUtils.getPath(name);
+                               if (name.equals(dirPath))// directory
+                                       continue entries;
+
+                               if (directories.contains(dirPath)) {
+                                       String path = name.substring(base.length());
+                                       zout.putNextEntry(new JarEntry(path));
+                                       IOUtils.copy(zin, zout);
+                                       zin.closeEntry();
+                                       zout.closeEntry();
+                                       continue entries;
+                               }
+                       }
+               } catch (Exception e) {
+                       throw new SlcException("Cannot retrieve sources from " + uri, e);
+               } finally {
+                       IOUtils.closeQuietly(zin);
+                       JcrUtils.logoutQuietly(distSession);
+               }
+
+       }
+
+       public void setOsgiFactory(OsgiFactory osgiFactory) {
+               this.osgiFactory = osgiFactory;
+       }
+
+       public void setUri(String uri) {
+               this.uri = uri;
+       }
+
+       public void setBase(String base) {
+               this.base = base;
+       }
+
+}
index 832b790bf91f94724aff64d9e572a4955060e193..1ffcf5332d21f52ac383e47d7593bd4ef80ad50c 100644 (file)
@@ -2,6 +2,7 @@ package org.argeo.slc.repo.osgi;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -11,6 +12,7 @@ import java.util.Set;
 import java.util.TreeSet;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
 
 import javax.jcr.Node;
 import javax.jcr.Property;
@@ -36,6 +38,8 @@ import org.sonatype.aether.util.artifact.DefaultArtifact;
 import org.springframework.util.AntPathMatcher;
 import org.springframework.util.PathMatcher;
 
+import aQute.lib.osgi.Jar;
+
 /**
  * Download a software distribution and generates the related OSGi bundles from
  * the jars, or import them directly if they are already OSGi bundles and don't
@@ -53,6 +57,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
        /** Jars to wrap as OSGi bundles */
        private Map<String, BndWrapper> wrappers = new HashMap<String, BndWrapper>();
 
+       private SourcesProvider sourcesProvider;
+
        // pattern of OSGi bundles to import
        private PathMatcher pathMatcher = new AntPathMatcher();
        private Map<String, String> includes = new HashMap<String, String>();
@@ -205,6 +211,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                ByteArrayOutputStream out = null;
                ByteArrayInputStream in = null;
                Node newJarNode;
+               Jar jar = null;
                try {
                        out = new ByteArrayOutputStream((int) zentry.getSize());
                        in = new ByteArrayInputStream(sourceJarBytes);
@@ -218,10 +225,43 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                        if (log.isDebugEnabled())
                                log.debug("Wrapped jar " + zentry.getName() + " to "
                                                + newJarNode.getPath());
+
+                       // sources
+                       if (sourcesProvider != null) {
+                               IOUtils.closeQuietly(in);
+                               in = new ByteArrayInputStream(out.toByteArray());
+                               jar = new Jar(null, in);
+                               List<String> packages = jar.getPackages();
+
+                               IOUtils.closeQuietly(out);
+                               out = new ByteArrayOutputStream();
+                               sourcesProvider
+                                               .writeSources(packages, new ZipOutputStream(out));
+
+                               IOUtils.closeQuietly(in);
+                               in = new ByteArrayInputStream(out.toByteArray());
+                               byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
+                                               new DefaultNameVersion(wrapper));
+                               Artifact sourcesArtifact = new DefaultArtifact(
+                                               artifact.getGroupId(), artifact.getArtifactId()
+                                                               + ".source", "jar", artifact.getVersion());
+                               Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
+                                               javaSession.getRootNode(), sourcesArtifact, sourcesJar);
+                               sourcesJarNode.getSession().save();
+
+                               if (log.isDebugEnabled())
+                                       log.debug("Added sources " + sourcesArtifact
+                                                       + " for bundle " + artifact);
+                       }
+
                        return artifact;
+               } catch (IOException e) {
+                       throw new SlcException("Cannot open jar", e);
                } finally {
                        IOUtils.closeQuietly(in);
                        IOUtils.closeQuietly(out);
+                       if (jar != null)
+                               jar.close();
                }
        }
 
@@ -291,4 +331,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                this.mavenGroupIndexes = mavenGroupIndexes;
        }
 
+       public void setSourcesProvider(SourcesProvider sourcesProvider) {
+               this.sourcesProvider = sourcesProvider;
+       }
+
 }
diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/SourcesProvider.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/SourcesProvider.java
new file mode 100644 (file)
index 0000000..a0a20f6
--- /dev/null
@@ -0,0 +1,17 @@
+package org.argeo.slc.repo.osgi;
+
+import java.util.List;
+import java.util.zip.ZipOutputStream;
+
+/** Provides access to Java sources */
+public interface SourcesProvider {
+       /**
+        * Writes sources into a ZIP (or a JAR), under the same sirectory structure.
+        * 
+        * @param packages
+        *            the packages to import
+        * @param out
+        *            the ZIP or JAR to write to
+        */
+       public void writeSources(List<String> packages, ZipOutputStream zout);
+}
index 7cb9de6e28646b49e29d12f9b5c9e6949afc71a1..7d717381da7df253dcd9a94d94c434db724becb1 100644 (file)
@@ -5,7 +5,6 @@ import java.io.InputStream;
 
 import javax.jcr.Node;
 import javax.jcr.Property;
-import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 import org.apache.commons.logging.Log;
@@ -25,6 +24,8 @@ public class UriWrapper extends BndWrapper implements Runnable {
 
        private OsgiFactory osgiFactory;
 
+       // private SourcesProvider sourcesProvider;
+
        public UriWrapper() {
                setFactory(this);
        }
@@ -34,6 +35,7 @@ public class UriWrapper extends BndWrapper implements Runnable {
                Session javaSession = null;
                InputStream in;
                ByteArrayOutputStream out;
+               // Jar jar = null;
                try {
                        distSession = osgiFactory.openDistSession();
                        javaSession = osgiFactory.openJavaSession();
@@ -55,11 +57,41 @@ public class UriWrapper extends BndWrapper implements Runnable {
                        newJarNode.getSession().save();
                        if (log.isDebugEnabled())
                                log.debug("Wrapped " + uri + " to " + newJarNode.getPath());
-               } catch (RepositoryException e) {
-                       throw new SlcException("Cannot wrap Maven " + uri, e);
+
+                       // sources
+                       // if (sourcesProvider != null) {
+                       // IOUtils.closeQuietly(in);
+                       // in = new ByteArrayInputStream(out.toByteArray());
+                       // jar = new Jar(null, in);
+                       // List<String> packages = jar.getPackages();
+                       //
+                       // IOUtils.closeQuietly(out);
+                       // out = new ByteArrayOutputStream();
+                       // sourcesProvider
+                       // .writeSources(packages, new ZipOutputStream(out));
+                       //
+                       // IOUtils.closeQuietly(in);
+                       // in = new ByteArrayInputStream(out.toByteArray());
+                       // byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
+                       // new DefaultNameVersion(this));
+                       // Artifact sourcesArtifact = new DefaultArtifact(getArtifact()
+                       // .getGroupId(), getArtifact().getArtifactId()
+                       // + ".source", "jar", getArtifact().getVersion());
+                       // Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
+                       // javaSession.getRootNode(), sourcesArtifact, sourcesJar);
+                       // sourcesJarNode.getSession().save();
+                       //
+                       // if (log.isDebugEnabled())
+                       // log.debug("Added sources " + sourcesArtifact
+                       // + " for bundle " + getArtifact());
+                       // }
+               } catch (Exception e) {
+                       throw new SlcException("Cannot wrap URI " + uri, e);
                } finally {
                        JcrUtils.logoutQuietly(distSession);
                        JcrUtils.logoutQuietly(javaSession);
+                       // if (jar != null)
+                       // jar.close();
                }
        }