X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.repo%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Frepo%2Fosgi%2FArchiveWrapper.java;h=3355424c509bcdd3fedd9d4fa5ba18ac029f300e;hb=3b44f736ec89e7bad73a333786e1a64dff34b579;hp=832b790bf91f94724aff64d9e572a4955060e193;hpb=ac2c378ef483fd486ae215c8e7019c69a6188eb9;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java index 832b790bf..3355424c5 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArchiveWrapper.java @@ -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 wrappers = new HashMap(); + private SourcesProvider sourcesProvider; + // pattern of OSGi bundles to import private PathMatcher pathMatcher = new AntPathMatcher(); private Map includes = new HashMap(); @@ -113,7 +119,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution { if (baseName.endsWith("-sources")) { String bundle = baseName.substring(0, baseName.length() - "-sources".length()); - log.debug(name + "," + baseName + ", " + bundle); + // log.debug(name + "," + baseName + ", " + bundle); String bundlePath = FilenameUtils.getPath(name) + bundle + ".jar"; if (wrappers.containsKey(bundlePath)) { @@ -190,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 { @@ -205,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); @@ -218,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 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 +332,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution { this.mavenGroupIndexes = mavenGroupIndexes; } + public void setSourcesProvider(SourcesProvider sourcesProvider) { + this.sourcesProvider = sourcesProvider; + } + }