[265383] WTP should provide standard JavaEE javadoc

Change-Id: Ia72213192a578f0b40e300d8b38a7ad98beabb43
diff --git a/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewRuntimeClasspathProvider.java b/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewRuntimeClasspathProvider.java
index 0f9554f..990694d 100644
--- a/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewRuntimeClasspathProvider.java
+++ b/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewRuntimeClasspathProvider.java
@@ -14,12 +14,17 @@
 import java.util.List;
 
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.jdt.core.IAccessRule;
+import org.eclipse.jdt.core.IClasspathAttribute;
 import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jst.server.core.RuntimeClasspathProviderDelegate;
-
+import org.eclipse.wst.common.project.facet.core.IFacetedProject;
+import org.eclipse.wst.common.project.facet.core.IProjectFacet;
+import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
 import org.eclipse.wst.server.core.IRuntime;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.FrameworkUtil;
@@ -40,18 +45,79 @@
 		return FrameworkUtil.getBundle(cls).getSymbolicName();
 	}
 
+	private String getJavadocLocation(IProject project) {
+		int eeVersion = 8;
+		try {
+			IFacetedProject faceted = ProjectFacetsManager.create(project);
+			if (faceted != null && ProjectFacetsManager.isProjectFacetDefined("jst.web")) {
+				IProjectFacet webModuleFacet = ProjectFacetsManager.getProjectFacet("jst.web");
+				if (faceted.hasProjectFacet(webModuleFacet)) {
+					String servletVersionStr = faceted.getInstalledVersion(webModuleFacet).getVersionString();
+					if (servletVersionStr.equals("4.0")) {
+						eeVersion = 8;
+					} else if (servletVersionStr.equals("3.1")) {
+						eeVersion = 7;
+					} else if (servletVersionStr.equals("3.0")) {
+						eeVersion = 6;
+					} else if (servletVersionStr.equals("2.5")) {
+						eeVersion = 5;
+					} else if (servletVersionStr.equals("2.4")) {
+						eeVersion = 4;
+					} else if (servletVersionStr.equals("2.3")) {
+						eeVersion = 3;
+					}
+				}
+			}
+		}
+		catch (NumberFormatException e) {
+			// default to the latest
+		}
+		catch (CoreException e) {
+			// default to the latest
+		}
+
+		String url = "https://javaee.github.io/javaee-spec/javadocs/";
+		switch (eeVersion) {
+		case 3:
+			url = "https://docs.oracle.com/javaee/3/api/";
+			break;
+		case 4:
+			url = "https://docs.oracle.com/javaee/4/api/";
+			break;
+		case 5:
+			url = "https://docs.oracle.com/javaee/5/api/";
+			break;
+		case 6:
+			url = "https://docs.oracle.com/javaee/6/api/";
+			break;
+		case 7:
+			url = "https://docs.oracle.com/javaee/7/api/";
+			break;
+		case 8:
+			url = "https://javaee.github.io/javaee-spec/javadocs/";
+			break;
+		default:
+			url = "https://javaee.github.io/javaee-spec/javadocs/";
+			break;
+		}
+		return url;
+	}
+
 	@Override
 	public IClasspathEntry[] resolveClasspathContainer(IProject project, IRuntime runtime) {
 		List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
-		
+		String docUrl = getJavadocLocation(project);
+
 		int size = REQUIRED_BUNDLE_IDS.length;
 		for (int i = 0; i < size; i++) {
 			Bundle b = Platform.getBundle(REQUIRED_BUNDLE_IDS[i]);
 			IPath path = PreviewRuntime.getJarredPluginPath(b);
-			if (path != null)
-				list.add(JavaCore.newLibraryEntry(path, null, null));
+			if (path != null) {
+				IClasspathEntry libraryEntry = JavaCore.newLibraryEntry(path, null, null, new IAccessRule[0], new IClasspathAttribute[]{JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, docUrl)}, false);
+				list.add(libraryEntry);
+			}
 		}
