[328067] Deadlock during debugging
diff --git a/plugins/org.eclipse.wst.server.core/plugin.properties b/plugins/org.eclipse.wst.server.core/plugin.properties
index e0d08e7..54c5652 100644
--- a/plugins/org.eclipse.wst.server.core/plugin.properties
+++ b/plugins/org.eclipse.wst.server.core/plugin.properties
@@ -29,6 +29,7 @@
 extensionPointServerMonitors=Server Monitors
 extensionPointInstallableServers=Installable Servers
 extensionPointInstallableRuntimes=Installable Runtimes
+extensionPointSaveEditorPrompter=Save Editor Prompter
 
 extensionPointRuntimeFacetComponentProviders=Runtime Facet Component Providers
 
diff --git a/plugins/org.eclipse.wst.server.core/plugin.xml b/plugins/org.eclipse.wst.server.core/plugin.xml
index 344f9cc..77b26c2 100644
--- a/plugins/org.eclipse.wst.server.core/plugin.xml
+++ b/plugins/org.eclipse.wst.server.core/plugin.xml
@@ -20,6 +20,7 @@
   <extension-point id="installableRuntimes" name="%extensionPointInstallableRuntimes" schema="schema/installableRuntimes.exsd"/>
   <extension-point id="runtimeFacetComponentProviders" name="%extensionPointRuntimeFacetComponentProviders" schema="schema/runtimeFacetComponentProviders.exsd"/>
   <extension-point id="runtimeModuleType" name="%runtimeModuleType" schema="schema/runtimeModuleType.exsd"/>
+  <extension-point id="saveEditorPrompter" name="%extensionPointSaveEditorPrompter" schema="schema/saveEditorPrompter.exsd"/>
 
   <extension point="org.eclipse.wst.server.core.moduleTypes">
     <moduleType
diff --git a/plugins/org.eclipse.wst.server.core/schema/saveEditorPrompter.exsd b/plugins/org.eclipse.wst.server.core/schema/saveEditorPrompter.exsd
new file mode 100644
index 0000000..aeafbb7
--- /dev/null
+++ b/plugins/org.eclipse.wst.server.core/schema/saveEditorPrompter.exsd
@@ -0,0 +1,121 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!-- Schema file written by PDE -->
+<schema targetNamespace="org.eclipse.wst.server.core" xmlns="http://www.w3.org/2001/XMLSchema">
+<annotation>
+      <appInfo>
+         <meta.schema plugin="org.eclipse.wst.server.core" id="saveEditorPrompter" name="%saveEditorPrompter"/>
+      </appInfo>
+      <documentation>
+         This is an internal extension point that should only be adopted by org.eclipse.wst.server.core. It is used to transfer the control to the UI and save all the editors are open before continuing
+      </documentation>
+   </annotation>
+
+   <element name="extension">
+      <annotation>
+         <appInfo>
+            <meta.element />
+         </appInfo>
+      </annotation>
+      <complexType>
+         <sequence>
+            <element ref="saveEditorPrompter"/>
+         </sequence>
+         <attribute name="point" type="string" use="required">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="id" type="string">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="name" type="string">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+               <appInfo>
+                  <meta.attribute translatable="true"/>
+               </appInfo>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
+   <element name="saveEditorPrompter">
+      <complexType>
+         <attribute name="id" type="string" use="required">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="class" type="string" use="required">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+               <appInfo>
+                  <meta.attribute kind="java" basedOn="org.eclipse.wst.server.core.internal.SaveEditorPrompter:"/>
+               </appInfo>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="since"/>
+      </appInfo>
+      <documentation>
+         [Enter the first release in which this extension point appears.]
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="examples"/>
+      </appInfo>
+      <documentation>
+         [Enter extension point usage example here.]
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="apiinfo"/>
+      </appInfo>
+      <documentation>
+         [Enter API information here.]
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="implementation"/>
+      </appInfo>
+      <documentation>
+         [Enter information about supplied implementation of this extension point.]
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="copyright"/>
+      </appInfo>
+      <documentation>
+         Copyright (c) 2010 IBM Corporation and others.&lt;br&gt;
+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 
+&lt;a href=&quot;http://www.eclipse.org/legal/epl-v10.html&quot;&gt;http://www.eclipse.org/legal/epl-v10.html&lt;/a&gt;
+      </documentation>
+   </annotation>
+
+</schema>
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
new file mode 100644
index 0000000..8e45842
--- /dev/null
+++ b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/SaveEditorPrompter.java
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * Copyright (c) 2010 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.wst.server.core.internal;
+
+/**
+ * <p>
+ * SaveEditorPrompter is the abstract implementation of the .saveEditorPrompter extension 
+ * point. This class is used for prompting to the user to save all the editors. The class 
+ * transfer the control from the non-ui code to the UI code via an the extension point
+ * </p>
+ * <b>This class is not intended to be extended adopters.</b> 
+ * 
+ */
+public class SaveEditorPrompter {
+	
+	/**
+	 * Move the control to the UI and save all the editors according to <code>org.eclipse.debug.internal.ui.IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH</code>  
+	 */
+	public void saveAllEditors(){
+		// nothing to do, should be implemented		
+	}
+	public void setDebugNeverSave(){
+		// nothing to do, should be implemented		
+	}
+	public void setDebugOriginalValue(){
+		// nothing to do, should be implemented		
+	}
+}
diff --git a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/Server.java b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/Server.java
index a74e444..f8612bc 100644
--- a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/Server.java
+++ b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/Server.java
@@ -1911,7 +1911,11 @@
 			else
 				pub = StartJob.PUBLISH_BEFORE;
 		}
