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

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