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

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