-		
+
 		return list.toArray(new IClasspathEntry[list.size()]);
 	}
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewServerBehaviour.java b/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewServerBehaviour.java
index 335d3af..722b601 100644
--- a/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewServerBehaviour.java
+++ b/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewServerBehaviour.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 IBM Corporation and others.
+ * Copyright (c) 2007, 2018 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
@@ -69,7 +69,7 @@
 	}
 
 	public PreviewServer getPreviewServer() {
-		return (PreviewServer) getServer().getAdapter(PreviewServer.class);
+		return getServer().getAdapter(PreviewServer.class);
 	}
 
 	/**
@@ -150,7 +150,7 @@
 			public void handleDebugEvents(DebugEvent[] events) {
 				if (events != null) {
 					for (DebugEvent event : events) {
-						if (newProcess != null && newProcess.equals(event.getSource()) && event.getKind() == DebugEvent.TERMINATE) {
+						if (newProcess.equals(event.getSource()) && event.getKind() == DebugEvent.TERMINATE) {
 							stop(true);
 						}
 					}
diff --git a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntimeClasspathProvider.java b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntimeClasspathProvider.java
index ab4d114..fcbe2b1 100644
--- a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntimeClasspathProvider.java
+++ b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatRuntimeClasspathProvider.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2003, 2011 IBM Corporation and others.
+ * Copyright (c) 2003, 2018 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
@@ -14,21 +14,102 @@
 import java.util.List;
 
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.jdt.core.IClasspathAttribute;
 import org.eclipse.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jst.server.core.RuntimeClasspathProviderDelegate;
-
+import org.eclipse.wst.common.project.facet.core.IFacetedProject;
+import org.eclipse.wst.common.project.facet.core.IProjectFacet;
+import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
 import org.eclipse.wst.server.core.IRuntime;
 /**
  * Classpath provider for the Tomcat runtime.
  */
 public class TomcatRuntimeClasspathProvider extends RuntimeClasspathProviderDelegate {
+	private static final String JST_WEB_FACET_ID = "jst.web";
+
+	private final String getEEJavadocLocation(IProject project) {
+		int eeVersion = 8;
+		try {
+			IFacetedProject faceted = ProjectFacetsManager.create(project);
+			if (faceted != null && ProjectFacetsManager.isProjectFacetDefined(JST_WEB_FACET_ID)) {
+				IProjectFacet webModuleFacet = ProjectFacetsManager.getProjectFacet(JST_WEB_FACET_ID);
+				if (faceted.hasProjectFacet(webModuleFacet)) {
+					String servletVersionStr = faceted.getInstalledVersion(webModuleFacet).getVersionString();
+					if (servletVersionStr.equals("4.0")) {
+						eeVersion = 8;
+					} else if (servletVersionStr.equals("3.1")) {
+						eeVersion = 7;
+					} else if (servletVersionStr.equals("3.0")) {
+						eeVersion = 6;
+					} else if (servletVersionStr.equals("2.5")) {
+						eeVersion = 5;
+					} else if (servletVersionStr.equals("2.4")) {
+						eeVersion = 4;
+					} else if (servletVersionStr.equals("2.3")) {
+						eeVersion = 3;
+					}
+				}
+			}
+		}
+		catch (NumberFormatException e) {
+			// default to the latest
+		}
+		catch (CoreException e) {
+			// default to the latest
+		}
+
+		String url = "https://javaee.github.io/javaee-spec/javadocs/";
+		switch (eeVersion) {
+		case 3:
+			url = "https://docs.oracle.com/javaee/3/api/";
+			break;
+		case 4:
+			url = "https://docs.oracle.com/javaee/4/api/";
+			break;
+		case 5:
+			url = "https://docs.oracle.com/javaee/5/api/";
+			break;
+		case 6:
+			url = "https://docs.oracle.com/javaee/6/api/";
+			break;
+		case 7:
+			url = "https://docs.oracle.com/javaee/7/api/";
+			break;
+		case 8:
+			url = "https://javaee.github.io/javaee-spec/javadocs/";
+			break;
+		default:
+			url = "https://javaee.github.io/javaee-spec/javadocs/";
+			break;
+		}
+
+		return url;
+	}
+
+	private String getTomcatJavadocLocation(IRuntime runtime) {
+		/* Default to v9.0 doc. v7.0 is currently the oldest advertised version on the front page */
+		String tomcatDocURL = "https://tomcat.apache.org/tomcat-9.0-doc/api/";
+		String runtimeTypeId = runtime.getRuntimeType().getId();
+		if (runtimeTypeId.indexOf("85") > 0) {
+			tomcatDocURL = "https://tomcat.apache.org/tomcat-8.5-doc/api/";
+		}
+		else if (runtimeTypeId.indexOf("80") > 0) {
+			tomcatDocURL = "https://tomcat.apache.org/tomcat-8.0-doc/api/";
+		}
+		else if (runtimeTypeId.indexOf("70") > 0) {
+			tomcatDocURL = "https://tomcat.apache.org/tomcat-7.0-doc/api/";
+		}
+		return tomcatDocURL;
+	}
+
 	/**
 	 * @see RuntimeClasspathProviderDelegate#resolveClasspathContainer(IProject, IRuntime)
 	 */
 	public IClasspathEntry[] resolveClasspathContainer(IProject project, IRuntime runtime) {
 		IPath installPath = runtime.getLocation();
-		
 		if (installPath == null)
 			return new IClasspathEntry[0];
 		
@@ -48,6 +129,23 @@
 			addLibraryEntries(list, path.append("lib").toFile(), true);
 			addLibraryEntries(list, path.append("endorsed").toFile(), true);
 		}
-		return list.toArray(new IClasspathEntry[list.size()]);
+
+		IClasspathEntry[] entries = list.toArray(new IClasspathEntry[list.size()]);
+		String apiJavadocLocation = getEEJavadocLocation(project);
+		String tomcatDocLocation = getTomcatJavadocLocation(runtime);
+		for (int i = 0; i < entries.length; i++) {
+			IClasspathEntry entry = entries[i];
+			String jarName = entry.getPath().lastSegment();
+			if (jarName.endsWith("-api.jar") && !jarName.startsWith("tomcat")) {
+				// these are assumed to be the API jars for the runtime standards
+				IClasspathEntry apiLibraryEntry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), new IClasspathAttribute[]{JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, apiJavadocLocation)}, entry.isExported());
+				entries[i] = apiLibraryEntry;
+			}
+			else {
+				IClasspathEntry tomcatLibraryEntry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), new IClasspathAttribute[]{JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, tomcatDocLocation)}, entry.isExported());
+				entries[i] = tomcatLibraryEntry;
+			}
+		}
+		return entries;
 	}
 }