Bug 578365 - Projector accesses the Activators bundlecontext without
null check

- access activator static singelton in a safe way

Change-Id: Id4d54ed1a6c16dddbf283adb04dc0c6764acc38d
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
Reviewed-on: https://git.eclipse.org/r/c/equinox/rt.equinox.p2/+/189985
Reviewed-by: Mickael Istria <mistria@redhat.com>
Tested-by: Equinox Bot <equinox-bot@eclipse.org>
diff --git a/bundles/org.eclipse.equinox.p2.director/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.director/META-INF/MANIFEST.MF
index 79fb04c..8b2a73e 100644
--- a/bundles/org.eclipse.equinox.p2.director/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.p2.director/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.equinox.p2.director;singleton:=true
-Bundle-Version: 2.5.100.qualifier
+Bundle-Version: 2.5.200.qualifier
 Bundle-ClassPath: .
 Bundle-Activator: org.eclipse.equinox.internal.p2.director.DirectorActivator
 Bundle-Vendor: %providerName
diff --git a/bundles/org.eclipse.equinox.p2.director/pom.xml b/bundles/org.eclipse.equinox.p2.director/pom.xml
index 935a2cc..dddade6 100644
--- a/bundles/org.eclipse.equinox.p2.director/pom.xml
+++ b/bundles/org.eclipse.equinox.p2.director/pom.xml
@@ -9,6 +9,6 @@
   </parent>
   <groupId>org.eclipse.equinox</groupId>
   <artifactId>org.eclipse.equinox.p2.director</artifactId>
-  <version>2.5.100-SNAPSHOT</version>
+  <version>2.5.200-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/DirectorActivator.java b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/DirectorActivator.java
index 7d33193..a2960e3 100644
--- a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/DirectorActivator.java
+++ b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/DirectorActivator.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2017 IBM Corporation and others.
+ * Copyright (c) 2007, 2022 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -10,24 +10,26 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Christoph Läubrich - access activator static singelton in a safe way
  *******************************************************************************/
 package org.eclipse.equinox.internal.p2.director;
 
+import java.util.Optional;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 
 public class DirectorActivator implements BundleActivator {
 	public static final String PI_DIRECTOR = "org.eclipse.equinox.p2.director"; //$NON-NLS-1$
-	public static BundleContext context;
+	public static volatile Optional<BundleContext> context = Optional.empty();
 
 	@Override
 	public void start(BundleContext ctx) throws Exception {
-		context = ctx;
+		context = Optional.of(ctx);
 	}
 
 	@Override
 	public void stop(BundleContext ctx) throws Exception {
-		DirectorActivator.context = null;
+		DirectorActivator.context = Optional.empty();
 	}
 
 }
diff --git a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java
index ae30164..458a8fb 100644
--- a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java
+++ b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/p2/director/Projector.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2020 IBM Corporation and others.
+ * Copyright (c) 2007, 2022 IBM Corporation and others.
  *
  * This
  * program and the accompanying materials are made available under the terms of
@@ -17,6 +17,7 @@
  *   Sonatype, Inc. - ongoing development
  *   Rapicorp, Inc. - split the optimization function
  *   Red Hat, Inc. - support for remediation page
+ *   Christoph Läubrich - access activator static singelton in a safe way
  ******************************************************************************/
 package org.eclipse.equinox.internal.p2.director;
 
@@ -207,7 +208,8 @@
 				// allow the user to specify a longer timeout.
 				// only set the value if it is a positive integer larger than the default.
 				// see https://bugs.eclipse.org/336967
-				timeoutString = DirectorActivator.context.getProperty(PROP_PROJECTOR_TIMEOUT);
+				timeoutString = DirectorActivator.context.map(ctx -> ctx.getProperty(PROP_PROJECTOR_TIMEOUT))
+						.orElse(null);
 				if (timeoutString != null)
 					timeout = Math.max(timeout, Integer.parseInt(timeoutString));
 			} catch (Exception e) {
diff --git a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/provisional/p2/director/PlanExecutionHelper.java b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/provisional/p2/director/PlanExecutionHelper.java
index 4c51ca1..83ac500 100644
--- a/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/provisional/p2/director/PlanExecutionHelper.java
+++ b/bundles/org.eclipse.equinox.p2.director/src/org/eclipse/equinox/internal/provisional/p2/director/PlanExecutionHelper.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2015 IBM Corporation and others.
+ * Copyright (c) 2009, 2022 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -11,6 +11,7 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *     Red Hat Inc. - Bug 460967
+ *     Christoph Läubrich - access activator static singelton in a safe way
  *******************************************************************************/
 package org.eclipse.equinox.internal.provisional.p2.director;
 
@@ -36,7 +37,8 @@
 					.getService(IEngine.class).perform(result.getInstallerPlan(), phaseSet, progress);
 			if (!installerPlanStatus.isOK())
 				return installerPlanStatus;
-			Configurator configChanger = ServiceHelper.getService(DirectorActivator.context, Configurator.class);
+			Configurator configChanger = DirectorActivator.context
+					.map(ctx -> ServiceHelper.getService(ctx, Configurator.class)).orElse(null);
 			try {
 				configChanger.applyConfiguration();
 			} catch (IOException e) {