Bug 579072 - FileReader.getProperty access BundleContext without null
check

Change-Id: I879482b63639b136758878636c16cbf91e4703f3
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
Reviewed-on: https://git.eclipse.org/r/c/equinox/rt.equinox.p2/+/191411
Tested-by: Equinox Bot <equinox-bot@eclipse.org>
Tested-by: Ed Merks <ed.merks@gmail.com>
Reviewed-by: Mickael Istria <mistria@redhat.com>
Reviewed-by: Ed Merks <ed.merks@gmail.com>
diff --git a/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/Activator.java b/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/Activator.java
index d0e8b4e..7b6649c 100644
--- a/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/Activator.java
+++ b/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/Activator.java
@@ -16,6 +16,7 @@
  ******************************************************************************/
 package org.eclipse.equinox.internal.p2.transport.ecf;
 
+import java.util.Optional;
 import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransferFactory;
 import org.eclipse.ecf.provider.filetransfer.IFileTransferProtocolToFactoryMapper;
 import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
@@ -173,4 +174,18 @@
 		return false;
 	}
 
+	public static String getProperty(String key) {
+		if (context != null) {
+			return context.getProperty(key);
+		}
+		return System.getProperty(key);
+	}
+
+	public static Optional<Version> getVersion() {
+		return Optional.ofNullable(context) //
+				.map(BundleContext::getBundle) //
+				.or(() -> Optional.ofNullable(FrameworkUtil.getBundle(FileReader.class)))//
+				.map(Bundle::getVersion);
+	}
+
 }
diff --git a/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/FileReader.java b/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/FileReader.java
index 97c7e2f..7d95289 100644
--- a/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/FileReader.java
+++ b/bundles/org.eclipse.equinox.p2.transport.ecf/src/org/eclipse/equinox/internal/p2/transport/ecf/FileReader.java
@@ -34,7 +34,7 @@
 import org.eclipse.equinox.internal.p2.repository.Messages;
 import org.eclipse.equinox.p2.core.IProvisioningAgent;
 import org.eclipse.osgi.util.NLS;
-import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.Version;
 
 /**
  * FileReader is an ECF FileTransferJob implementation.
@@ -64,11 +64,11 @@
 	static Map<String, Map<String, String>> options;
 
 	static private String getProperty(String key, String defaultValue) {
-		String value = FrameworkUtil.getBundle(FileReader.class).getBundleContext().getProperty(key);
-		if (value == null) {
-			value = defaultValue;
+		String value = Activator.getProperty(key);
+		if (value != null) {
+			return value;
 		}
-		return value;
+		return defaultValue;
 	}
 
 	static {
@@ -80,7 +80,7 @@
 		String osgiArch = getProperty("org.osgi.framework.processor", "unknownArch");//$NON-NLS-1$//$NON-NLS-2$
 		String language = getProperty("osgi.nl", "unknownLanguage");//$NON-NLS-1$//$NON-NLS-2$
 		String osVersion = getProperty("org.osgi.framework.os.version", "unknownOSVersion"); //$NON-NLS-1$ //$NON-NLS-2$
-		String p2Version = FrameworkUtil.getBundle(FileReader.class).getVersion().toString();
+		String p2Version = Activator.getVersion().map(Version::toString).orElse("unknownVersion"); //$NON-NLS-1$
 		userAgent = "p2/" + p2Version + " (Java " + javaSpec + ' ' + javaVendor + "; " + osName + ' ' + osVersion + ' ' //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 				+ osgiArch + "; " + language + ") "; //$NON-NLS-1$ //$NON-NLS-2$
 		String userAgentProvided = getProperty("p2.userAgent", null); //$NON-NLS-1$