From: Mathieu Baudier Date: Mon, 21 May 2012 10:23:03 +0000 (+0000) Subject: Import bundle zip (Eclipse) X-Git-Tag: argeo-slc-2.1.7~724 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=ece297b709bf55a514b471e1141dcd22fcdc54c6;p=gpl%2Fargeo-slc.git Import bundle zip (Eclipse) git-svn-id: https://svn.argeo.org/slc/trunk@5308 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoUtils.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoUtils.java index df8a01daa..ff7b13d8f 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoUtils.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoUtils.java @@ -270,6 +270,10 @@ public class RepoUtils implements SlcNames { } } + /** + * Copy this bytes array as an artifact, relative to the root of the + * repository (typically the workspace root node) + */ public static Node copyBytesAsArtifact(Node artifactsBase, Artifact artifact, byte[] bytes) throws RepositoryException { String parentPath = MavenConventionsUtils.artifactParentPath( @@ -279,7 +283,7 @@ public class RepoUtils implements SlcNames { return JcrUtils.copyBytesAsFile(folderNode, MavenConventionsUtils.artifactFileName(artifact), bytes); } - + private RepoUtils() { } } diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/Migration_01_03.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/Migration_01_03.java index 325d352b0..8b15eaea2 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/Migration_01_03.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/Migration_01_03.java @@ -2,7 +2,6 @@ package org.argeo.slc.repo.maven; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -55,7 +54,6 @@ public class Migration_01_03 implements Runnable, SlcNames { private Repository repository; private String sourceWorkspace; private String targetWorkspace; - private String osgiProfile = "JavaSE-1.6.profile"; private List excludedBundles = new ArrayList(); private Map symbolicNamesMapping = new HashMap(); @@ -126,9 +124,11 @@ public class Migration_01_03 implements Runnable, SlcNames { Artifact origArtifact = RepoUtils.asArtifact(origArtifactNode); // skip eclipse artifacts - if ((origArtifact.getGroupId().startsWith("org.eclipse") && !origArtifact - .getArtifactId().equals("org.eclipse.osgi")) - || (origArtifact.getArtifactId().startsWith("org.polymap"))) { + if ((origArtifact.getGroupId().startsWith("org.eclipse") && !(origArtifact + .getArtifactId().equals("org.eclipse.osgi") || origArtifact + .getArtifactId().equals("org.eclipse.osgi.source"))) + || origArtifact.getArtifactId().startsWith("org.polymap") + || origArtifact.getArtifactId().startsWith("com.ibm.icu")) { if (log.isDebugEnabled()) log.debug("Skip " + origArtifact); return; diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ImportBundlesZip.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ImportBundlesZip.java new file mode 100644 index 000000000..8701688d8 --- /dev/null +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ImportBundlesZip.java @@ -0,0 +1,145 @@ +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.argeo.slc.repo.maven.MavenConventionsUtils; +import org.sonatype.aether.artifact.Artifact; +import org.sonatype.aether.util.artifact.DefaultArtifact; + +/** + * Import all bundles in a zip file (typically an Eclipse distribution) into the + * workspace. + */ +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 = RepoUtils.readNameVersion(manifest); + + // skip excluded bundles and their sources + if (excludedBundles.contains(extractBundleNameFromSourceName(nv + .getName()))) + 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); + } + + } + + /** If a source return the base bundle name, does not change otherwise */ + private String extractBundleNameFromSourceName(String sourceBundleName) { + if (sourceBundleName.endsWith(".source")) + return sourceBundleName.substring(0, sourceBundleName.length() + - ".source".length()); + else + return sourceBundleName; + } + + 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; + } + +}