]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java
SLC Unit
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / ArchiveWrapper.java
index 3355424c509bcdd3fedd9d4fa5ba18ac029f300e..29fa8f14d8dba8790cb631241307d924c3eaeb3e 100644 (file)
@@ -2,7 +2,7 @@ package org.argeo.slc.repo.osgi;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -10,6 +10,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.jar.JarInputStream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
@@ -24,6 +25,7 @@ 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.CategorizedNameVersion;
 import org.argeo.slc.DefaultNameVersion;
 import org.argeo.slc.ModuleSet;
 import org.argeo.slc.NameVersion;
@@ -85,7 +87,65 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
        }
 
        public Iterator<? extends NameVersion> nameVersions() {
-               return wrappers.values().iterator();
+               if (wrappers.size() > 0)
+                       return wrappers.values().iterator();
+               else
+                       return osgiNameVersions();
+       }
+
+       @SuppressWarnings("resource")
+       protected Iterator<? extends NameVersion> osgiNameVersions() {
+               List<CategorizedNameVersion> nvs = new ArrayList<CategorizedNameVersion>();
+
+               Session distSession = null;
+               ZipInputStream zin = null;
+               try {
+                       distSession = osgiFactory.openDistSession();
+
+                       Node distNode = osgiFactory.getDist(distSession, uri);
+                       zin = new ZipInputStream(distNode.getNode(Node.JCR_CONTENT)
+                                       .getProperty(Property.JCR_DATA).getBinary().getStream());
+
+                       ZipEntry zentry = null;
+                       entries: while ((zentry = zin.getNextEntry()) != null) {
+                               String name = zentry.getName();
+                               if (log.isTraceEnabled())
+                                       log.trace("Zip entry " + name);
+                               for (String exclude : excludes)
+                                       if (pathMatcher.match(exclude, name))
+                                               continue entries;
+
+                               for (String include : includes.keySet()) {
+                                       if (pathMatcher.match(include, name)) {
+                                               String groupId = includes.get(include);
+                                               JarInputStream jis = new JarInputStream(zin);
+                                               if (jis.getManifest() == null) {
+                                                       log.warn("No MANIFEST in entry " + name
+                                                                       + ", skipping...");
+                                                       continue entries;
+                                               }
+                                               NameVersion nv = RepoUtils.readNameVersion(jis
+                                                               .getManifest());
+                                               if (nv != null) {
+                                                       if (nv.getName().endsWith(".source"))
+                                                               continue entries;
+                                                       CategorizedNameVersion cnv = new OsgiCategorizedNV(
+                                                                       groupId, nv.getName(), nv.getVersion(),
+                                                                       this);
+                                                       nvs.add(cnv);
+                                                       // no need to process further includes
+                                                       continue entries;
+                                               }
+                                       }
+                               }
+                       }
+                       return nvs.iterator();
+               } catch (Exception e) {
+                       throw new SlcException("Cannot wrap distribution " + uri, e);
+               } finally {
+                       IOUtils.closeQuietly(zin);
+                       JcrUtils.logoutQuietly(distSession);
+               }
        }
 
        public void run() {
@@ -105,6 +165,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
 
                        if (log.isDebugEnabled())
                                log.debug("Wrapping " + uri);
+                       boolean nothingWasDone = true;
 
                        Node distNode = osgiFactory.getDist(distSession, uri);
                        zin = new ZipInputStream(distNode.getNode(Node.JCR_CONTENT)
@@ -128,25 +189,33 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                                                wrapper.getName(), wrapper.getVersion());
                                                byte[] pdeSource = RepoUtils.packageAsPdeSource(zin,
                                                                bundleNv);
+                                               Artifact sourcesArtifact = new DefaultArtifact(
+                                                               wrapper.getCategory(), wrapper.getName()
+                                                                               + ".source", "jar",
+                                                               wrapper.getVersion());
                                                Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(
-                                                               javaSession.getRootNode(),
-                                                               new DefaultArtifact(wrapper.getCategory(),
-                                                                               wrapper.getName() + ".source", "jar",
-                                                                               wrapper.getVersion()), pdeSource);
+                                                               javaSession.getRootNode(), sourcesArtifact,
+                                                               pdeSource);
                                                osgiFactory.indexNode(pdeSourceNode);
                                                pdeSourceNode.getSession().save();
+                                               if (log.isDebugEnabled())
+                                                       log.debug("Added sources " + sourcesArtifact
+                                                                       + " for bundle " + wrapper.getArtifact()
+                                                                       + "from " + name + " in binary archive.");
                                        }
-                               } else if (baseName.endsWith(".source")) {
-                                       // TODO Eclipse source already available
+
                                }
+                               // else if (baseName.endsWith(".source")) {
+                               // }
 
                                // binaries
                                if (wrappers.containsKey(name)) {
                                        BndWrapper wrapper = (BndWrapper) wrappers.get(name);
                                        // we must copy since the stream is closed by BND
-                                       byte[] sourceJarBytes = IOUtils.toByteArray(zin);
+                                       byte[] origJarBytes = IOUtils.toByteArray(zin);
                                        Artifact artifact = wrapZipEntry(javaSession, zentry,
-                                                       sourceJarBytes, wrapper);
+                                                       origJarBytes, wrapper);
+                                       nothingWasDone = false;
                                        addArtifactToIndex(binaries, wrapper.getGroupId(), artifact);
                                } else {
                                        for (String wrapperKey : wrappers.keySet())
@@ -155,9 +224,10 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                                        BndWrapper wrapper = (BndWrapper) wrappers
                                                                        .get(wrapperKey);
                                                        // we must copy since the stream is closed by BND
-                                                       byte[] sourceJarBytes = IOUtils.toByteArray(zin);
+                                                       byte[] origJarBytes = IOUtils.toByteArray(zin);
                                                        Artifact artifact = wrapZipEntry(javaSession,
-                                                                       zentry, sourceJarBytes, wrapper);
+                                                                       zentry, origJarBytes, wrapper);
+                                                       nothingWasDone = false;
                                                        addArtifactToIndex(binaries, wrapper.getGroupId(),
                                                                        artifact);
                                                        continue entries;
