Bug 573131: Make bundlepool installations relocatable

Contributed by STMicroelectronics

Change-Id: I399899441fd926ad4bf89eb64360bd3f213a7320
Signed-off-by: Torbjörn Svensson <torbjorn.svensson@st.com>
Reviewed-on: https://git.eclipse.org/r/c/equinox/rt.equinox.p2/+/179791
Reviewed-by: Mickael Istria <mistria@redhat.com>
Tested-by: Equinox Bot <equinox-bot@eclipse.org>
diff --git a/bundles/org.eclipse.equinox.frameworkadmin.equinox/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.frameworkadmin.equinox/META-INF/MANIFEST.MF
index 4a87a87..655b135 100644
--- a/bundles/org.eclipse.equinox.frameworkadmin.equinox/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.frameworkadmin.equinox/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.equinox.frameworkadmin.equinox;singleton:=true
-Bundle-Version: 1.2.0.qualifier
+Bundle-Version: 1.2.100.qualifier
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
 Import-Package: org.eclipse.equinox.frameworkadmin;version="[2.0.0,3.0.0)",
diff --git a/bundles/org.eclipse.equinox.frameworkadmin.equinox/pom.xml b/bundles/org.eclipse.equinox.frameworkadmin.equinox/pom.xml
index b271efa..bb863c4 100644
--- a/bundles/org.eclipse.equinox.frameworkadmin.equinox/pom.xml
+++ b/bundles/org.eclipse.equinox.frameworkadmin.equinox/pom.xml
@@ -9,6 +9,6 @@
   </parent>
   <groupId>org.eclipse.equinox</groupId>
   <artifactId>org.eclipse.equinox.frameworkadmin.equinox</artifactId>
-  <version>1.2.0-SNAPSHOT</version>
+  <version>1.2.100-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/EclipseLauncherParser.java b/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/EclipseLauncherParser.java
index 1dd8181..829753a 100644
--- a/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/EclipseLauncherParser.java
+++ b/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/EclipseLauncherParser.java
@@ -34,7 +34,7 @@
 	//this figures out the location of the data area on partial data read from the <eclipse>.ini
 	private URI getOSGiInstallArea(List<String> lines, URI base, LauncherData launcherData) {
 		//does the eclipse.ini say anything for osgi.install.area?
-		File osgiInstallArea = ParserUtils.getOSGiInstallArea(lines, null, base);
+		File osgiInstallArea = ParserUtils.getOSGiInstallArea(lines, null, launcherData.getLauncher(), base);
 		if (osgiInstallArea == null) {
 			//otherwise use the launcherData to figure it out
 			osgiInstallArea = ParserUtils.getOSGiInstallArea(lines, null, launcherData);
@@ -49,12 +49,13 @@
 			ParserUtils.removeArgument(EquinoxConstants.OPTION_INSTALL, lines);
 			return;
 		}
+		File folder = ParserUtils.fromOSGiJarToOSGiInstallArea(launcherData.getFwJar().getAbsolutePath());
 		if (Constants.OS_MACOSX.equals(launcherData.getOS())) {
-			if (!new File(ParserUtils.fromOSGiJarToOSGiInstallArea(launcherData.getFwJar().getAbsolutePath()), "../MacOS").equals(launcherFolder)) {
-				ParserUtils.setValueForArgument(EquinoxConstants.OPTION_INSTALL, launcherFolder.getAbsolutePath().replace('\\', '/'), lines);
-			}
-		} else if (!ParserUtils.fromOSGiJarToOSGiInstallArea(launcherData.getFwJar().getAbsolutePath()).equals(launcherFolder)) {
-			ParserUtils.setValueForArgument(EquinoxConstants.OPTION_INSTALL, launcherFolder.getAbsolutePath().replace('\\', '/'), lines);
+			folder = new File(folder, "../MacOS");
+		}
+		if (!folder.equals(launcherFolder)) {
+			// Launcher will replace "@launcher.dir" with actual path. See bug 572890.
+			ParserUtils.setValueForArgument(EquinoxConstants.OPTION_INSTALL, "@launcher.dir/", lines);
 		}
 	}
 
diff --git a/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/ParserUtils.java b/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/ParserUtils.java
index e9a245a..cbe533a 100644
--- a/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/ParserUtils.java
+++ b/bundles/org.eclipse.equinox.frameworkadmin.equinox/src/org/eclipse/equinox/internal/frameworkadmin/equinox/ParserUtils.java
@@ -28,6 +28,7 @@
 
 public class ParserUtils {
 	private static final String FILE_PROTOCOL = "file:"; //$NON-NLS-1$
+	private static final String LAUNCHER_DIR = "@launcher.dir"; //$NON-NLS-1$
 
 	public static File getOSGiInstallArea(List<String> programArgs, Properties properties, LauncherData launcherData) {
 		if (launcherData == null)
@@ -38,7 +39,7 @@
 			base = launcherData.getLauncher().getParentFile().toURI();
 		else if (launcherData.getHome() != null)
 			base = launcherData.getHome().toURI();
-		File result = getOSGiInstallArea(programArgs, properties, base);
+		File result = getOSGiInstallArea(programArgs, properties, launcherData.getLauncher(), base);
 		if (result != null)
 			return result;
 
@@ -84,7 +85,7 @@
 	}
 
 	//This method should only be used to determine the osgi install area when reading the eclipse.ini
-	public static File getOSGiInstallArea(List<String> args, Properties properties, URI base) {
+	public static File getOSGiInstallArea(List<String> args, Properties properties, File launcherFile, URI base) {
 		if (args == null)
 			return null;
 		String install = getValueForArgument(EquinoxConstants.OPTION_INSTALL, args);
@@ -94,6 +95,8 @@
 		if (install != null) {
 			if (install.startsWith(FILE_PROTOCOL))
 				install = install.substring(FILE_PROTOCOL.length() + 1);
+			if (install.startsWith(LAUNCHER_DIR))
+				install = install.replaceAll(LAUNCHER_DIR, launcherFile.getParent().toString());
 			File installFile = new File(install);
 			if (installFile.isAbsolute())
 				return installFile;