334576 - Headless runs detect that org.eclipse.ui is active, a better check is needed
diff --git a/features/org.eclipse.wst.server_core.feature.patch/buildnotes_org.eclipse.wst.server_core.feature.patch.html b/features/org.eclipse.wst.server_core.feature.patch/buildnotes_org.eclipse.wst.server_core.feature.patch.html
index fea027f..c83e80d 100644
--- a/features/org.eclipse.wst.server_core.feature.patch/buildnotes_org.eclipse.wst.server_core.feature.patch.html
+++ b/features/org.eclipse.wst.server_core.feature.patch/buildnotes_org.eclipse.wst.server_core.feature.patch.html
@@ -15,6 +15,7 @@
 <h3>Plugin(s) replaced:</h3>
 <ul><li>org.eclipse.wst.server.core</li></ul>
 <p>Bug <a href='https://bugs.eclipse.org/279110'>279110</a>. Server status stuck on 'Republish'</p>
+<p>Bug <a href='https://bugs.eclipse.org/334576'>334576</a>. Headless runs detect that org.eclipse.ui is active, a better check is needed</p>
 <ul><li>org.eclipse.wst.internet.monitor.core</li></ul>
 <p>Bug <a href='https://bugs.eclipse.org/279101'>279101</a>. TCP/IP HTTP parser is case sensitive, when the specs say it shouldn't be</p>
 <p>Bug <a href='https://bugs.eclipse.org/282909'>282909</a>. Can't create a new server if rename an existing one</p>
diff --git a/features/org.eclipse.wst.server_core.feature.patch/feature.properties b/features/org.eclipse.wst.server_core.feature.patch/feature.properties
index f5091bf..ba244ab 100644
--- a/features/org.eclipse.wst.server_core.feature.patch/feature.properties
+++ b/features/org.eclipse.wst.server_core.feature.patch/feature.properties
@@ -38,6 +38,7 @@
 Bug https://bugs.eclipse.org/323658 NPE- modules variable not initialized when API called during automated scenario\n\
 Bug https://bugs.eclipse.org/309781 Deadlock during debugging\n\
 Bug https://bugs.eclipse.org/327806 Backport the 311794\n\
+Bug https://bugs.eclipse.org/334576 Headless runs detect that org.eclipse.ui is active, a better check is needed\n\
 \n\
 
 # "copyright" property - text of the "Feature Update Copyright"
diff --git a/features/org.eclipse.wst.server_ui.feature.patch/buildnotes_org.eclipse.wst.server_ui.feature.patch.html b/features/org.eclipse.wst.server_ui.feature.patch/buildnotes_org.eclipse.wst.server_ui.feature.patch.html
index d24046f..e15b6af 100644
--- a/features/org.eclipse.wst.server_ui.feature.patch/buildnotes_org.eclipse.wst.server_ui.feature.patch.html
+++ b/features/org.eclipse.wst.server_ui.feature.patch/buildnotes_org.eclipse.wst.server_ui.feature.patch.html
@@ -18,5 +18,6 @@
 <p>Bug <a href='https://bugs.eclipse.org/291960'>291960</a>. XMLViewer makes (apparently) invalid assumptions about TransformerFactory</p>
 <p>Bug <a href='https://bugs.eclipse.org/309781'>309781</a>. Deadlock during debugging</p>
 <p>Bug <a href='https://bugs.eclipse.org/328200'>328200</a>. ServerUIPlugin shows server view when the view is not open</p>
+<p>Bug <a href='https://bugs.eclipse.org/334576'>334576</a>. Headless runs detect that org.eclipse.ui is active, a better check is needed</p>
 
 </body></html>
\ No newline at end of file
diff --git a/features/org.eclipse.wst.server_ui.feature.patch/feature.properties b/features/org.eclipse.wst.server_ui.feature.patch/feature.properties
index 604bb27..4aeca1c 100644
--- a/features/org.eclipse.wst.server_ui.feature.patch/feature.properties
+++ b/features/org.eclipse.wst.server_ui.feature.patch/feature.properties
@@ -31,6 +31,7 @@
 Bug https://bugs.eclipse.org/291960 XMLViewer makes (apparently) invalid assumptions about TransformerFactory\n\
 Bug https://bugs.eclipse.org/309781 Deadlock during debugging\n\
 Bug https://bugs.eclipse.org/328200 ServerUIPlugin shows server view when the view is not open\n\
+Bug https://bugs.eclipse.org/334576 Headless runs detect that org.eclipse.ui is active, a better check is needed\n\
 \n\
 
 # "copyright" property - text of the "Feature Update Copyright"
diff --git a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/SaveEditorPrompter.java b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/SaveEditorPrompter.java
index 8e45842..dca207e 100644
--- a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/SaveEditorPrompter.java
+++ b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/SaveEditorPrompter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010 IBM Corporation and others.
+ * Copyright (c) 2010, 2011 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
@@ -34,4 +34,8 @@
 	public void setDebugOriginalValue(){
 		// nothing to do, should be implemented		
 	}
+	
+	public boolean isCurrentContextUI(){
+		return false;
+	}
 }