@@ -174,13 +244,20 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                        for (String include : includes.keySet()) {
                                                if (pathMatcher.match(include, name)) {
                                                        String groupId = includes.get(include);
-                                                       byte[] sourceJarBytes = IOUtils.toByteArray(zin);
+                                                       byte[] origJarBytes = IOUtils.toByteArray(zin);
                                                        Artifact artifact = importZipEntry(javaSession,
-                                                                       zentry, sourceJarBytes, groupId);
+                                                                       zentry, origJarBytes, groupId);
+                                                       if (artifact == null) {
+                                                               log.warn("Skipped non identified " + zentry);
+                                                               continue entries;
+                                                       }
+                                                       nothingWasDone = false;
                                                        if (artifact.getArtifactId().endsWith(".source"))
                                                                addArtifactToIndex(sources, groupId, artifact);
                                                        else
                                                                addArtifactToIndex(binaries, groupId, artifact);
+                                                       // no need to process this entry further
+                                                       continue entries;
                                                }
                                        }
                                }
@@ -196,6 +273,9 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                }
                        }
 
+                       if (nothingWasDone)
+                               throw new SlcException("Nothing was done");
+
                        // FIXME Fail if not all wrappers matched
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap distribution " + uri, e);
@@ -207,15 +287,14 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
        }
 
        protected Artifact wrapZipEntry(Session javaSession, ZipEntry zentry,
-                       byte[] sourceJarBytes, BndWrapper wrapper)
-                       throws RepositoryException {
+                       byte[] origJarBytes, BndWrapper wrapper) throws RepositoryException {
                ByteArrayOutputStream out = null;
                ByteArrayInputStream in = null;
                Node newJarNode;
                Jar jar = null;
                try {
                        out = new ByteArrayOutputStream((int) zentry.getSize());
-                       in = new ByteArrayInputStream(sourceJarBytes);
+                       in = new ByteArrayInputStream(origJarBytes);
                        wrapper.wrapJar(in, out);
 
                        Artifact artifact = wrapper.getArtifact();
@@ -227,37 +306,50 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                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);
-                       }
+                       if (sourcesProvider != null)
+                               addSource(javaSession, artifact, out.toByteArray());
 
                        return artifact;
-               } catch (IOException e) {
-                       throw new SlcException("Cannot open jar", e);
+               } finally {
+                       IOUtils.closeQuietly(in);
+                       IOUtils.closeQuietly(out);
+                       if (jar != null)
+                               jar.close();
+               }
+       }
+
+       protected void addSource(Session javaSession, Artifact artifact,
+                       byte[] binaryJarBytes) {
+               InputStream in = null;
+               ByteArrayOutputStream out = null;
+               Jar jar = null;
+               try {
+                       in = new ByteArrayInputStream(binaryJarBytes);
+                       jar = new Jar(null, in);
+                       List<String> packages = jar.getPackages();
+
+                       out = new ByteArrayOutputStream();
+                       sourcesProvider.writeSources(packages, new ZipOutputStream(out));
+
+                       IOUtils.closeQuietly(in);
+                       in = new ByteArrayInputStream(out.toByteArray());
+                       byte[] sourcesJar = RepoUtils.packageAsPdeSource(
+                                       in,
+                                       new DefaultNameVersion(artifact.getArtifactId(), artifact
+                                                       .getVersion()));
+                       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 + "from source provider " + sourcesProvider);
+               } catch (Exception e) {
+                       throw new SlcException("Cannot get sources for " + artifact, e);
                } finally {
                        IOUtils.closeQuietly(in);
                        IOUtils.closeQuietly(out);
@@ -267,21 +359,29 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
        }
 
        protected Artifact importZipEntry(Session javaSession, ZipEntry zentry,
-                       byte[] sourceJarBytes, String groupId) throws RepositoryException {
+                       byte[] binaryJarBytes, String groupId) throws RepositoryException {
                ByteArrayInputStream in = null;
                Node newJarNode;
                try {
-                       in = new ByteArrayInputStream(sourceJarBytes);
+                       in = new ByteArrayInputStream(binaryJarBytes);
                        NameVersion nameVersion = RepoUtils.readNameVersion(in);
+                       if (nameVersion == null) {
+                               log.warn("Cannot identify " + zentry.getName());
+                               return null;
+                       }
                        Artifact artifact = new DefaultArtifact(groupId,
                                        nameVersion.getName(), "jar", nameVersion.getVersion());
                        newJarNode = RepoUtils.copyBytesAsArtifact(
-                                       javaSession.getRootNode(), artifact, sourceJarBytes);
+                                       javaSession.getRootNode(), artifact, binaryJarBytes);
                        osgiFactory.indexNode(newJarNode);
                        newJarNode.getSession().save();
                        if (log.isDebugEnabled())
                                log.debug("Imported OSGi bundle " + zentry.getName() + " to "
                                                + newJarNode.getPath());
+
+                       if (sourcesProvider != null)
+                               addSource(javaSession, artifact, binaryJarBytes);
+
                        return artifact;
                } finally {
                        IOUtils.closeQuietly(in);