Blame SOURCES/eclipse-support-symlink-bundles.patch

dc388e
From aabcf5acff194b807c4d0bcf68425c3452c90339 Mon Sep 17 00:00:00 2001
dc388e
From: Roland Grunberg <rgrunber@redhat.com>
dc388e
Date: Fri, 12 Sep 2014 10:27:14 -0400
dc388e
Subject: [PATCH] Add support for regenerating bundle versions for symlinks.
dc388e
dc388e
When the version field in a bundle info file corresponds to a bundle
dc388e
whose location is a symbolic link, the correct version should be
dc388e
regenerated every time, in case a change has occured.
dc388e
dc388e
Change-Id: Ifbe8efed2218a8a1250fd1ac59f0cdd6bdd5f309
dc388e
---
dc388e
 .../META-INF/MANIFEST.MF                           |   1 +
dc388e
 .../utils/SimpleConfiguratorUtils.java             | 106 ++++++++++++++++++++-
dc388e
 2 files changed, 106 insertions(+), 1 deletion(-)
dc388e
dc388e
diff --git rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/META-INF/MANIFEST.MF rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/META-INF/MANIFEST.MF
dc388e
index d88d0a6..07fe087 100644
dc388e
--- rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/META-INF/MANIFEST.MF
dc388e
+++ rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/META-INF/MANIFEST.MF
dc388e
@@ -9,6 +9,7 @@ Bundle-Activator: org.eclipse.equinox.internal.simpleconfigurator.Activator
dc388e
 Bundle-ActivationPolicy: lazy
dc388e
 Import-Package: org.eclipse.osgi.framework.console;version="1.0.0";resolution:=optional,
dc388e
  org.eclipse.osgi.service.datalocation;version="1.0.0";resolution:=optional,
dc388e
+ org.eclipse.osgi.util;version="1.1.0",
dc388e
  org.osgi.framework;version="1.3.0",
dc388e
  org.osgi.framework.namespace;version="1.0.0",
dc388e
  org.osgi.framework.startlevel;version="1.0.0",
dc388e
diff --git rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/src/org/eclipse/equinox/internal/simpleconfigurator/utils/SimpleConfiguratorUtils.java rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/src/org/eclipse/equinox/internal/simpleconfigurator/utils/SimpleConfiguratorUtils.java
dc388e
index ab69b88..d6bf121 100644
dc388e
--- rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/src/org/eclipse/equinox/internal/simpleconfigurator/utils/SimpleConfiguratorUtils.java
dc388e
+++ rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator/src/org/eclipse/equinox/internal/simpleconfigurator/utils/SimpleConfiguratorUtils.java
dc388e
@@ -20,8 +20,12 @@
dc388e
 import java.nio.file.Files;
dc388e
 import java.nio.file.attribute.FileTime;
dc388e
 import java.util.*;
dc388e
+import java.util.jar.JarFile;
dc388e
+import java.util.zip.ZipEntry;
dc388e
+import java.util.zip.ZipFile;
dc388e
 import org.eclipse.equinox.internal.simpleconfigurator.Activator;
dc388e
-import org.osgi.framework.Version;
dc388e
+import org.eclipse.osgi.util.ManifestElement;
dc388e
+import org.osgi.framework.*;
dc388e
 
dc388e
 public class SimpleConfiguratorUtils {
dc388e
 
dc388e
@@ -282,6 +286,16 @@
dc388e
 		String symbolicName = tok.nextToken().trim();
dc388e
 		String version = tok.nextToken().trim();
dc388e
 		URI location = parseLocation(tok.nextToken().trim());
dc388e
+		if (base != null) {
dc388e
+			URI absLoc = URIUtil.append(base, location.toString());
dc388e
+			java.nio.file.Path absPath = java.nio.file.Paths.get(absLoc);
dc388e
+			// Symbolic links may change outside Eclipse so regenerate proper bundle version.
dc388e
+			if (Files.isSymbolicLink(absPath) && absPath.toFile().isFile()) {
dc388e
+				// We can't depend on org.eclipse.equinox.internal.frameworkadmin.utils.Utils
dc388e
+				Dictionary<String, String> manifest = getOSGiManifest(absLoc);
dc388e
+				version = manifest.get(Constants.BUNDLE_VERSION);
dc388e
+			}
dc388e
+		}
dc388e
 		int startLevel = Integer.parseInt(tok.nextToken().trim());
dc388e
 		boolean markedAsStarted = Boolean.parseBoolean(tok.nextToken());
dc388e
 		BundleInfo result = new BundleInfo(symbolicName, version, location, startLevel, markedAsStarted);
dc388e
@@ -420,4 +434,93 @@
dc388e
 		}