-
+		
+		if (ServerPlugin.isRunningGUIMode()){
+			ServerPlugin.getSaveEditorHelper().saveAllEditors();
+		}
+		
 		StartJob startJob = new StartJob(mode2);
 		if (opListener != null && pub != StartJob.PUBLISH_AFTER) {
 			startJob.addJobChangeListener(new JobChangeAdapter() {
@@ -3198,15 +3202,23 @@
 
 	protected void startImpl2(String mode2, IProgressMonitor monitor) throws CoreException {
 		Trace.trace(Trace.FINEST, "Starting server: " + Server.this.toString() + ", launchMode: " + mode2);
-		
+		SaveEditorPrompter editorHelper = (ServerPlugin.isRunningGUIMode()) ? ServerPlugin.getSaveEditorHelper() : null;
 		// make sure that the delegate is loaded and the server state is correct
 		loadAdapter(ServerBehaviourDelegate.class, monitor);
 		
 		try {
 			ILaunchConfiguration launchConfig = getLaunchConfiguration(true, monitor);
-			//if (launchConfig == null)
-			//	throw new CoreException();
+			
+			if (editorHelper != null){
+				editorHelper.setDebugNeverSave();
+			}
+			
 			launch = launchConfig.launch(mode2, monitor); // , true); - causes workspace lock
+			
+			if (editorHelper != null){
+				editorHelper.setDebugOriginalValue();
+			}
+			
 			Trace.trace(Trace.FINEST, "Launch: " + launch);
 		} catch (CoreException e) {
 			Trace.trace(Trace.SEVERE, "Error starting server " + Server.this.toString(), e);
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 1afce00..5fda91e 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
@@ -10,18 +10,16 @@
  *******************************************************************************/
 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.*;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.BundleListener;
+import org.osgi.framework.*;
 /**
  * The main server plugin class.
  */
@@ -69,6 +67,12 @@
 	//	cached copy of all installable runtimes
 	private static List<IInstallableRuntime> installableRuntimes;
 
+	// cached copy of SaveEditorPrompter
+	private static SaveEditorPrompter saveEditorPrompter;
+	
+	// cached copy of isRunningInGUICache
+	public static boolean isRunningInGUICache = false;
+
 	// registry listener
 	private static IRegistryChangeListener registryListener;
 	
@@ -1282,4 +1286,52 @@
 			return Platform.getProduct().getProperty(key);
 		return value;
 	}
+	
+	public static boolean isRunningGUIMode(){
+		// check only when the the plugin is not Bundle.ACTIVE
+		if (isRunningInGUICache == true){
+			return isRunningInGUICache;
+		}
+
+		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;
+	}	
+
+
+	/**
+	 * Transfer the control to the UI and prompts to save all the editors
+	 */
+	public static SaveEditorPrompter getSaveEditorHelper() {
+		loadSaveEditorExtension();
+		return saveEditorPrompter;
+	}
+
+	private static void loadSaveEditorExtension() {
+		if (saveEditorPrompter != null)
+			return;
+		Trace.trace(Trace.EXTENSION_POINT, "->- Loading .saveEditorPrompter extension point ->-");
+		
+		IExtensionRegistry registry = Platform.getExtensionRegistry();
+		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 < 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 -<-");
+		
+	}
+	
+	
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/plugin.xml b/plugins/org.eclipse.wst.server.ui/plugin.xml
index 9baca0e..10f4697 100644
--- a/plugins/org.eclipse.wst.server.ui/plugin.xml
+++ b/plugins/org.eclipse.wst.server.ui/plugin.xml
@@ -623,4 +623,11 @@
       </enablement>
    </decorator>
 </extension>
+<extension
+      point="org.eclipse.wst.server.core.saveEditorPrompter">
+   <saveEditorPrompter
+         class="org.eclipse.wst.server.ui.internal.SaveEditorPrompter"
+         id="org.eclipse.wst.server.ui.saveEditorPrompter1">
+   </saveEditorPrompter>
+</extension>
 </plugin>
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
new file mode 100644
index 0000000..ccd204a
--- /dev/null
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/SaveEditorPrompter.java
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * Copyright (c) 2010 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.wst.server.ui.internal;
+
+import org.eclipse.debug.ui.DebugUITools;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.PlatformUI;
+
+public class SaveEditorPrompter extends
+		org.eclipse.wst.server.core.internal.SaveEditorPrompter { 
+	
+	private String cachedSaveBeforeLaunch;
+	
+	@Override
+	public void saveAllEditors() {
+		IWorkbench w = PlatformUI.getWorkbench();
+		String saveBeforeLaunch = DebugUITools.getPreferenceStore().getString(org.eclipse.debug.internal.ui.IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH);
+		if (saveBeforeLaunch.equalsIgnoreCase(org.eclipse.jface.dialogs.MessageDialogWithToggle.ALWAYS)){
+			Display d =PlatformUI.getWorkbench().getDisplay();
+			d.asyncExec(new SaveAllEditorsRunnable(w,false));
+		}
+		else if (saveBeforeLaunch.equalsIgnoreCase(org.eclipse.jface.dialogs.MessageDialogWithToggle.PROMPT)){
+			Display d =PlatformUI.getWorkbench().getDisplay();
+			d.asyncExec(new SaveAllEditorsRunnable(w,true));
+		}
+	}
+	
+	private class SaveAllEditorsRunnable implements Runnable{
+		IWorkbench w;
+		boolean confirm;
+		public SaveAllEditorsRunnable(IWorkbench w, boolean confirm){
+			this.w = w;
+			this.confirm = confirm;
+		}
+		
+		public void run() {
+			w.saveAllEditors(confirm);
+		}
+	}
+		
+	/**
+	 * Sets <code>org.eclipse.debug.internal.ui.IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH</code> to 
+	 * the given value. The given value should be one of the following:
+	 * <ul>
+	 * <li><code>org.eclipse.jface.dialogs.MessageDialogWithToggle.ALWAYS</code></li>
+	 * <li><code>org.eclipse.jface.dialogs.MessageDialogWithToggle.NEVER</code></li>
+	 * <li><code>org.eclipse.jface.dialogs.MessageDialogWithToggle.PROMPT</code></li>
+	 * </ul>  
+	 * @see org.eclipse.jface.dialogs.MessageDialogWithToggle
+	 */
+	public String setDebugSaveBeforeLaunching(String newValue){
+		IPreferenceStore debugPrefs = DebugUITools.getPreferenceStore();
+		String oldValue = debugPrefs.getString(org.eclipse.debug.internal.ui.IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH);
+		debugPrefs.setValue(org.eclipse.debug.internal.ui.IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH, newValue);
+		return oldValue; 
+	}
+	
+	public void setDebugNeverSave(){
+		cachedSaveBeforeLaunch = setDebugSaveBeforeLaunching(org.eclipse.jface.dialogs.MessageDialogWithToggle.NEVER);
+	}
+	
+	public void setDebugOriginalValue(){
+		if (cachedSaveBeforeLaunch == null){
+			cachedSaveBeforeLaunch = org.eclipse.jface.dialogs.MessageDialogWithToggle.PROMPT;
+		}
+		setDebugSaveBeforeLaunching(cachedSaveBeforeLaunch);
+	}
+}