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

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