]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java
Incremental improvements.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / ArchiveWrapper.java
index 0adfe883f6d95ee7036dfa138c7d6da73d3061a9..3355424c509bcdd3fedd9d4fa5ba18ac029f300e 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,21 +12,25 @@ 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;
 import javax.jcr.RepositoryException;
 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.DefaultNameVersion;
 import org.argeo.slc.ModuleSet;
 import org.argeo.slc.NameVersion;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.aether.ArtifactIdComparator;
 import org.argeo.slc.build.Distribution;
+import org.argeo.slc.build.License;
 import org.argeo.slc.repo.OsgiFactory;
 import org.argeo.slc.repo.RepoUtils;
 import org.sonatype.aether.artifact.Artifact;
@@ -33,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
@@ -43,12 +50,15 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
 
        private OsgiFactory osgiFactory;
        private String version;
+       private License license;
 
        private String uri;
 
-       // jars to wrap as OSGi bundles
+       /** 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>();
@@ -57,11 +67,13 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
        private Boolean mavenGroupIndexes = false;
 
        public void init() {
-               if (version != null)
-                       for (BndWrapper wrapper : wrappers.values()) {
-                               if (wrapper.getVersion() == null)
-                                       wrapper.setVersion(version);
-                       }
+               for (BndWrapper wrapper : wrappers.values()) {
+                       wrapper.setFactory(this);
+                       if (version != null && wrapper.getVersion() == null)
+                               wrapper.setVersion(version);
+                       if (license != null && wrapper.getLicense() == null)
+                               wrapper.setLicense(license);
+               }
        }
 
        public void destroy() {
@@ -101,6 +113,34 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                        ZipEntry zentry = null;
                        entries: while ((zentry = zin.getNextEntry()) != null) {
                                String name = zentry.getName();
+
+                               // sources autodetect
+                               String baseName = FilenameUtils.getBaseName(name);
+                               if (baseName.endsWith("-sources")) {
+                                       String bundle = baseName.substring(0, baseName.length()
+                                                       - "-sources".length());
+                                       // log.debug(name + "," + baseName + ", " + bundle);
+                                       String bundlePath = FilenameUtils.getPath(name) + bundle
+                                                       + ".jar";
+                                       if (wrappers.containsKey(bundlePath)) {
+                                               BndWrapper wrapper = wrappers.get(bundlePath);
+                                               NameVersion bundleNv = new DefaultNameVersion(
+                                                               wrapper.getName(), wrapper.getVersion());
+                                               byte[] pdeSource = RepoUtils.packageAsPdeSource(zin,
+                                                               bundleNv);
+                                               Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(
+                                                               javaSession.getRootNode(),
+                                                               new DefaultArtifact(wrapper.getCategory(),
+                                                                               wrapper.getName() + ".source", "jar",
+                                                                               wrapper.getVersion()), pdeSource);
+                                               osgiFactory.indexNode(pdeSourceNode);
+                                               pdeSourceNode.getSession().save();
+                                       }
+                               } else if (baseName.endsWith(".source")) {
+                                       // TODO Eclipse source already available
+                               }
+
+                               // binaries
                                if (wrappers.containsKey(name)) {
                                        BndWrapper wrapper = (BndWrapper) wrappers.get(name);
                                        // we must copy since the stream is closed by BND
@@ -156,6 +196,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                }
                        }
 
+                       // FIXME Fail if not all wrappers matched
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap distribution " + uri, e);
                } finally {
@@ -171,6 +212,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);
@@ -184,10 +226,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();
                }
        }
 
@@ -237,6 +312,10 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                this.version = version;
        }
 
+       public void setLicense(License license) {
+               this.license = license;
+       }
+
        public void setPathMatcher(PathMatcher pathMatcher) {
                this.pathMatcher = pathMatcher;
        }
@@ -253,4 +332,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                this.mavenGroupIndexes = mavenGroupIndexes;
        }
 
+       public void setSourcesProvider(SourcesProvider sourcesProvider) {
+               this.sourcesProvider = sourcesProvider;
+       }
+
 }