[492984] New Server wizard is broken when server installation directory is changed

Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=492984
Change-Id: I1694374c0f10473ca7f49fbba0af61c84a590b76
Signed-off-by: Rob Stryker <stryker@redhat.com>
diff --git a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntime.java b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntime.java
index cb2d5be..b4310ff 100644
--- a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntime.java
+++ b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntime.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2003, 2016 IBM Corporation and others.
+ * Copyright (c) 2003, 2017 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
@@ -101,7 +101,11 @@
 	 * @return boolean
 	 */
 	public IStatus verifyLocation() {
-		return getVersionHandler().verifyInstallPath(getRuntime().getLocation());
+		return verifyLocation(getRuntime().getLocation());
+	}
+	
+	private IStatus verifyLocation(IPath loc) {
+		return getVersionHandler().verifyInstallPath(loc);
 	}
 	
 	/*
@@ -223,7 +227,9 @@
 	 */
 	public void setDefaults(IProgressMonitor monitor) {
 		IRuntimeType type = getRuntimeWorkingCopy().getRuntimeType();
-		getRuntimeWorkingCopy().setLocation(new Path(TomcatPlugin.getPreference("location" + type.getId())));
+		Path p = new Path(TomcatPlugin.getPreference("location" + type.getId()));
+		if( p != null && p.toFile().exists() && verifyLocation(p).isOK())
+			getRuntimeWorkingCopy().setLocation(p);
 	}
 
 	public void setVMInstall(IVMInstall vmInstall) {
diff --git a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerType.java b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerType.java
index 5a83676..be56076 100644
--- a/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerType.java
+++ b/plugins/org.eclipse.wst.server.core/servercore/org/eclipse/wst/server/core/internal/ServerType.java
@@ -223,7 +223,8 @@
 		
 		swc.setDefaults(monitor);
 		
-		if (hasServerConfiguration() && runtime != null && runtime.getLocation() != null && !runtime.getLocation().isEmpty())
+		if (hasServerConfiguration() && runtime != null && runtime.getLocation() != null && !runtime.getLocation().isEmpty()
+				&& runtime.getLocation().toFile().exists())
 			swc.importRuntimeConfiguration(runtime, null);
 		
 		return swc;
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java
index 3b04fdb..2c723ad 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java
@@ -15,12 +15,15 @@
 import java.beans.PropertyChangeEvent;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.ToolBarManager;
@@ -122,6 +125,8 @@
 	private boolean isServerNameInUse=false;
 	private String cacheServerNameCheck="";
 	
+	private HashMap<String,IStatus> serverTypeCriticalFailure = new HashMap<String, IStatus>();
+	
 	/**
 	 * Creates a new server and server configuration.  If the initial
 	 * resource selection contains exactly one container resource then it will be
@@ -475,7 +480,15 @@
 	
 	protected boolean validate(IServerType selectedServerType){
 		wizard.setMessage(null, IMessageProvider.NONE);
-		return (checkHostAndServerType(selectedServerType) & checkServerName());
+		boolean ret = (checkHostAndServerType(selectedServerType) & checkServerName());
+		if( selectedServerType != null && serverTypeCriticalFailure.get(selectedServerType.getId()) != null ) {
+			IStatus stat = serverTypeCriticalFailure.get(selectedServerType.getId());
+			int msgProviderSev = stat.getSeverity();
+			if( msgProviderSev == IStatus.ERROR)
+				msgProviderSev = IMessageProvider.ERROR;
+			wizard.setMessage(stat.getMessage(), msgProviderSev);
+		}
+		return ret;
 	}
 	
 	/**
@@ -529,11 +542,15 @@
 			}
 			server = null;
 			runtime = null;
+			serverTypeCriticalFailure.put(serverType.getId(), new Status(IStatus.ERROR, ServerUIPlugin.PLUGIN_ID, ce.getLocalizedMessage(), ce));
 			wizard.setMessage(ce.getLocalizedMessage(), IMessageProvider.ERROR);
+			return; // Return to preserve this error message for clarity
 		}
 			
-		if (server == null)
+		if (server == null) {
+			serverTypeCriticalFailure.put(serverType.getId(), new Status(IStatus.ERROR, ServerUIPlugin.PLUGIN_ID, Messages.wizErrorServerCreationError, null));
 			wizard.setMessage(Messages.wizErrorServerCreationError, IMessageProvider.ERROR);
+		}
 	}
 
 	/**
@@ -568,7 +585,7 @@
 			int size = runtimes.length;
 			for (int i = 0; i < size; i++) {
 				IRuntime runtime2 = runtimes[i];
-				if (isLocalhost || !runtime2.isStub())
+				if ((isLocalhost || !runtime2.isStub()) && (runtime2.validate(new NullProgressMonitor()).getSeverity() < IStatus.ERROR))
 					runtimes2.add(runtime2);
 			}
 			runtimes = new IRuntime[runtimes2.size()];