Bug 343020 - [region] need to perform a consistency check when starting from persistent digraph
 - fixed by checking for removals on read and checking for orphans after the digraph is loaded.
diff --git a/bundles/org.eclipse.equinox.region/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.region/META-INF/MANIFEST.MF
index 37b4bac..d7ed418 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.100.qualifier
+Bundle-Version: 1.1.101.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/pom.xml b/bundles/org.eclipse.equinox.region/pom.xml
index 70485db..7da9aba 100644
--- a/bundles/org.eclipse.equinox.region/pom.xml
+++ b/bundles/org.eclipse.equinox.region/pom.xml
@@ -19,6 +19,6 @@
   </parent>
   <groupId>org.eclipse.equinox</groupId>
   <artifactId>org.eclipse.equinox.region</artifactId>
-  <version>1.1.100-SNAPSHOT</version>
+  <version>1.1.101-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/RegionManager.java b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/RegionManager.java
index 8a33295..e16591f 100644
--- a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/RegionManager.java
+++ b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/RegionManager.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008, 2011 VMware Inc.
+ * Copyright (c) 2008, 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
@@ -57,10 +57,31 @@
 			this.domain = REGION_DOMAIN_PROP;
 		digraph = loadRegionDigraph();
 		registerRegionHooks(digraph);
+		// after registering the region hooks we need to verify no ophans exist
+		// if they do then we assume the need to be in the kernel region
+		checkForOrphans(digraph);
 		digraphMBean = registerDigraphMbean(digraph);
 		registerRegionDigraph(digraph);
 	}
 
+	private void checkForOrphans(StandardRegionDigraph regionDigraph) {
+		// we assume the system bundle is in the root region.
+		Region rootRegion = regionDigraph.getRegion(0);
+		if (rootRegion != null) {
+			Bundle[] bundles = bundleContext.getBundles();
+			for (Bundle bundle : bundles) {
+				if (regionDigraph.getRegion(bundle) == null) {
+					// we have an orphan; add it to the root region
+					try {
+						rootRegion.addBundle(bundle.getBundleId());
+					} catch (BundleException e) {
+						// ignore, someone added the bundle to another region since we checked
+					}
+				}
+			}
+		}
+	}
+
 	public void stop(BundleContext bc) throws IOException {
 		digraphMBean.unregisterMbean();
 		for (ServiceRegistration<?> registration : registrations)
diff --git a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionDigraphPersistence.java b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionDigraphPersistence.java
index 1c45f29..544d1ae 100644
--- a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionDigraphPersistence.java
+++ b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionDigraphPersistence.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011 IBM Corporation and others.
+ * Copyright (c) 2011, 2013 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
@@ -113,7 +113,7 @@
 		// read the number of regions
 		int numRegions = in.readInt();
 		for (int i = 0; i < numRegions; i++) {
-			readRegion(in, digraph);
+			readRegion(in, digraph, bundleContext);
 		}
 		// read each edge
 		// read number of tail regions
@@ -129,7 +129,7 @@
 		return digraph;
 	}
 
-	private static Region readRegion(DataInputStream in, RegionDigraph digraph) throws IOException, BundleException {
+	private static Region readRegion(DataInputStream in, RegionDigraph digraph, BundleContext context) throws IOException, BundleException {
 		// read region name
 		String name = in.readUTF();
 		Region region = digraph.createRegion(name);
@@ -137,7 +137,9 @@
 		// read number of bundles
 		int numIds = in.readInt();
 		for (int i = 0; i < numIds; i++) {
-			region.addBundle(in.readLong());
+			long id = in.readLong();
+			if (context == null || context.getBundle(id) != null)
+				region.addBundle(id);
 		}
 		return region;
 	}