Bug 397703 - [region] bundle and service hooks throw IllegalStateException if finder context becomes invalid
diff --git a/bundles/org.eclipse.equinox.region/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.region/META-INF/MANIFEST.MF
index 5508b23..ae5f6d3 100644
--- a/bundles/org.eclipse.equinox.region/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.region/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %Bundle-Name
 Bundle-SymbolicName: org.eclipse.equinox.region
-Bundle-Version: 1.1.0.qualifier
+Bundle-Version: 1.1.1.qualifier
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Import-Package: javax.management,
  org.eclipse.osgi.service.resolver;version="1.5.0";resolution:=optional,
diff --git a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionBundleFindHook.java b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionBundleFindHook.java
index 3d09e62..f0aa2e1 100644
--- a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionBundleFindHook.java
+++ b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionBundleFindHook.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011 VMware Inc.
+ * Copyright (c) 2013 VMware Inc.
  * 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
@@ -40,14 +40,19 @@
 	 */
 	@Override
 	public void find(BundleContext context, Collection<Bundle> bundles) {
-		long bundleID = context.getBundle().getBundleId();
-
+		Bundle finderBundle = getBundle(context);
+		if (finderBundle == null) {
+			// invalid finder bundle; clear out result
+			bundles.clear();
+			return;
+		}
+		long bundleID = finderBundle.getBundleId();
 		if (bundleID == 0 || bundleID == hookImplID) {
 			// The system bundle and the hook impl bundle can see all bundles
 			return;
 		}
 
-		Region finderRegion = getRegion(context);
+		Region finderRegion = this.regionDigraph.getRegion(finderBundle);
 		if (finderRegion == null) {
 			bundles.clear();
 			return;
@@ -81,10 +86,14 @@
 		protected boolean isAllowed(Bundle candidate, RegionFilter filter) {
 			return filter.isAllowed(candidate);
 		}
-
 	}
 
-	private Region getRegion(BundleContext context) {
-		return this.regionDigraph.getRegion(context.getBundle());
+	static Bundle getBundle(BundleContext context) {
+		try {
+			return context.getBundle();
+		} catch (IllegalStateException e) {
+			// happens if the context is invalid
+			return null;
+		}
 	}
 }
diff --git a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionServiceFindHook.java b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionServiceFindHook.java
index 2eb7ea9..2ee9d3c 100644
--- a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionServiceFindHook.java
+++ b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/hook/RegionServiceFindHook.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011 VMware Inc.
+ * Copyright (c) 2013 VMware Inc.
  * 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
@@ -13,8 +13,7 @@
 
 import java.util.Collection;
 import org.eclipse.equinox.region.*;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
+import org.osgi.framework.*;
 import org.osgi.framework.hooks.service.FindHook;
 
 /**
@@ -37,11 +36,17 @@
 	 * {@inheritDoc}
 	 */
 	public void find(BundleContext context, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references) {
-		if (context.getBundle().getBundleId() == 0L) {
+		Bundle finderBundle = RegionBundleFindHook.getBundle(context);
+		if (finderBundle == null) {
+			// invalid finder bundle; clear out result
+			references.clear();
+			return;
+		}
+		if (finderBundle.getBundleId() == 0L) {
 			return;
 		}
 
-		Region finderRegion = getRegion(context);
+		Region finderRegion = this.regionDigraph.getRegion(finderBundle);
 		if (finderRegion == null) {
 			references.clear();
 			return;
@@ -77,9 +82,4 @@
 		}
 
 	}
-
-	private Region getRegion(BundleContext context) {
-		return this.regionDigraph.getRegion(context.getBundle());
-	}
-
 }