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=9088e2803515740fd87304f49ee0e38ab139542e;hb=825d60c5348dbe3f5be25b0bccf7bdebfe694219;hp=0000000000000000000000000000000000000000;hpb=5e991fff5cba01858dcc5747a27e637325bc5c8e;p=gpl%2Fargeo-jcr.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 0000000..9088e28 --- /dev/null +++ b/org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ArchiveSourcesProvider.java @@ -0,0 +1,89 @@ +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.argeo.api.cms.CmsLog; +import org.argeo.jcr.JcrUtils; +import org.argeo.slc.SlcException; +import org.argeo.slc.repo.OsgiFactory; + +public class ArchiveSourcesProvider implements SourcesProvider { + private final static CmsLog log = CmsLog.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; + } + +}