[198999] JSF does not ask server/runtime about classpath entries
diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/META-INF/MANIFEST.MF b/jsf/plugins/org.eclipse.jst.jsf.core/META-INF/MANIFEST.MF
index 653102f..0e88e30 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.core/META-INF/MANIFEST.MF
+++ b/jsf/plugins/org.eclipse.jst.jsf.core/META-INF/MANIFEST.MF
@@ -31,7 +31,8 @@
  org.eclipse.swt;bundle-version="[3.2.0,4.0.0)",
  org.eclipse.ui;bundle-version="[3.2.0,4.0.0)",
  org.eclipse.emf.ecore.xmi;bundle-version="[2.2.0,2.4.0)",
- org.eclipse.emf.edit;bundle-version="[2.2.0,2.4.0)"
+ org.eclipse.emf.edit;bundle-version="[2.2.0,2.4.0)",
+ org.eclipse.jst.common.project.facet.core;bundle-version="[1.1.0,1.2.0)"
 Eclipse-LazyStart: true
 Export-Package: org.eclipse.jst.jsf.core,
  org.eclipse.jst.jsf.core.internal;x-friends:="org.eclipse.jst.jsf.core.tests,org.eclipse.jst.jsf.ui,org.eclipse.jst.jsf.ui.tests",
diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/plugin.xml b/jsf/plugins/org.eclipse.jst.jsf.core/plugin.xml
index 2a31e5b..3eff8fd 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.core/plugin.xml
+++ b/jsf/plugins/org.eclipse.jst.jsf.core/plugin.xml
@@ -51,11 +51,11 @@
         <config-factory class="org.eclipse.jst.jsf.core.internal.project.facet.JSFFacetInstallDataModelProvider"/>
     </action>  
       
-    <action id="jst.jsf.v11.uninstall" facet="jst.jsf" version="1.1" type="UNINSTALL">
+    <action id="jst.jsf.v11.uninstall" facet="jst.jsf" type="UNINSTALL" version="1.1" >
 	 	<delegate
         class="org.eclipse.jst.jsf.core.internal.project.facet.JSFFacetUninstallDelegate"/>
  	</action>
- 	
+	
  	<action id="jst.jsf.v12.install" facet="jst.jsf" type="INSTALL" version="[1.2">
       	<delegate class="org.eclipse.jst.jsf.core.internal.project.facet.JSFFacetInstallDelegate"/>
         <config-factory class="org.eclipse.jst.jsf.core.internal.project.facet.JSFFacetInstallDataModelProvider"/>
@@ -64,8 +64,18 @@
     <action id="jst.jsf.v12.uninstall" facet="jst.jsf" version="[1.2" type="UNINSTALL">
 	 	<delegate
         class="org.eclipse.jst.jsf.core.internal.project.facet.JSFFacetUninstallDelegate"/>
- 	</action>          	
+ 	</action>  
+	      	
   </extension> 
+  
+  <extension
+        point="org.eclipse.wst.common.project.facet.core.listeners">
+     <listener
+           class="org.eclipse.jst.jsf.core.internal.project.facet.JSFFacetPrimaryRuntimeChangedListener"
+           eventTypes="PRIMARY_RUNTIME_CHANGED">
+     </listener>
+  </extension>
+  
   <extension
         point="org.eclipse.wst.common.project.facet.core.presets">
      <static-preset
@@ -89,6 +99,7 @@
      </description>
      </static-preset>
   </extension>
+
   <extension point="org.eclipse.wst.common.project.facet.core.runtimes">
     <supported>
 		<runtime-component any="true"/>
diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java
index 7c1b932..b068f66 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java
+++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java
@@ -28,6 +28,7 @@
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jst.common.project.facet.core.ClasspathHelper;
 import org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil;
 import org.eclipse.jst.j2ee.classpathdep.IClasspathDependencyConstants;
 import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
@@ -134,7 +135,7 @@
 			path = cp.append(new Path(libref.getID()));
 			entry = getNewCPEntry(path, libref);		
 			cpEntries.add(entry);
-		}
+		} 
 
 		JSFLibraryInternalReference[] compLibs = (JSFLibraryInternalReference[])config.getProperty(IJSFFacetInstallDataModelProperties.COMPONENT_LIBRARIES);
 		for (int i=0;i<compLibs.length;i++){
@@ -147,6 +148,18 @@
 		}	
 
 		JSFLibraryRegistryUtil.setRawClasspath(javaProject, cpEntries, monitor);
