Bug 478221 - Remove ContextsActivator

The ContextsActivator was only responsible for locating a DebugOptions
instance, which was stored in a static field after loading and then
not used again. Replace with a service lookup. In addition, replace
a reference from the activator's BundleContext to a dynamically acquired
one.

Change-Id: I70195fd567875098b110d19a5e925bc1d66a45fb
Signed-off-by: Alex Blewitt <alex.blewitt@gmail.com>
diff --git a/bundles/org.eclipse.e4.core.contexts/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.core.contexts/META-INF/MANIFEST.MF
index 48305c8..0be52d6 100644
--- a/bundles/org.eclipse.e4.core.contexts/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.e4.core.contexts/META-INF/MANIFEST.MF
@@ -5,12 +5,11 @@
 Bundle-Name: %pluginName
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
-Bundle-Activator: org.eclipse.e4.core.internal.contexts.osgi.ContextsActivator
 Bundle-ActivationPolicy: lazy
-Require-Bundle: org.eclipse.e4.core.di,
- org.eclipse.osgi;bundle-version="3.6.0"
+Require-Bundle: org.eclipse.e4.core.di
 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
-Import-Package: javax.inject;version="1.0.0"
+Import-Package: javax.inject;version="1.0.0",
+ org.osgi.framework;version="1.5.0";resolution:=optional
 Export-Package: org.eclipse.e4.core.contexts,
- org.eclipse.e4.core.internal.contexts;x-friends:="org.eclipse.e4.core.tests,org.eclipse.e4.core.contexts.debug",
+ org.eclipse.e4.core.internal.contexts;x-friends:="org.eclipse.e4.core.tests",
  org.eclipse.e4.core.internal.contexts.osgi;x-internal:=true
diff --git a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/DebugHelper.java b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/DebugHelper.java
deleted file mode 100644
index 9226072..0000000
--- a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/DebugHelper.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2010 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.e4.core.internal.contexts;
-
-public final class DebugHelper {
-
-	private static final String PLUGIN_NAME = "org.eclipse.e4.core.contexts"; //$NON-NLS-1$
-	private static final String OPTION_DEBUG = PLUGIN_NAME + "/debug"; //$NON-NLS-1$
-	private static final String OPTION_DEBUG_NAMES = OPTION_DEBUG + "/names"; //$NON-NLS-1$
-	private static final String OPTION_DEBUG_LISTENERS = OPTION_DEBUG + "/listeners"; //$NON-NLS-1$
-
-	public static boolean DEBUG = false;
-	public static boolean DEBUG_NAMES = false;
-	public static boolean DEBUG_LISTENERS = false;
-
-	static {
-		try {
-			// use qualified name for ContextsActivator to ensure that it won't be loaded outside of this block
-			DEBUG = org.eclipse.e4.core.internal.contexts.osgi.ContextsActivator.getBooleanDebugOption(OPTION_DEBUG, false);
-			DEBUG_NAMES = org.eclipse.e4.core.internal.contexts.osgi.ContextsActivator.getBooleanDebugOption(OPTION_DEBUG_NAMES, false);
-			DEBUG_LISTENERS = org.eclipse.e4.core.internal.contexts.osgi.ContextsActivator.getBooleanDebugOption(OPTION_DEBUG_LISTENERS, false);
-		} catch (NoClassDefFoundError noClass) {
-			// no OSGi - OK
-		}
-	}
-
-}
diff --git a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/osgi/ContextDebugHelper.java b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/osgi/ContextDebugHelper.java
index e95a012..a703986 100644
--- a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/osgi/ContextDebugHelper.java
+++ b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/osgi/ContextDebugHelper.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010 IBM Corporation and others.
+ * Copyright (c) 2010, 2015 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -11,14 +11,18 @@
 package org.eclipse.e4.core.internal.contexts.osgi;
 
 import org.eclipse.e4.core.internal.contexts.IEclipseContextDebugger;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.ServiceReference;
 
 public class ContextDebugHelper {
 
-	static public IEclipseContextDebugger getDebugger() {
-		BundleContext bundleContext = ContextsActivator.getDefault().getBundleContext();
-		ServiceReference<IEclipseContextDebugger> ref = bundleContext.getServiceReference(IEclipseContextDebugger.class);
+	public static IEclipseContextDebugger getDebugger() {
+		Bundle bundle = FrameworkUtil.getBundle(ContextDebugHelper.class);
+		BundleContext bundleContext = bundle == null ? null : bundle.getBundleContext();
+		ServiceReference<IEclipseContextDebugger> ref = bundleContext == null ? null
+				: bundleContext.getServiceReference(IEclipseContextDebugger.class);
 		if (ref == null)
 			return null;
 		IEclipseContextDebugger contextDebugger = bundleContext.getService(ref);
diff --git a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/osgi/ContextsActivator.java b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/osgi/ContextsActivator.java
deleted file mode 100644
index 76d1821..0000000
--- a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/osgi/ContextsActivator.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*******************************************************************************
- *  Copyright (c) 2009, 2015 IBM Corporation and others.
- *  All rights reserved. This program and the accompanying materials
- *  are made available under the terms of the Eclipse Public License v1.0
- *  which accompanies this distribution, and is available at
- *  http://www.eclipse.org/legal/epl-v10.html
- *
- *  Contributors:
- *      IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.e4.core.internal.contexts.osgi;
-
-import org.eclipse.osgi.service.debug.DebugOptions;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.util.tracker.ServiceTracker;
-
-public class ContextsActivator implements BundleActivator {
-
-	static private ContextsActivator defaultInstance;
-	private BundleContext bundleContext;
-	private ServiceTracker<DebugOptions, DebugOptions> debugTracker = null;
-
-	public ContextsActivator() {
-		defaultInstance = this;
-	}
-
-	public static ContextsActivator getDefault() {
-		return defaultInstance;
-	}
-
-	@Override
-	public void start(BundleContext context) throws Exception {
-		bundleContext = context;
-	}
-
-	@Override
-	public void stop(BundleContext context) throws Exception {
-		if (debugTracker != null) {
-			debugTracker.close();
-			debugTracker = null;
-		}
-		bundleContext = null;
-	}
-
-	public BundleContext getBundleContext() {
-		return bundleContext;
-	}
-
-	public static boolean getBooleanDebugOption(String option, boolean defaultValue) {
-		BundleContext myBundleContext = getDefault().bundleContext;
-		if (myBundleContext == null)
-			return defaultValue;
-		if (getDefault().debugTracker == null) {
-			getDefault().debugTracker = new ServiceTracker<DebugOptions, DebugOptions>(getDefault().bundleContext, DebugOptions.class.getName(), null);
-			getDefault().debugTracker.open();
-		}
-		DebugOptions options = getDefault().debugTracker.getService();
-		if (options != null) {
-			String value = options.getOption(option);
-			if (value != null)
-				return value.equalsIgnoreCase("true"); //$NON-NLS-1$
-		}
-		return defaultValue;
-	}
-
-}