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