+	
+		//allow for the raw classpath to be set from JSF Libs before setting the server supplied impl libs from the server, if available
+		if (config.getProperty(IJSFFacetInstallDataModelProperties.IMPLEMENTATION_TYPE_PROPERTY_NAME) 
+				== IJSFFacetInstallDataModelProperties.IMPLEMENTATION_TYPE.SERVER_SUPPLIED) {
+			try {
+				ClasspathHelper.removeClasspathEntries(project, fv);
+				ClasspathHelper.addClasspathEntries(project, fv);
+			} catch (CoreException e) {
+				JSFCorePlugin.log(IStatus.ERROR, "Unable to add server supplied implementation to the classpath.", e);//$NON-NLS-1$
+			}
+		}
+		
 	}
 
 	/**
diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetPrimaryRuntimeChangedListener.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetPrimaryRuntimeChangedListener.java
new file mode 100644
index 0000000..e855551
--- /dev/null
+++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetPrimaryRuntimeChangedListener.java
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Oracle Corporation.
+ * 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:
+ *    Oracle - initial API and implementation
+ *******************************************************************************/ 
+package org.eclipse.jst.jsf.core.internal.project.facet;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jst.common.project.facet.core.ClasspathHelper;
+import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
+import org.eclipse.jst.jsf.core.jsfappconfig.JSFAppConfigUtils;
+import org.eclipse.jst.jsf.core.jsflibraryconfiguration.JSFLibraryConfigurationHelper;
+import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
+import org.eclipse.wst.common.project.facet.core.events.IFacetedProjectEvent;
+import org.eclipse.wst.common.project.facet.core.events.IFacetedProjectListener;
+import org.eclipse.wst.common.project.facet.core.events.IPrimaryRuntimeChangedEvent;
+
+/**
+ * Handles primary runtime changed events when the JSF Facet is installed
+ * 
+ * @since JSF 1.0.1
+ */
+public class JSFFacetPrimaryRuntimeChangedListener implements IFacetedProjectListener {
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.wst.common.project.facet.core.events.IFacetedProjectListener#handleEvent(org.eclipse.wst.common.project.facet.core.events.IFacetedProjectEvent)
+	 */
+	public void handleEvent(IFacetedProjectEvent event) {
+		if (event instanceof IPrimaryRuntimeChangedEvent &&
+				getJSFFacetedVersion(event.getProject().getProject()) != null && //must be a JSF faceted project
+				JSFLibraryConfigurationHelper.isConfiguredForSystemSuppliedImplementation(event.getProject().getProject())){
+			
+			try {				
+				IProject project = event.getProject().getProject();
+				IProjectFacetVersion fv = getJSFFacetedVersion(project);
+				ClasspathHelper.removeClasspathEntries(project, fv);
+				ClasspathHelper.addClasspathEntries(project, fv);
+			} catch (CoreException e) {
+				JSFCorePlugin.log(IStatus.ERROR, "Unable to replace server supplied implementation when runtime changed.", e);//$NON-NLS-1$
+			}
+		}
+		
+	}
+
+	/**
+	 * @param project
+	 * @return IProjectFacetVersion and null if not JSF faceted
+	 */
+	private IProjectFacetVersion getJSFFacetedVersion(IProject project) {
+		return JSFAppConfigUtils.getProjectFacet(project);		
+	}
+
+}
diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java
index 9a584ad..b447e72 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java
+++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java
@@ -17,11 +17,13 @@
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jst.common.project.facet.core.ClasspathHelper;
 import org.eclipse.jst.j2ee.model.IModelProvider;
 import org.eclipse.jst.j2ee.model.ModelProviderManager;
 import org.eclipse.jst.javaee.core.ParamValue;
@@ -55,7 +57,7 @@
 
 			try {
 				// Remove JSF Libraries
-				removeJSFLibaries(project, monitor);
+				removeJSFLibaries(project, fv, monitor);
 
 				// remove servlet stuff from web.xml
 				uninstallJSFReferencesFromWebApp(project, monitor);
@@ -76,7 +78,7 @@
 	 * @param project
 	 * @param monitor
 	 */
-	private void removeJSFLibaries(final IProject project, final IProgressMonitor monitor) {
+	private void removeJSFLibaries(final IProject project, final IProjectFacetVersion fv, final IProgressMonitor monitor) {
 		 final IJavaProject jproj = JavaCore.create(project);
 		 List keptEntries = new ArrayList();
 		 try {
@@ -101,6 +103,11 @@
 			}
 		 }	
 		
+		try {
+			ClasspathHelper.removeClasspathEntries(project, fv);				
+		} catch (CoreException e) {
+			JSFCorePlugin.log(IStatus.ERROR, "Unable to remove server supplied implementation from the classpath.", e);//$NON-NLS-1$
+		}
 	}
 	
 	private void uninstallJSFReferencesFromWebApp(final IProject project,
diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/jsflibraryconfiguration/JSFLibraryConfigurationHelper.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/jsflibraryconfiguration/JSFLibraryConfigurationHelper.java
index 0f9818f..3c29cb1 100644
--- a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/jsflibraryconfiguration/JSFLibraryConfigurationHelper.java
+++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/jsflibraryconfiguration/JSFLibraryConfigurationHelper.java
@@ -70,4 +70,17 @@
 		IPath path = cpEntry.getPath();
 		return path != null && path.segmentCount() == 2 && JSF_LIBRARY_CP_CONTAINER_ID.equals(path.segment(0));
 	}
+	
+	/**
+	 * @param project
+	 * @return true if the JSF Faceted project is configured to use system supplied implementation
+	 */
+	public static boolean isConfiguredForSystemSuppliedImplementation(IProject project) {
+		Collection<JSFLibraryReference> refs = getJSFLibraryReferences(project);
+		for(JSFLibraryReference ref : refs){			
+			if (ref instanceof JSFLibraryReferenceServerSupplied)
+				return true;
+		}
+		return false;
+	}
 }