Bug 490112 - Add unique id to p2 user agent information

Change-Id: Ib3bcbe28218cb33116e44d30590024c966fa2731
Signed-off-by: Pascal Rapicault <pascal@rapicorp.com>
diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java
index a5cbb50..728f4eb 100644
--- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java
+++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/runtime/InternalPlatform.java
@@ -84,7 +84,9 @@
 	public static final String PROP_PRODUCT = "eclipse.product"; //$NON-NLS-1$
 	public static final String PROP_WS = "osgi.ws"; //$NON-NLS-1$
 	public static final String PROP_ACTIVATE_PLUGINS = "eclipse.activateRuntimePlugins"; //$NON-NLS-1$
+	public static final String PROP_UUID = "eclipse.uuid"; //$NON-NLS-1$
 
+	private static final String UUID_PATH = "/.eclipse/eclipse.uuid"; //$NON-NLS-1$
 	private static final InternalPlatform singleton = new InternalPlatform();
 
 	private static final String[] WS_LIST = {Platform.WS_CARBON, Platform.WS_COCOA, Platform.WS_GTK, Platform.WS_MOTIF, Platform.WS_PHOTON, Platform.WS_WIN32, Platform.WS_WPF};
@@ -718,6 +720,54 @@
 			// activate Jobs plugin by creating a class from it:
 			org.eclipse.core.runtime.jobs.Job.getJobManager();
 		}
+		loadMachineUUID();
+	}
+
+	private void loadMachineUUID() {
+		EnvironmentInfo environment = environmentTracker.getService();
+		if (environment != null) {
+			if (environment.getProperty(PROP_UUID) != null)
+				return;
+		}
+		String uuid = loadExistingUUID();
+		if (uuid == null) {
+			uuid = UUID.randomUUID().toString();
+			saveUUID(uuid);
+		}
+		if (environment != null) {
+			environment.setProperty(PROP_UUID, uuid);
+		} else {
+			System.setProperty(PROP_UUID, uuid);
+		}
+	}
+
+	private void saveUUID(String uuid) {
+		File eclipseUUIDFile = new File(System.getProperty("user.home") + UUID_PATH); //$NON-NLS-1$
+		if (!eclipseUUIDFile.getParentFile().exists()) {
+			if (!eclipseUUIDFile.getParentFile().mkdirs())
+				return;
+		}
+		try (OutputStream os = new BufferedOutputStream(new FileOutputStream(eclipseUUIDFile))){
+			Properties prop = new Properties();
+			prop.setProperty(PROP_UUID, uuid);
+			prop.setProperty("version", "1"); //$NON-NLS-1$//$NON-NLS-2$
+			prop.store(os, ""); //$NON-NLS-1$
+		} catch (IOException e) {
+			return;
+		}
+	}
+
+	private String loadExistingUUID() {
+		File eclipseUUIDFile = new File(System.getProperty("user.home") + UUID_PATH); //$NON-NLS-1$
+		if (!eclipseUUIDFile.exists())
+			return null;
+		try(InputStream in = new BufferedInputStream(new FileInputStream(eclipseUUIDFile))) {
+			Properties prop = new Properties();
+			prop.load(in);
+			return (String) prop.get(PROP_UUID);
+		} catch (IOException e) {
+			return null;
+		}
 	}
 
 	/**