dc388e
 		return lastModified;
dc388e
 	}
dc388e
+
dc388e
+	private static Dictionary<String, String> getOSGiManifest(URI location) {
dc388e
+		if (location == null)
dc388e
+			return null;
dc388e
+		// if we have a file-based URL that doesn't end in ".jar" then...
dc388e
+		if (FILE_SCHEME.equals(location.getScheme()))
dc388e
+			return basicLoadManifest(URIUtil.toFile(location));
dc388e
+
dc388e
+		try {
dc388e
+			URL url = new URL("jar:" + location.toString() + "!/"); //$NON-NLS-1$//$NON-NLS-2$
dc388e
+			JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
dc388e
+			ZipFile jar = jarConnection.getJarFile();
dc388e
+
dc388e
+			try {
dc388e
+				ZipEntry entry = jar.getEntry(JarFile.MANIFEST_NAME);
dc388e
+				if (entry == null)
dc388e
+					return null;
dc388e
+
dc388e
+				Map<String, String> manifest = ManifestElement.parseBundleManifest(jar.getInputStream(entry), null);
dc388e
+				return manifestToProperties(manifest);
dc388e
+			} catch (BundleException e) {
dc388e
+				return null;
dc388e
+			} finally {
dc388e
+				jar.close();
dc388e
+			}
dc388e
+		} catch (IOException e) {
dc388e
+			if (System.getProperty("osgi.debug") != null) { //$NON-NLS-1$
dc388e
+				System.err.println("location=" + location); //$NON-NLS-1$
dc388e
+				e.printStackTrace();
dc388e
+			}
dc388e
+		}
dc388e
+		return null;
dc388e
+	}
dc388e
+
dc388e
+	private static Dictionary<String, String> basicLoadManifest(File bundleLocation) {
dc388e
+		InputStream manifestStream = null;
dc388e
+		ZipFile jarFile = null;
dc388e
+		try {
dc388e
+			try {
dc388e
+				// Handle a JAR'd bundle
dc388e
+				if (bundleLocation.isFile()) {
dc388e
+					jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ);
dc388e
+					ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME);
dc388e
+					if (manifestEntry != null) {
dc388e
+						manifestStream = jarFile.getInputStream(manifestEntry);
dc388e
+					}
dc388e
+				} else {
dc388e
+					// we have a directory-based bundle
dc388e
+					File bundleManifestFile = new File(bundleLocation, JarFile.MANIFEST_NAME);
dc388e
+					if (bundleManifestFile.exists())
dc388e
+						manifestStream = new BufferedInputStream(new FileInputStream(new File(bundleLocation, JarFile.MANIFEST_NAME)));
dc388e
+				}
dc388e
+			} catch (IOException e) {
dc388e
+				//ignore
dc388e
+			}
dc388e
+
dc388e
+			try {
dc388e
+				Map<String, String> manifest = ManifestElement.parseBundleManifest(manifestStream, null);
dc388e
+				return manifestToProperties(manifest);
dc388e
+			} catch (IOException ioe) {
dc388e
+				return null;
dc388e
+			} catch (BundleException e) {
dc388e
+				return null;
dc388e
+			}
dc388e
+		} finally {
dc388e
+			try {
dc388e
+				if (manifestStream != null)
dc388e
+					manifestStream.close();
dc388e
+			} catch (IOException e1) {
dc388e
+				//Ignore
dc388e
+			}
dc388e
+			try {
dc388e
+				if (jarFile != null)
dc388e
+					jarFile.close();
dc388e
+			} catch (IOException e2) {
dc388e
+				//Ignore
dc388e
+			}
dc388e
+		}
dc388e
+	}
dc388e
+
dc388e
+	private static Dictionary<String, String> manifestToProperties(Map<String, String> d) {
dc388e
+		Iterator<String> iter = d.keySet().iterator();
dc388e
+		Dictionary<String, String> result = new Hashtable<String, String>();
dc388e
+		while (iter.hasNext()) {
dc388e
+			String key = iter.next();
dc388e
+			result.put(key, d.get(key));
dc388e
+		}
dc388e
+		return result;
dc388e
+	}
dc388e
 }