Import bundle zip (Eclipse)
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 21 May 2012 10:23:03 +0000 (10:23 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 21 May 2012 10:23:03 +0000 (10:23 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@5308 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoUtils.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/Migration_01_03.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ImportBundlesZip.java [new file with mode: 0644]

index df8a01daa956db7507cbdd184107e8ff430d02ed..ff7b13d8faaa45f0ba50e4f83435ace8eb7563f5 100644 (file)
@@ -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() {
        }
 }
index 325d352b0fecf2557f7cc399b150f6f33c43c911..8b15eaea292a4c240722fc156028d428cf9188c0 100644 (file)
@@ -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<String> excludedBundles = new ArrayList<String>();
        private Map<String, String> symbolicNamesMapping = new HashMap<String, String>();
@@ -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 (file)
index 0000000..8701688
--- /dev/null
@@ -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<String> excludedBundles = new ArrayList<String>();
+
+       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<String> excludedBundles) {
+               this.excludedBundles = excludedBundles;
+       }
+
+}