Bug 513883 - e4 Depency injection fails if not running in an OSGi
context

Signed-off-by: Olivier Croquette <ocroquette@free.fr>
Change-Id: I088d1722caef37e45b29510cd534d10f57d83d80
diff --git a/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/osgi/ProviderHelper.java b/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/osgi/ProviderHelper.java
index 2c65457..e318be4 100644
--- a/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/osgi/ProviderHelper.java
+++ b/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/osgi/ProviderHelper.java
@@ -19,6 +19,7 @@
 import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier;
 import org.eclipse.e4.core.di.suppliers.PrimaryObjectSupplier;
 import org.eclipse.e4.core.internal.di.shared.CoreLogger;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkUtil;
@@ -36,21 +37,24 @@
 	static protected Map<String, ExtendedObjectSupplier> extendedSuppliers = new HashMap<>();
 
 	static {
-		// in case if any extended object supplier changes, clear the supplier cache
-		BundleContext bundleContext = FrameworkUtil.getBundle(ProviderHelper.class).getBundleContext();
-		String filter = '(' + Constants.OBJECTCLASS + '=' + ExtendedObjectSupplier.SERVICE_NAME + ')';
-		try {
-			bundleContext.addServiceListener(new ServiceListener() {
-				@Override
-				public void serviceChanged(ServiceEvent event) {
-					synchronized (extendedSuppliers) {
-						extendedSuppliers.clear();
+		Bundle bundle = FrameworkUtil.getBundle(ProviderHelper.class);
+		if (bundle != null) { // In case we are not in an OSGi context, see bug 513883
+			// in case if any extended object supplier changes, clear the supplier cache
+			BundleContext bundleContext = bundle.getBundleContext();
+			String filter = '(' + Constants.OBJECTCLASS + '=' + ExtendedObjectSupplier.SERVICE_NAME + ')';
+			try {
+				bundleContext.addServiceListener(new ServiceListener() {
+					@Override
+					public void serviceChanged(ServiceEvent event) {
+						synchronized (extendedSuppliers) {
+							extendedSuppliers.clear();
+						}
 					}
-				}
-			}, filter);
-		} catch (InvalidSyntaxException e) {
-			// should not happen - we tested the line above
-			CoreLogger.logError("Invalid filter format in the provider helper", e); //$NON-NLS-1$
+				}, filter);
+			} catch (InvalidSyntaxException e) {
+				// should not happen - we tested the line above
+				CoreLogger.logError("Invalid filter format in the provider helper", e); //$NON-NLS-1$
+			}
 		}
 	}
 
@@ -58,7 +62,12 @@
 		synchronized (extendedSuppliers) {
 			if (extendedSuppliers.containsKey(qualifier))
 				return extendedSuppliers.get(qualifier);
-			BundleContext bundleContext = FrameworkUtil.getBundle(ProviderHelper.class).getBundleContext();
+			Bundle bundle = FrameworkUtil.getBundle(ProviderHelper.class);
+			if (bundle == null) {
+				// In case we are not in an OSGi context, see bug 513883
+				return null;
+			}
+			BundleContext bundleContext = bundle.getBundleContext();
 			try {
 				String filter = '(' + ExtendedObjectSupplier.SERVICE_CONTEXT_KEY + '=' + qualifier + ')';
 				ServiceReference<?>[] refs = bundleContext.getServiceReferences(ExtendedObjectSupplier.SERVICE_NAME, filter);
diff --git a/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionWithoutOSGITest.java b/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionWithoutOSGITest.java
new file mode 100644
index 0000000..03f4c35
--- /dev/null
+++ b/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/di/extensions/InjectionWithoutOSGITest.java
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 474274
+ ******************************************************************************/
+package org.eclipse.e4.core.internal.tests.di.extensions;
+
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.eclipse.e4.core.contexts.ContextInjectionFactory;
+import org.eclipse.e4.core.contexts.EclipseContextFactory;
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.junit.Test;
+import org.osgi.service.prefs.BackingStoreException;
+
+/**
+ * This test should be execute without an OSGI runtime running to verfiy BR
+ * 513883
+ * 
+ * @author jonas
+ *
+ */
+public class InjectionWithoutOSGITest {
+
+	static class InjectTarget {
+		public String other;
+
+		@Inject
+		public void setSth(@Named("testMixed") String otherString) {
+			other = otherString;
+		}
+	}
+
+	@Test
+	public void testPreferencesQualifier() throws BackingStoreException, InvocationTargetException, InstantiationException {
+		IEclipseContext context = EclipseContextFactory.create();
+		context.set("testMixed", "other");
+		InjectTarget target = ContextInjectionFactory.make(InjectTarget.class, context);
+
+		// test
+		assertEquals("other", target.other);
+
+		// change
+		context.set("testMixed", "bingo");
+
+		// re-test
+		// assertEquals("xyz", target.pref);
+		assertEquals("bingo", target.other);
+	}
+
+
+}