X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.repo%2Fsrc%2Forg%2Fargeo%2Fslc%2Frepo%2Fosgi%2FImportBundlesZip.java;fp=org.argeo.slc.repo%2Fsrc%2Forg%2Fargeo%2Fslc%2Frepo%2Fosgi%2FImportBundlesZip.java;h=0518bbf1ae0a13e307d91f058fcfe4d462b14814;hb=6fc94d69efe089414ac9e63bde3efab1cbf7b7ca;hp=0000000000000000000000000000000000000000;hpb=b36c62642bd0db11b3133b369cc026fd4b7a1ec6;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ImportBundlesZip.java b/org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ImportBundlesZip.java new file mode 100644 index 000000000..0518bbf1a --- /dev/null +++ b/org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ImportBundlesZip.java @@ -0,0 +1,149 @@ +package org.argeo.slc.repo.osgi; + +import java.io.ByteArrayInputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarInputStream; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import javax.jcr.Node; +import javax.jcr.Repository; +import javax.jcr.Session; + +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.NameVersion; +import org.argeo.slc.SlcException; +import org.argeo.slc.repo.ArtifactIndexer; +import org.argeo.slc.repo.JarFileIndexer; +import org.argeo.slc.repo.RepoUtils; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.DefaultArtifact; + +/** + * Import all bundles in a zip file (typically an Eclipse distribution) into the + * workspace. + * + * @deprecated Use {@link ArchiveWrapper} instead. + */ +@Deprecated +public class ImportBundlesZip implements Runnable { + private final static Log log = LogFactory.getLog(ImportBundlesZip.class); + private Repository repository; + private String workspace; + private String groupId; + private String artifactBasePath = "/"; + + private ArtifactIndexer artifactIndexer = new ArtifactIndexer(); + private JarFileIndexer jarFileIndexer = new JarFileIndexer(); + + private String zipFile; + + private List excludedBundles = new ArrayList(); + + public void run() { + ZipInputStream zipIn = null; + JarInputStream jarIn = null; + Session session = null; + try { + URL url = new URL(zipFile); + session = repository.login(workspace); + + // clear + // String groupPath = MavenConventionsUtils.groupPath( + // artifactBasePath, groupId); + // if (session.itemExists(groupPath)) { + // session.getNode(groupPath).remove(); + // session.save(); + // if (log.isDebugEnabled()) + // log.debug("Cleared " + groupPath); + // } + + zipIn = new ZipInputStream(url.openStream()); + ZipEntry zipEntry = null; + entries: while ((zipEntry = zipIn.getNextEntry()) != null) { + String entryName = zipEntry.getName(); + if (!entryName.endsWith(".jar") + || entryName.contains("feature")) + continue entries;// skip + byte[] jarBytes = IOUtils.toByteArray(zipIn); + zipIn.closeEntry(); + jarIn = new JarInputStream(new ByteArrayInputStream(jarBytes)); + Manifest manifest = jarIn.getManifest(); + IOUtils.closeQuietly(jarIn); + if (manifest == null) { + log.warn(entryName + " has no MANIFEST"); + continue entries; + } + NameVersion nv; + try { + nv = RepoUtils.readNameVersion(manifest); + } catch (Exception e) { + log.warn("Cannot read name version from " + entryName, e); + continue entries; + } + + String bundleName = RepoUtils + .extractBundleNameFromSourceName(nv.getName()); + // skip excluded bundles and their sources + if (excludedBundles.contains(bundleName)) + continue entries; + // for(String excludedBundle:excludedBundles){ + // if(bundleName.contains(excludedBundle)) + // continue entries; + // } + + Artifact artifact = new DefaultArtifact(groupId, nv.getName(), + "jar", nv.getVersion()); + Node artifactNode = RepoUtils.copyBytesAsArtifact( + session.getNode(artifactBasePath), artifact, jarBytes); + jarBytes = null;// superstition, in order to free memory + + // indexes + artifactIndexer.index(artifactNode); + jarFileIndexer.index(artifactNode); + session.save(); + if (log.isDebugEnabled()) + log.debug("Imported " + entryName + " to " + artifactNode); + } + } catch (Exception e) { + throw new SlcException("Cannot import zip " + zipFile + " to " + + workspace, e); + } finally { + IOUtils.closeQuietly(zipIn); + IOUtils.closeQuietly(jarIn); + JcrUtils.logoutQuietly(session); + } + + } + + public void setRepository(Repository repository) { + this.repository = repository; + } + + public void setWorkspace(String workspace) { + this.workspace = workspace; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public void setArtifactBasePath(String artifactBasePath) { + this.artifactBasePath = artifactBasePath; + } + + public void setZipFile(String zipFile) { + this.zipFile = zipFile; + } + + public void setExcludedBundles(List excludedBundles) { + this.excludedBundles = excludedBundles; + } + +}