diff --git a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerPlugin.java b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerPlugin.java
index 7057b14..8086ca6 100644
--- a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerPlugin.java
+++ b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerPlugin.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2003, 2010 IBM Corporation and others.
+ * Copyright (c) 2003, 2011 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
@@ -10,23 +10,15 @@
  *******************************************************************************/
 package org.eclipse.wst.server.core.internal;
 
-import java.io.*;
-import java.util.*;
+import java.io.File;
 import java.text.DateFormat;
+import java.util.*;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.*;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.osgi.util.NLS;
-import org.eclipse.wst.server.core.IModuleArtifact;
-import org.eclipse.wst.server.core.IModuleType;
-import org.eclipse.wst.server.core.IRuntime;
-import org.eclipse.wst.server.core.IRuntimeType;
-import org.eclipse.wst.server.core.IRuntimeWorkingCopy;
-import org.eclipse.wst.server.core.IServerAttributes;
-import org.eclipse.wst.server.core.IServerWorkingCopy;
-import org.eclipse.wst.server.core.ServerCore;
-import org.osgi.framework.Bundle;
+import org.eclipse.wst.server.core.*;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleListener;
@@ -80,7 +72,7 @@
 	private static SaveEditorPrompter saveEditorPrompter;
 	
 	// cached copy of isRunningInGUICache
-	public static boolean isRunningInGUICache = false;
+	public static Boolean isRunningInGUICache = null;
 
 	// registry listener
 	private static IRegistryChangeListener registryListener;
@@ -1245,18 +1237,20 @@
 		return value;
 	}
 	
-	public static boolean isRunningGUIMode(){
-		// check only when the the plugin is not Bundle.ACTIVE
-		if (isRunningInGUICache == true){
-			return isRunningInGUICache;
-		}
+	public static boolean isRunningGUIMode() {
 
-		Bundle swtEclipseUIbndl = Platform.getBundle("org.eclipse.ui");  //running in GUI mode if it is active.
-		if(swtEclipseUIbndl != null){
-			isRunningInGUICache= (swtEclipseUIbndl.getState() == Bundle.ACTIVE);
-			return isRunningInGUICache; 
-		}			
-		return false;
+		if (isRunningInGUICache == null) {
+
+			// first check if the plugin extension is adopted (meaning that org.eclipse.wst.server.ui is present)
+			SaveEditorPrompter sep = getSaveEditorHelper();
+			if (sep == null)
+				// sep is null thus plugin doesn't exists, we must be running in HEADLESS
+				isRunningInGUICache = new Boolean(false);
+			else
+				// sep is valid, check if there is UI elements 
+				isRunningInGUICache = new Boolean(sep.isCurrentContextUI());
+		}		
+		return isRunningInGUICache.booleanValue();
 	}	
 
 
@@ -1277,18 +1271,21 @@
 		IConfigurationElement[] cf = registry.getConfigurationElementsFor(ServerPlugin.PLUGIN_ID, "saveEditorPrompter");
 		
 		int size = cf.length;
-		try{
-			saveEditorPrompter = (SaveEditorPrompter)cf[0].createExecutableExtension("class");
-			Trace.trace(Trace.EXTENSION_POINT, "  Loaded saveEditorPrompter: " + cf[0].getAttribute("id"));
-		} catch (CoreException ce){
-			Trace.trace(Trace.SEVERE, "  Could not load saveEditorPrompter: " + cf[0].getAttribute("id"), ce);			
+
+		if (size > 0){
+			try{
+				saveEditorPrompter = (SaveEditorPrompter)cf[0].createExecutableExtension("class");
+				Trace.trace(Trace.EXTENSION_POINT, "  Loaded saveEditorPrompter: " + cf[0].getAttribute("id"));
+			} catch (CoreException ce){
+				Trace.trace(Trace.SEVERE, "  Could not load saveEditorPrompter: " + cf[0].getAttribute("id"), ce);
+			}
 		}
+		
 		if (size < 1) {
 			Trace.trace(Trace.WARNING, "  More than one .saveEditorPrompter found, only one loaded =>"+ cf[0].getAttribute("id"));
 		}
-		
+
 		Trace.trace(Trace.EXTENSION_POINT, "-<- Done loading .saveEditorPrompter extension point -<-");
-		
 	}
 	
 	
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/SaveEditorPrompter.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/SaveEditorPrompter.java
index ccd204a..8f84b11 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/SaveEditorPrompter.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/SaveEditorPrompter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010 IBM Corporation and others.
+ * Copyright (c) 2010, 2011 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
@@ -11,6 +11,7 @@
 package org.eclipse.wst.server.ui.internal;
 
 import org.eclipse.debug.ui.DebugUITools;
+import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IWorkbench;
@@ -75,4 +76,16 @@
 		}
 		setDebugSaveBeforeLaunching(cachedSaveBeforeLaunch);
 	}
+	
+	public boolean isCurrentContextUI() {
+		try {
+			if (ErrorDialog.AUTOMATED_MODE)
+				return false;
+
+			return PlatformUI.isWorkbenchRunning() || PlatformUI.getWorkbench().isClosing(); 
+		} catch (Exception e) {
+			//Ignore, workbench must not be running
+			return false;
+		}
+	}
 }