Bug 534874 - Fix preview server adaptors if java ee implementations have unexpected BSNs

To allow downstream integrators to use any implementation of the
java ee APIs, we should avoid hard-coding bundle names for those
APIs. This change makes the preview server adapters ask the framework
for the bundle names instead, based on class names that we know
must be present in such bundles.

Change-Id: Iad600de7f795ebcc71603fae82b2ceb22fe1f9c6
Signed-off-by: Mat Booth <mat.booth@redhat.com>
diff --git a/plugins/org.eclipse.jst.server.preview.adapter/META-INF/MANIFEST.MF b/plugins/org.eclipse.jst.server.preview.adapter/META-INF/MANIFEST.MF
index 978ac7a..ece2ac0 100644
--- a/plugins/org.eclipse.jst.server.preview.adapter/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.jst.server.preview.adapter/META-INF/MANIFEST.MF
@@ -20,3 +20,8 @@
  org.eclipse.jdt.debug.ui;bundle-version="[3.2.100,4.0.0)"
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Import-Package: com.sun.el;version="2.2.0",
+ javax.el;version="2.2.0",
+ javax.servlet;version="3.1.0",
+ javax.servlet.jsp;version="2.2.0",
+ org.apache.jasper;version="2.2.2"
diff --git a/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java b/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java
index 280f91f..d3c892a 100644
--- a/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java
+++ b/plugins/org.eclipse.jst.server.preview.adapter/src/org/eclipse/jst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2013 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
@@ -31,6 +31,7 @@
 import org.eclipse.wst.server.core.ServerCore;
 import org.eclipse.wst.server.core.ServerUtil;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
 /**
  * 
  */
@@ -43,9 +44,12 @@
 	// this array, please ensure the index of org.eclipse.wst.server.preview 
 	// corresponds to CLASSPATH_BIN_INDEX_PREVIEW_SERVER
 	private static final String[] REQUIRED_BUNDLE_IDS = new String[] {
+		getBundleForClass(javax.servlet.ServletContext.class),
+		getBundleForClass(javax.servlet.jsp.JspContext.class),
+		getBundleForClass(org.apache.jasper.JspCompilationContext.class),
+		getBundleForClass(javax.el.ELContext.class),
+		getBundleForClass(com.sun.el.ExpressionFactoryImpl.class),
 		"org.apache.commons.logging",
-		"javax.servlet",
-		"javax.servlet.jsp",
 		"org.eclipse.jetty.continuation",
 		"org.eclipse.jetty.http",
 		"org.eclipse.jetty.io",
@@ -55,17 +59,22 @@
 		"org.eclipse.jetty.util",
 		"org.eclipse.jetty.webapp",
 		"org.eclipse.jetty.xml",
-		"javax.el",
-		"com.sun.el",
-		"org.apache.jasper.glassfish",
 		"org.eclipse.wst.server.preview"
 	};
 
-	private static final String MAIN_CLASS = "org.eclipse.wst.server.preview.internal.PreviewStarter";
 	// The index of org.eclipse.wst.server.preview in REQUIRED_BUNDLE_IDS, for supporting
 	// running on the workbench when the plug-in is checked out
 	private static final int CLASSPATH_BIN_INDEX_PREVIEW_SERVER = REQUIRED_BUNDLE_IDS.length-1;
 
+	/**
+	 * Gets the symbolic name of the bundle that supplies the given class.
+	 */
+	private static String getBundleForClass(Class<?> cls) {
+		return FrameworkUtil.getBundle(cls).getSymbolicName();
+	}
+
+	private static final String MAIN_CLASS = "org.eclipse.wst.server.preview.internal.PreviewStarter";
+
 	public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
 		IServer server = ServerUtil.getServer(configuration);
 		if (server == null) {
diff --git a/plugins/org.eclipse.jst.server.preview/archived.txt b/plugins/org.eclipse.jst.server.preview/archived.txt
deleted file mode 100644
index 954fe2c..0000000
--- a/plugins/org.eclipse.jst.server.preview/archived.txt
+++ /dev/null
@@ -1 +0,0 @@
-This bundle was used during development of the preview server and never shipped in WTP. 
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.preview.adapter/META-INF/MANIFEST.MF b/plugins/org.eclipse.wst.server.preview.adapter/META-INF/MANIFEST.MF
index d86c1b3..5d8ca50 100644
--- a/plugins/org.eclipse.wst.server.preview.adapter/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.wst.server.preview.adapter/META-INF/MANIFEST.MF
@@ -14,3 +14,4 @@
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Export-Package: org.eclipse.wst.server.preview.adapter.internal.core;x-internal:=true,
  org.eclipse.wst.server.preview.adapter.internal.ui;x-internal:=true
+Import-Package: javax.servlet;version="3.1.0"
diff --git a/plugins/org.eclipse.wst.server.preview.adapter/src/org/eclipse/wst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java b/plugins/org.eclipse.wst.server.preview.adapter/src/org/eclipse/wst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java
index cf87c8b..606207d 100644
--- a/plugins/org.eclipse.wst.server.preview.adapter/src/org/eclipse/wst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java
+++ b/plugins/org.eclipse.wst.server.preview.adapter/src/org/eclipse/wst/server/preview/adapter/internal/core/PreviewLaunchConfigurationDelegate.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2017 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
@@ -34,6 +34,7 @@
 import org.eclipse.wst.server.core.ServerCore;
 import org.eclipse.wst.server.core.ServerUtil;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
 /**
  * 
  */
@@ -46,7 +47,7 @@
 	// this array, please ensure the index of org.eclipse.wst.server.preview 
 	// corresponds to CLASSPATH_BIN_INDEX_PREVIEW_SERVER	
 	private static final String[] REQUIRED_BUNDLE_IDS = new String[] {
-		"javax.servlet",
+		getBundleForClass(javax.servlet.ServletContext.class),
 		"org.eclipse.jetty.continuation",
 		"org.eclipse.jetty.http",
 		"org.eclipse.jetty.io",
@@ -63,6 +64,13 @@
 	// running on the workbench when the plug-in is checked out
 	private static final int CLASSPATH_BIN_INDEX_PREVIEW_SERVER = REQUIRED_BUNDLE_IDS.length-1;	
 
+	/**
+	 * Gets the symbolic name of the bundle that supplies the given class.
+	 */
+	private static String getBundleForClass(Class<?> cls) {
+		return FrameworkUtil.getBundle(cls).getSymbolicName();
+	}
+
 	private static final String[] fgCandidateJavaFiles = {"javaw", "javaw.exe", "java",
 		"java.exe", "j9w", "j9w.exe", "j9", "j9.exe"};
 	private static final String[] fgCandidateJavaLocations = {"bin" + File.separatorChar,