Added BundleListener to HeadlessLauncher to check that workspace plugins
are loaded during headless launch. Should eliminate possible race
condition.
diff --git a/org.eclipse.stem/core/org.eclipse.stem.ui.headless/src/org/eclipse/stem/ui/headless/HeadlessLauncher.java b/org.eclipse.stem/core/org.eclipse.stem.ui.headless/src/org/eclipse/stem/ui/headless/HeadlessLauncher.java
index 5fccc5a..57694f2 100644
--- a/org.eclipse.stem/core/org.eclipse.stem.ui.headless/src/org/eclipse/stem/ui/headless/HeadlessLauncher.java
+++ b/org.eclipse.stem/core/org.eclipse.stem.ui.headless/src/org/eclipse/stem/ui/headless/HeadlessLauncher.java
@@ -20,6 +20,9 @@
 import org.eclipse.stem.jobs.DisplaySafeExecutor;
 import org.eclipse.stem.ui.launcher.Launcher;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+
 
 /**
  * Launcher that starts STEM in a non-GUI "headless" mode.  The purpose
@@ -31,6 +34,18 @@
 {
 	private static final String DISTRIBUTED_CONTROLLER_PLUGIN_NAME = "org.eclipse.stem.graphsynchronizer";
 	
+	private Boolean workspacePluginsLoadSuccess = new Boolean(false);
+	
+	BundleListener workspaceLoadBundleListener = new BundleListener() {
+		public void bundleChanged(BundleEvent event) {
+			if (event.getType() == BundleEvent.STARTED && event.getBundle().getLocation().contains("org.eclipse.stem.model.common")) { 
+				workspacePluginsLoadSuccess = Boolean.TRUE;
+				// System.out.println("received event BundleEvent.STARTED= " + event.getType());
+			}
+		}
+	};
+	
+	
 	/**
 	 * STEM Launcher for headless mode.
 	 * @param ctx Eclipse Application Context
@@ -79,17 +94,54 @@
 	 */
 	public Object launch()
 	{	
-		// Drop the splash screen
-		context.applicationRunning();
-		
 		// Tell the display executor to use non-UI execution controls
 		DisplaySafeExecutor.forceHeadless();
 		
-		// Touch the model generator's bundle loader activator to force workspace bundles to load
+		// model generator's bundle loader activator 
+		org.eclipse.stem.model.common.Activator plugin = null;
+		
 		if (args.containsKey("loadWorkspacePlugins")) {
+			
+			// Set up a bundle listener
+			Activator.getContext().addBundleListener(workspaceLoadBundleListener);
+			
 			System.out.println("Loading workspace plugins");
-			org.eclipse.stem.model.common.Activator.getPlugin();
-		}
+			// Touch the model generator's bundle loader activator to force workspace bundles to load
+			plugin = org.eclipse.stem.model.common.Activator.getPlugin();
+			
+			if (plugin != null) {
+						
+				// wait at most 30 seconds for the bundle to load
+				try {
+					for (int i = 0; i < 60; i++) {
+						if (workspacePluginsLoadSuccess.booleanValue()) {
+							System.out.println("**** success loading plugin after attempts = " + i);
+							break;
+						}
+						Thread.sleep(500);
+					}
+				} catch (InterruptedException ie) {
+					// nothing
+				}
+
+				if (!workspacePluginsLoadSuccess.booleanValue()) {
+					System.out.println("Loading workspace plugins: Warning plugin not loaded. Possible Race Condition");
+				} else {
+					System.out.println("Loading workspace plugins SUCCESS");
+				}
+			} // if plugin not null
+			Activator.getContext().removeBundleListener(workspaceLoadBundleListener);
+			workspaceLoadBundleListener = null;
+			
+			// context.applicationRunning() should be called once the application is completely initialized and running.
+			// This method will perform certain operations that are needed once an application is running.  
+			// One example is bringing down a splash screen if it exists.
+			context.applicationRunning();
+			
+
+		} //loadWorkspacePlugins
+		
+		
 		
 		setStreams();