X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.repo%2Fsrc%2Forg%2Fargeo%2Fslc%2Frepo%2Fosgi%2FArchiveSourcesProvider.java;fp=org.argeo.slc.repo%2Fsrc%2Forg%2Fargeo%2Fslc%2Frepo%2Fosgi%2FArchiveSourcesProvider.java;h=ab52c70691af3a6a45c203d3b666f5b0d74ff7b6;hb=b9505fef5ba8186433e903e9de3c73c17bdf6562;hp=0000000000000000000000000000000000000000;hpb=04ef2e4533e909122a560a5cb6499fa62bac82ec;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ArchiveSourcesProvider.java b/org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ArchiveSourcesProvider.java new file mode 100644 index 000000000..ab52c7069 --- /dev/null +++ b/org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ArchiveSourcesProvider.java @@ -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 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 directories = new TreeSet(); + 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; + } + +}