Bug 513690 - IEclipseContext disposal not communicated to OSGi layer

Change-Id: Ia971b7556ed5e72e38c4d8c95aa27f1aea48f20a
Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.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 65f1a0a..6796dce 100644
--- a/bundles/org.eclipse.e4.core.contexts/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.e4.core.contexts/META-INF/MANIFEST.MF
@@ -1,7 +1,7 @@
 Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-SymbolicName: org.eclipse.e4.core.contexts
-Bundle-Version: 1.5.0.qualifier
+Bundle-Version: 1.6.0.qualifier
 Bundle-Name: %pluginName
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
@@ -9,7 +9,8 @@
 Require-Bundle: org.eclipse.e4.core.di
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 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.osgi.framework;version="1.5.0";resolution:=optional,
+ org.osgi.service.event;version="1.3.0"
+Export-Package: org.eclipse.e4.core.contexts;version="1.6.0",
  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/pom.xml b/bundles/org.eclipse.e4.core.contexts/pom.xml
index c7d507c..159f758 100644
--- a/bundles/org.eclipse.e4.core.contexts/pom.xml
+++ b/bundles/org.eclipse.e4.core.contexts/pom.xml
@@ -19,6 +19,6 @@
   </parent>
   <groupId>org.eclipse.e4</groupId>
   <artifactId>org.eclipse.e4.core.contexts</artifactId>
-  <version>1.5.0-SNAPSHOT</version>
+  <version>1.6.0-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/IEclipseContext.java b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/IEclipseContext.java
index 434658b..a2dae65 100644
--- a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/IEclipseContext.java
+++ b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/IEclipseContext.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2013 IBM Corporation and others.
+ * Copyright (c) 2009, 2017 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
@@ -45,10 +45,21 @@
 public interface IEclipseContext {
 
 	/**
-	 * Returns whether this context or a parent has a value stored for the given name.
-	 * @param name the name being queried
-	 * @return <code>true</code> if this context has a value for the given name, and
-	 *         <code>false</code> otherwise.
+	 * Topic for sending an event via EventAdmin to inform about the disposal of
+	 * an IEclipseContext.
+	 *
+	 * @since 1.6
+	 */
+	public static final String TOPIC_DISPOSE = "org/eclipse/e4/core/contexts/IEclipseContext/DISPOSE"; //$NON-NLS-1$
+
+	/**
+	 * Returns whether this context or a parent has a value stored for the given
+	 * name.
+	 *
+	 * @param name
+	 *            the name being queried
+	 * @return <code>true</code> if this context has a value for the given name,
+	 *         and <code>false</code> otherwise.
 	 */
 	public boolean containsKey(String name);
 
diff --git a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java
index ba11dcb..2addd36 100644
--- a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java
+++ b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java
@@ -30,6 +30,8 @@
 import org.eclipse.e4.core.contexts.RunAndTrack;
 import org.eclipse.e4.core.di.IInjector;
 import org.eclipse.e4.core.internal.contexts.osgi.ContextDebugHelper;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 
 /**
  * This implementation assumes that all contexts are of the class EclipseContext. The external
@@ -202,8 +204,18 @@
 
 		if (parent != null) {
 			parent.removeChild(this);
-			if (rootContext != null)
+			if (rootContext != null) {
 				rootContext.cleanup();
+			}
+
+			// inform the OSGi layer via EventAdmin about the context disposal
+			// used for example to be able to cleanup cached requestors in
+			// ExtendedObjectSupplier implementations
+			EventAdmin admin = parent.get(EventAdmin.class);
+			if (admin != null) {
+				Event osgiEvent = new Event(IEclipseContext.TOPIC_DISPOSE, (Map<String, ?>) null);
+				admin.postEvent(osgiEvent);
+			}
 		}
 
 		if (debugAddOn != null)