[180563] ClassPath Model fixes for JEE5
diff --git a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/ClasspathModel.java b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/ClasspathModel.java
index 95fb757..8c3000e 100644
--- a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/ClasspathModel.java
+++ b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/ClasspathModel.java
@@ -11,6 +11,7 @@
 package org.eclipse.jst.j2ee.internal.common;
 
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -40,22 +41,21 @@
 import org.eclipse.jst.j2ee.application.internal.operations.ClassPathSelection;
 import org.eclipse.jst.j2ee.application.internal.operations.ClasspathElement;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
-import org.eclipse.jst.j2ee.commonarchivecore.internal.EARFile;
-import org.eclipse.jst.j2ee.commonarchivecore.internal.EJBJarFile;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.DeploymentDescriptorLoadException;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.ManifestException;
-import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.OpenFailureException;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifestImpl;
-import org.eclipse.jst.j2ee.componentcore.EnterpriseArtifactEdit;
 import org.eclipse.jst.j2ee.componentcore.J2EEModuleVirtualComponent;
 import org.eclipse.jst.j2ee.componentcore.util.EARArtifactEdit;
 import org.eclipse.jst.j2ee.internal.J2EEConstants;
+import org.eclipse.jst.j2ee.internal.archive.JavaEEArchiveUtilities;
 import org.eclipse.jst.j2ee.internal.common.classpath.J2EEComponentClasspathUpdater;
 import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
 import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
 import org.eclipse.jst.j2ee.model.IModelProvider;
 import org.eclipse.jst.j2ee.model.ModelProviderManager;
+import org.eclipse.jst.jee.archive.ArchiveOpenFailureException;
+import org.eclipse.jst.jee.archive.IArchive;
 import org.eclipse.wst.common.componentcore.ComponentCore;
 import org.eclipse.wst.common.componentcore.UnresolveableURIException;
 import org.eclipse.wst.common.componentcore.internal.impl.ModuleURIUtil;
@@ -72,9 +72,9 @@
 
 	protected IProject project;
 	protected IVirtualComponent selectedEARComponent;
-	protected EARFile earFile;
+	protected IArchive earFile;
 	protected IVirtualComponent component;
-	protected Archive archive;
+//	protected IArchive archive;
 	public EARArtifactEdit earArtifactEdit;
 	/** The EAR nature runtimes for all the open EAR projects in the workspace */
 	protected IVirtualComponent[] availableEARComponents = null;
@@ -155,11 +155,21 @@
 
 	public String getArchiveURI() {
 		if (selectedEARComponent != null) {
-			return getEARArtifactEdit().getModuleURI(getComponent());
+			IVirtualReference [] refs = selectedEARComponent.getReferences();
+			IVirtualComponent moduleComp = getComponent();
+			for(int i=0; i<refs.length; i++){
+				if(refs[i].getReferencedComponent().equals(moduleComp)){
+					return refs[i].getArchiveName();
+				}
+			}
 		}
 		return null;
 	}
 
+	/**
+	 * 
+	 * @deprecated - will be removed in WTP 3.0
+	 */
 	public EARArtifactEdit getEARArtifactEdit() {
 		if (earArtifactEdit == null || selectedEARComponentChanged())
 			earArtifactEdit = EARArtifactEdit.getEARArtifactEditForRead(selectedEARComponent);
@@ -181,14 +191,10 @@
 			return;
 		}
 		try {
-			IModelProvider provider = ModelProviderManager.getModelProvider(selectedEARComponent.getProject());
-			Object model = provider.getModelObject();
-			if (model instanceof org.eclipse.jst.j2ee.application.Application)
-			{
-				earFile = (EARFile) getEARArtifactEdit().asArchive(false, false);
-			}
-		} catch (OpenFailureException ex) {
-			handleOpenFailureException(ex);
+			earFile = JavaEEArchiveUtilities.INSTANCE.openArchive(selectedEARComponent);
+		} catch (ArchiveOpenFailureException aofex)
+		{
+			handleOpenFailureException(aofex);
 		}
 	}
 
@@ -200,70 +206,58 @@
 			initializeEARFile();
 			initializeArchive();
 			if (!J2EEProjectUtilities.isEARProject(getProject())) {
-				if (archive != null) {
+				if (getProject() != null) {
 					if (existing == null) {
 						if (manifest != null)
-							archive.setManifest(manifest);
+						{
+							J2EEProjectUtilities.writeManifest(getProject(), manifest);
+						}
 						else
 							// Load it now because we're going to close the EAR;
 							// this might be a binary project
-							archive.getManifest();
+							manifest = J2EEProjectUtilities.readManifest(getProject());
 					} else
-						archive.setManifest(existing);
-					List archiveFiles = earFile.getArchiveFiles();
-					for (int i = 0; i < archiveFiles.size(); i++) {
-						Archive anArchive = (Archive) archiveFiles.get(i);
-						try {
-							if (anArchive.isEJBJarFile())
-								((EJBJarFile) anArchive).getDeploymentDescriptor();
-							anArchive.getManifest();
-						} catch (ManifestException mfEx) {
-							Logger.getLogger().logError(mfEx);
-							anArchive.setManifest((ArchiveManifest) new ArchiveManifestImpl());
-						} catch (DeploymentDescriptorLoadException ddException) {
-							Logger.getLogger().logError(ddException);
+					{
+						J2EEProjectUtilities.writeManifest(getProject(), existing);
+						manifest = existing;
+					}
+					if (selectedEARComponent != null) {
+						IVirtualReference[] archiveFiles = selectedEARComponent.getReferences();
+						IVirtualComponent anArchive = null;
+						for (int i = 0; i < archiveFiles.length; i++) {
+							anArchive = archiveFiles[i].getReferencedComponent();
+							try {
+								if (J2EEProjectUtilities.isEJBComponent(anArchive)) {
+									IModelProvider modelProvider = ModelProviderManager.getModelProvider(anArchive.getProject());
+									modelProvider.getModelObject();
+								}
+								J2EEProjectUtilities.readManifest(anArchive.getProject());
+							} catch (ManifestException mfEx) {
+								Logger.getLogger().logError(mfEx);
+								// anArchive.setManifest((ArchiveManifest) new ArchiveManifestImpl());
+							} catch (DeploymentDescriptorLoadException ddException) {
+								Logger.getLogger().logError(ddException);
+							}
 						}
 					}
 				}
 				createClassPathSelection();
 			}
+		} catch (IOException e) {
+			handleOpenFailureException(e);
 		} finally {
 			if (earFile != null)
-				earFile.close();
+				JavaEEArchiveUtilities.INSTANCE.closeArchive(earFile);
 		}
 	}
 
 	protected void initializeArchive() {
-		if (!J2EEProjectUtilities.isEARProject(getProject())) {
-			if (earFile == null) {
-				archive = null;
-				return;
-			}
-			String uri = getArchiveURI();
-			if (uri != null) {
-				try {
-					archive = (Archive) earFile.getFile(uri);
-				} catch (java.io.FileNotFoundException ex) {
-					archive = null;
-				}
-			}
-		} else {
-			EnterpriseArtifactEdit edit = null;
-			try {
-				edit = EARArtifactEdit.getEARArtifactEditForRead(getProject());
-				archive = edit.asArchive(true);
-			} catch (OpenFailureException oe) {
-				Logger.getLogger().log(oe);
-			} finally {
-				if (edit != null)
-					edit.dispose();
-			}
-		}
+		// do nothing now- just use the project
 	}
 
 	protected void createClassPathSelection() {
-		if (archive != null)
-			classPathSelection = new ClassPathSelection(archive, earFile);
+		if (getComponent() != null && selectedEARComponent != null )
+			classPathSelection = new ClassPathSelection(getComponent(), selectedEARComponent);
 		else
 			classPathSelection = null;
 	}
@@ -276,7 +270,7 @@
 		return mofRoot.exists(new Path(aComponent.getRootFolder().getProjectRelativePath().toString() + "//" + J2EEConstants.APPLICATION_DD_URI)); //$NON-NLS-1$
 	}
 
-	protected void handleOpenFailureException(OpenFailureException ex) {
+	protected void handleOpenFailureException(Exception ex) {
 		org.eclipse.jem.util.logger.proxy.Logger.getLogger().logError(ex);
 	}
 
@@ -368,9 +362,10 @@
 	 * Gets the archive.
 	 * 
 	 * @return Returns a Archive
+	 * @deprecated - use the project
 	 */
 	public Archive getArchive() {
-		return archive;
+		return null;
 	}
 
 	/**
@@ -379,7 +374,7 @@
 	 */
 	public void updateManifestClasspath() {
 		if (classPathSelection != null && classPathSelection.isModified()) {
-			archive.getManifest().setClassPath(classPathSelection.toString());
+			manifest.setClassPath(classPathSelection.toString());
 			fireNotification(new ClasspathModelEvent(ClasspathModelEvent.CLASS_PATH_CHANGED));
 		}
 	}
@@ -389,7 +384,7 @@
 	 * {@link ClasspathModelEvent#MAIN_CLASS_CHANGED}
 	 */
 	public void updateMainClass(String mainClass) {
-		archive.getManifest().setMainClass(mainClass);
+		manifest.setMainClass(mainClass);
 		fireNotification(new ClasspathModelEvent(ClasspathModelEvent.MAIN_CLASS_CHANGED));
 	}
 
@@ -398,7 +393,7 @@
 	 * {@link ClasspathModelEvent#MAIN_CLASS_CHANGED}
 	 */
 	public void updateImplVersion(String implVersion) {
-		archive.getManifest().setImplemenationVersion(implVersion);
+		manifest.setImplemenationVersion(implVersion);
 		fireNotification(new ClasspathModelEvent(ClasspathModelEvent.IMPL_VERSION_CHANGED));
 	}
 
@@ -416,17 +411,19 @@
 	/**
 	 * Sets the manfest on the archive, updates the classpath selection, and notifies
 	 */
-	public void setManifest(ArchiveManifest manifest) {
-		if(archive != null){
-			archive.setManifest(manifest);
-			getClassPathSelection(); // Ensure the selection is initialized.
-			fireNotification(new ClasspathModelEvent(ClasspathModelEvent.MANIFEST_CHANGED));
+	public void setManifest(ArchiveManifest newManifest) {
+		try {
+			J2EEProjectUtilities.writeManifest(getProject(), newManifest);
+		} catch (IOException e) {
+			handleOpenFailureException(e);
 		}
+		getClassPathSelection(); // Ensure the selection is initialized.
+		fireNotification(new ClasspathModelEvent(ClasspathModelEvent.MANIFEST_CHANGED));
 	}
 
 	public void selectEAR(int index) {
 		ArchiveManifest mf = new ArchiveManifestImpl((ArchiveManifestImpl) getArchive().getManifest());
-		earFile.close();
+		JavaEEArchiveUtilities.INSTANCE.closeArchive(earFile);
 		selectedEARComponent = availableEARComponents[index];
 		initializeSelection(mf);
 		fireNotification(new ClasspathModelEvent(ClasspathModelEvent.EAR_PROJECT_CHANGED));
@@ -444,8 +441,8 @@
 
 	public void refresh() {
 		ArchiveManifest mf = null;
-		if (archive != null)
-			mf = new ArchiveManifestImpl((ArchiveManifestImpl) getArchive().getManifest());
+		if (getComponent() != null)
+			mf = J2EEProjectUtilities.readManifest(getProject());
 		refreshAvailableEARs();
 		resetClassPathSelection(mf);
 	}
diff --git a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/classpath/J2EEComponentClasspathUpdater.java b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/classpath/J2EEComponentClasspathUpdater.java
index 17fa11b..2970bfb 100644
--- a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/classpath/J2EEComponentClasspathUpdater.java
+++ b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/classpath/J2EEComponentClasspathUpdater.java
@@ -234,17 +234,20 @@
 			Object[] earProjects = earQueue.getListeners();
 			for (int i = 0; i < earProjects.length; i++) {
 				IProject earProject = (IProject) earProjects[i];
-				IVirtualComponent earComponent = ComponentCore.createComponent(earProject); 
-				IVirtualReference[] refs = J2EEProjectUtilities.getComponentReferences(earComponent);
-				IVirtualComponent comp = null;
-				for (int j = 0; j < refs.length; j++) {
-					comp = refs[j].getReferencedComponent();
-					if (!comp.isBinary()) {
-						queueModule(comp.getProject());
+				if (J2EEProjectUtilities.isEARProject(earProject))
+				{
+					IVirtualComponent earComponent = ComponentCore.createComponent(earProject); 
+					IVirtualReference[] refs = J2EEProjectUtilities.getComponentReferences(earComponent);
+					IVirtualComponent comp = null;
+					for (int j = 0; j < refs.length; j++) {
+						comp = refs[j].getReferencedComponent();
+						if (!comp.isBinary()) {
+							queueModule(comp.getProject());
+						}
 					}
-				}
-				if(null != earComponent){
-					EnterpriseBinaryComponentHelper.ArchiveCache.getInstance().clearDisconnectedArchivesInEAR(earComponent);	
+					if(null != earComponent){
+						EnterpriseBinaryComponentHelper.ArchiveCache.getInstance().clearDisconnectedArchivesInEAR(earComponent);	
+					}
 				}
 			}
 		}
diff --git a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/model/ModelProviderManager.java b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/model/ModelProviderManager.java
index 163709d..171f492 100644
--- a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/model/ModelProviderManager.java
+++ b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/model/ModelProviderManager.java
@@ -13,6 +13,7 @@
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
+import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
 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.IProjectFacetVersion;
@@ -38,6 +39,12 @@
 		return factory.create(project);
 	}
 
+	public static IModelProvider getModelProvider(IVirtualComponent aModule, IProjectFacetVersion vers) {
+
+		IModelProviderFactory factory = getProvider(vers);
+		return factory.create(aModule);
+	}
+
 	/**
 	 * Used to register an IModelProviderFactory against a facet version
 	 * @param providerFactory - {@link IModelProviderFactory}
@@ -95,6 +102,11 @@
 		
 	}
 
+	public static IModelProvider getModelProvider(IVirtualComponent aModule) {
+		IProjectFacetVersion facetVersion = getDefaultFacet(aModule);
+		return getModelProvider(aModule, facetVersion);		
+	}
+
 	private static IProjectFacetVersion getDefaultFacet(IProject proj) {
 		String type = J2EEProjectUtilities.getJ2EEProjectType(proj);
 		IProjectFacet facet = ProjectFacetsManager.getProjectFacet(type);
@@ -112,6 +124,29 @@
 			
 	}
 
+	private static IProjectFacetVersion getDefaultFacet(IVirtualComponent aModule) {
+		String type = J2EEProjectUtilities.getJ2EEComponentType(aModule);
+		IProjectFacet facet = ProjectFacetsManager.getProjectFacet(type);
+		IFacetedProject fp = null;
+		try {
+			if (aModule.isBinary())
+			{
+				
+			}
+			else
+			{
+				fp = ProjectFacetsManager.create(aModule.getProject());
+			}
+		} catch (CoreException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		if (fp != null && facet != null) {
+			return fp.getInstalledVersion(facet);
+		}
+		return null;			
+	}
+
 	private static HashMap<ModelProviderKey, IModelProviderFactory> getProviders() {
 		if (registry == null)
 			initProviders();
diff --git a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClassPathSelection.java b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClassPathSelection.java
index d9591da..b9cd5ad 100644
--- a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClassPathSelection.java
+++ b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClassPathSelection.java
@@ -15,6 +15,7 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -45,16 +46,19 @@
 import org.eclipse.jst.j2ee.commonarchivecore.internal.EARFile;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.ManifestException;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.OpenFailureException;
+import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.impl.CommonarchiveFactoryImpl;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.LoadStrategy;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.ZipFileLoadStrategyImpl;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.util.ArchiveUtil;
 import org.eclipse.jst.j2ee.componentcore.J2EEModuleVirtualComponent;
-import org.eclipse.jst.j2ee.componentcore.util.EARArtifactEdit;
 import org.eclipse.jst.j2ee.internal.archive.operations.ComponentLoadStrategyImpl;
 import org.eclipse.jst.j2ee.internal.archive.operations.EARComponentLoadStrategyImpl;
 import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
 import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
+import org.eclipse.jst.j2ee.model.IModelProvider;
+import org.eclipse.jst.j2ee.model.ModelProviderManager;
+import org.eclipse.jst.javaee.ejb.EJBJar;
 import org.eclipse.wst.common.componentcore.ComponentCore;
 import org.eclipse.wst.common.componentcore.UnresolveableURIException;
 import org.eclipse.wst.common.componentcore.internal.impl.ModuleURIUtil;
@@ -67,6 +71,8 @@
 
 public class ClassPathSelection {
 	protected Archive archive;
+	protected org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest manifest;
+	protected IVirtualComponent component;
 	protected IProject earProject;
 	protected IVirtualComponent earComponent;
 	protected List classpathElements;
@@ -86,9 +92,24 @@
 		 * @see Comparator#compare(Object, Object)
 		 */
 		public int compare(Object o1, Object o2) {
-			Archive a1 = (Archive) o1;
-			Archive a2 = (Archive) o2;
-			return a1.getURI().compareTo(a2.getURI());
+			int retVal = 0;
+			if (o1 instanceof Archive)
+			{
+				Archive a1 = (Archive) o1;
+				Archive a2 = (Archive) o2;
+				retVal = a1.getURI().compareTo(a2.getURI());
+			}
+			else if (o1 instanceof IVirtualReference)
+			{
+				IVirtualReference ref1 = (IVirtualReference) o1;
+				IVirtualReference ref2 = (IVirtualReference) o2;
+				retVal = ref1.getArchiveName().compareTo(ref2.getArchiveName());
+			}
+			else
+			{
+				retVal = o1.toString().compareTo(o2.toString());
+			}
+			return retVal;
 		}
 	};
 
@@ -109,7 +130,18 @@
 		initializeEARProject(earFile);
 		initializeElements();
 	}
-	
+
+	/**
+	 * ClassPathSelection constructor comment.
+	 */
+	public ClassPathSelection(IVirtualComponent aComponent, IVirtualComponent anEarComponent) {
+		super();
+		component = aComponent;
+		earComponent = anEarComponent;
+		earProject = earComponent.getProject();
+		initializeElements();
+	}
+
 	/**
 	 * ClassPathSelection constructor comment.
 	 */
@@ -149,6 +181,31 @@
 		return element;
 	}
 
+	protected ClasspathElement createElement(IVirtualComponent referencingArchive, IVirtualReference referencedArchive, String cpEntry) {
+		ClasspathElement element = new ClasspathElement(referencingArchive);
+		element.setValid(true);
+				
+		String uriString = referencedArchive.getArchiveName();
+		
+		element.setText(uriString);
+		element.setTargetComponent(referencedArchive.getReferencedComponent());
+		element.setEarProject(earProject);
+		if( earComponent != null ){
+			IContainer earConentFolder = earComponent.getRootFolder().getUnderlyingFolder();
+			if( earConentFolder.getType() == IResource.FOLDER ){
+				element.setEarContentFolder( earConentFolder.getName());
+			}else {
+				element.setEarContentFolder( "" );
+			}
+		}
+	
+		setProjectValues(element, referencedArchive);
+		if (cpEntry != null)
+			element.setValuesSelected(cpEntry);
+		setType(element, referencedArchive);
+		return element;
+	}
+
 	protected ClasspathElement createInvalidElement(String cpEntry) {
 		ClasspathElement element = new ClasspathElement(archive);
 		element.setValid(false);
@@ -294,26 +351,24 @@
 	private void setInvalidProject(ClasspathElement element) {
 		IProject earProj = element.getEarProject();
 		//IVirtualComponent[] component = ComponentUtilities.getComponent(earProj.getName());
-		IVirtualComponent component = ComponentUtilities.getComponent(earProj.getName());
-		EARArtifactEdit edit = null;
-		try {
-			edit = EARArtifactEdit.getEARArtifactEditForRead(component);
-		if (edit != null) {
-			String moduleName = element.getRelativeText();
-			if(moduleName != null) {
-				IVirtualComponent modComponent = edit.getModule(moduleName);
-				if(modComponent != null) {
-				IProject mappedProject = modComponent.getProject();
-				element.setProject(mappedProject);
+		IVirtualComponent refEarComponent = ComponentUtilities.getComponent(earProj.getName());
+		
+		IVirtualReference[] references = J2EEProjectUtilities.getComponentReferences(refEarComponent);
+		String moduleName = element.getRelativeText();
+		if(moduleName != null) {
+			IVirtualComponent modComponent = null;
+			for (int cnt=0; cnt < references.length; cnt++)
+			{
+				if (moduleName.equals(references[cnt].getArchiveName()))
+				{
+					modComponent = references[cnt].getReferencedComponent();
 				}
 			}
+			if(modComponent != null) {
+				IProject mappedProject = modComponent.getProject();
+				element.setProject(mappedProject);
+			}
 		}
-	  } finally {
-		  if(edit != null) {
-			  edit.dispose();
-		    edit = null;
-		  }
-	  }
 	}
 
 	/**
@@ -442,28 +497,94 @@
 		return null;
 	}
 
+	protected IVirtualReference getVirtualReference(String uri, List archives) {
+		for (int i = 0; i < archives.size(); i++) {
+			IVirtualReference anArchive = (IVirtualReference) archives.get(i);
+			
+			String archiveURIString = anArchive.getArchiveName();
+			if (archiveURIString.equals(uri))
+				return anArchive;
+		}
+		return null;
+	}
+
+	public static boolean isValidDependency(IVirtualComponent referencedJAR, IVirtualComponent referencingJAR) {
+		//No other modules should reference wars
+		if (J2EEProjectUtilities.isDynamicWebComponent(referencedJAR))
+			return false;
+
+		if (referencedJAR == referencingJAR)
+			return false;
+
+		//Clients can reference all but the WARs, which we've already covered
+		// above; WARs and EJB JARs
+		//can reference all but WARs, above, or ApplicationClients
+		return J2EEProjectUtilities.isApplicationClientComponent(referencingJAR) || !J2EEProjectUtilities.isApplicationClientComponent(referencedJAR);
+	}
+
 	protected void initializeElements() {
-		ejbToClientJARs = J2EEProjectUtilities.collectEJBClientJARs(getEARFile());
+//		ejbToClientJARs = J2EEProjectUtilities.collectEJBClientJARs(getEARFile());
+		ejbToClientJARs = new HashMap();
+		IVirtualReference[] references = earComponent.getReferences();
+		IVirtualComponent currentComponent = null;
+		IVirtualComponent clientComponent = null;
+		Object rootModelObject = null;
+		IModelProvider modelProvider = null;
+		String ejbClientJarName = null;
+		for (int cnt=0; cnt<references.length; cnt++)
+		{
+			clientComponent = null;
+			modelProvider = null;
+			rootModelObject = null;
+			ejbClientJarName = null;
+			currentComponent = references[cnt].getReferencedComponent();
+			if (J2EEProjectUtilities.isEJBComponent(currentComponent))
+			{
+				modelProvider = ModelProviderManager.getModelProvider(currentComponent);
+				rootModelObject = modelProvider.getModelObject();
+				if (rootModelObject instanceof EJBJar)
+				{
+					ejbClientJarName = ((EJBJar)rootModelObject).getEjbClientJar();
+				}
+				else if (rootModelObject instanceof org.eclipse.jst.j2ee.ejb.EJBJar)
+				{
+					ejbClientJarName = ((org.eclipse.jst.j2ee.ejb.EJBJar)rootModelObject).getEjbClientJar();
+				}
+				if (ejbClientJarName != null)
+				{
+					clientComponent = J2EEProjectUtilities.getModule(earComponent, ejbClientJarName);
+				}
+				if (clientComponent != null)
+				{
+					ejbToClientJARs.put(currentComponent, clientComponent);
+				}
+			}
+		}
 		clientToEJBJARs = reverse(ejbToClientJARs);
 		classpathElements = new ArrayList();
 		urisToElements = new HashMap();
 		
 		String[] cp = new String[0];
 		try {
-			cp = archive.getManifest().getClassPathTokenized();
+//			cp = archive.getManifest().getClassPathTokenized();
+			manifest = J2EEProjectUtilities.readManifest(component.getProject());
+			cp = manifest.getClassPathTokenized();
 		} catch (ManifestException ex) {
 			Logger.getLogger().logError(ex);
 		}
-		List archives = getEARFile().getArchiveFiles();
+		String projectUri = earComponent.getReference(component.getName()).getArchiveName();
+//		List archives = getEARFile().getArchiveFiles();
+		List archives = new ArrayList(Arrays.asList(earComponent.getReferences()));
 		ClasspathElement element = null;
-		Archive other = null;
+//		Archive other = null;
+		IVirtualReference other;
 		for (int i = 0; i < cp.length; i++) {
 			String cpEntry = cp[i];
-			String uri = ArchiveUtil.deriveEARRelativeURI(cpEntry, archive);
+			String uri = ArchiveUtil.deriveEARRelativeURI(cpEntry, projectUri);
 
-			other = getArchive(uri, archives);
-			if (other != null && ArchiveUtil.isValidDependency(other, archive)) {
-				element = createElement(archive, other, cpEntry);
+			other = getVirtualReference(uri, archives);
+			if (other != null && isValidDependency(other.getReferencedComponent(), component)) {
+				element = createElement(component, other, cpEntry);
 				archives.remove(other);
 			} else {
 				element = createInvalidElement(cpEntry);
@@ -472,7 +593,7 @@
 					element.setProject(getProject(archive));
 				}
 				if (other != null)
-					element.setProject(getProject(other));
+					element.setProject(other.getReferencedComponent().getProject());
 				
 				if( other == null ){
 					//making a best guess for the project name
@@ -491,14 +612,14 @@
 		}
 		
 		// Add resolved contributions from tagged classpath entries
-		IVirtualComponent comp = getComponent(archive);
+//		IVirtualComponent comp = getComponent(archive);
 
 		// XXX Don't show resolved contributions from tagged classpath entries on this project's classpath; we should elements corresponding to the raw entries instead
 		//createClasspathComponentDependencyElements(comp);
 		
 		// Add elements for raw classpath entries (either already tagged or potentially taggable) 
 		try {
-		    createClasspathEntryElements(comp, IClasspathDependencyConstants.RUNTIME_MAPPING_INTO_CONTAINER_PATH);
+		    createClasspathEntryElements(component, IClasspathDependencyConstants.RUNTIME_MAPPING_INTO_CONTAINER_PATH);
 		} catch (CoreException ce) {
 			Logger.getLogger(J2EEPlugin.PLUGIN_ID).logError(ce);
 		}
@@ -507,14 +628,14 @@
 		//Anything that remains in the list of available archives that is valid should be
 		//available for selection
 		for (int i = 0; i < archives.size(); i++) {
-			other = (Archive) archives.get(i);
+			other = (IVirtualReference) archives.get(i);
 			
-			if (other != archive && ArchiveUtil.isValidDependency(other, archive)) {
-				IProject project = getProject(other);
+			if (other != archive && isValidDependency(other.getReferencedComponent(), component)) {
+				IProject project = other.getReferencedComponent().getProject();
 				if (null == targetProjectName || null == project || !project.getName().equals(targetProjectName)) {
-					element = createElement(archive, other, null);
-					element.setProject(getProject(other));
-					addClasspathElement(element, other.getURI());
+					element = createElement(component, other, null);
+					element.setProject(other.getReferencedComponent().getProject());
+					addClasspathElement(element, other.getArchiveName());
 				}
 			}
 		}
@@ -700,6 +821,15 @@
 			element.setJarType(ClasspathElement.EJB_JAR);
 	}
 
+	private void setType(ClasspathElement element, IVirtualReference other) {
+		if (other == null)
+			return;
+		else if (clientToEJBJARs.containsKey(other))
+			element.setJarType(ClasspathElement.EJB_CLIENT_JAR);
+		else if (J2EEProjectUtilities.isEJBComponent(other.getReferencedComponent()))
+			element.setJarType(ClasspathElement.EJB_JAR);
+	}
+
 	/**
 	 * @param localejbToClientJARs
 	 * @return
@@ -771,6 +901,38 @@
 			element.setImportedJarPaths(paths);
 	}
 
+	protected void setProjectValues(ClasspathElement element, IVirtualReference referencedArchive) {
+		IProject p = referencedArchive.getReferencedComponent().getProject();
+		if (p == null)
+			return;
+
+		element.setProject(p);
+		//Handle the imported jars in the project
+		String[] cp = null;
+		try {
+//			cp = referencedArchive.getManifest().getClassPathTokenized();
+			ArchiveManifest referencedManifest = J2EEProjectUtilities.readManifest(p);
+			cp = referencedManifest.getClassPathTokenized();
+		} catch (ManifestException mfEx) {
+			Logger.getLogger().logError(mfEx);
+			cp = new String[]{};
+		}
+		List paths = new ArrayList(cp.length);
+		for (int i = 0; i < cp.length; i++) {
+
+			IFile file = null;
+			try {
+				file = p.getFile(cp[i]);
+			} catch (IllegalArgumentException invalidPath) {
+				continue;
+			}
+			if (file.exists())
+				paths.add(file.getFullPath());
+		}
+		if (!paths.isEmpty())
+			element.setImportedJarPaths(paths);
+	}
+
 	public String toString() {
 		StringBuffer sb = new StringBuffer();
 		for (int i = 0; i < classpathElements.size(); i++) {
@@ -849,6 +1011,17 @@
 		return (ClasspathElement) urisToElements.get(uri);
 	}
 
+	public ClasspathElement getClasspathElement(IVirtualComponent archiveComponent) {
+		if (archiveComponent != null) {
+			for (int i = 0; i < classpathElements.size(); i++) {
+				ClasspathElement elmnt = (ClasspathElement) classpathElements.get(i);
+				if (archiveComponent.equals(elmnt.getComponent()))
+					return elmnt;
+			}
+		}
+		return null;
+	}
+
 	public ClasspathElement getClasspathElement(IProject archiveProject) {
 		if (archiveProject != null) {
 			for (int i = 0; i < classpathElements.size(); i++) {
@@ -986,22 +1159,22 @@
 	 */
 	public ClasspathElement getOppositeElement(ClasspathElement element) {
 		String uri = element.getText();
-		Archive target = element.getTargetArchive();
+		IVirtualComponent target = element.getTargetComponent();
 		if (uri == null || target == null)
 			return null;
-		Archive oppositeJAR = null;
+		IVirtualComponent oppositeJAR = null;
 		switch (element.getJarType()) {
 			case (ClasspathElement.EJB_CLIENT_JAR) :
-				oppositeJAR = (Archive) clientToEJBJARs.get(target);
+				oppositeJAR = (IVirtualComponent) clientToEJBJARs.get(target);
 				break;
 			case (ClasspathElement.EJB_JAR) :
-				oppositeJAR = (Archive) ejbToClientJARs.get(target);
+				oppositeJAR = (IVirtualComponent) ejbToClientJARs.get(target);
 				break;
 			default :
 				break;
 		}
 		if (oppositeJAR != null)
-			return getClasspathElement(oppositeJAR.getURI());
+			return getClasspathElement(oppositeJAR);
 
 		return null;
 	}
@@ -1017,8 +1190,8 @@
 	public boolean isMyClientJAR(ClasspathElement element) {
 		if (element == null || ejbToClientJARs == null)
 			return false;
-		Archive myClientJar = (Archive) ejbToClientJARs.get(archive);
-		return myClientJar != null && myClientJar == element.getTargetArchive();
+		IVirtualComponent myClientJar = (IVirtualComponent) ejbToClientJARs.get(component);
+		return myClientJar != null && myClientJar == element.getTargetComponent();
 	}
 
 	public Map getUrisToElements() {
diff --git a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClasspathElement.java b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClasspathElement.java
index 4810d51..3f3544d 100644
--- a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClasspathElement.java
+++ b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/application/internal/operations/ClasspathElement.java
@@ -80,6 +80,8 @@
 	protected ClassPathSelection parentSelection;
 	protected URI archiveURI;
 	protected String earContentFolder;
+	protected IVirtualComponent component;
+	protected IVirtualComponent targetComponent;
 
 	public ClasspathElement(Archive anArchive) {
 		super();
@@ -96,12 +98,37 @@
 		archiveURI = aArchiveURI;
 	}	
 
+	public ClasspathElement(IVirtualComponent aComponent) {
+		super();
+		this.component = aComponent;
+		this.project = aComponent.getProject();
+	}
+
 	protected void computeRelativeText() {
 		if (archive != null) {
 			relativeText = J2EEProjectUtilities.computeRelativeText(archive.getURI(), getText());
 			if (relativeText == null)
 				relativeText = getText();
 		}
+		if (component != null)
+		{
+			IVirtualComponent earComponent = ComponentCore.createComponent(earProject);
+			IVirtualReference[] refs = earComponent.getReferences();
+			IVirtualReference reference = null;
+			String archiveURI = null;
+			for (int i = 0; i < refs.length; i++) {
+				reference = refs[i];
+				if( component.equals(reference.getReferencedComponent())){
+					archiveURI = reference.getArchiveName();
+				}
+			}
+			if (archiveURI != null)
+			{
+				relativeText = J2EEProjectUtilities.computeRelativeText(archiveURI, getText());
+			}
+			if (relativeText == null)
+				relativeText = getText();
+		}
 	}
 
 	/**
@@ -513,12 +540,23 @@
 	}
 
 	/**
+	 * @return
+	 */
+	public IVirtualComponent getTargetComponent() {
+		return targetComponent;
+	}
+
+	/**
 	 * @param archive
 	 */
 	public void setTargetArchive(Archive archive) {
 		targetArchive = archive;
 	}
 
+	public void setTargetComponent(IVirtualComponent aTargetComponent) {
+		targetComponent = aTargetComponent;
+	}
+
 	public URI getArchiveURI() {
 		return archiveURI;
 	}
diff --git a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARArtifactEdit.java b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARArtifactEdit.java
index ae6c793..12eec7d 100644
--- a/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARArtifactEdit.java
+++ b/plugins/org.eclipse.jst.j2ee/earproject/org/eclipse/jst/j2ee/componentcore/util/EARArtifactEdit.java
@@ -486,6 +486,7 @@
 	 * ".rar") which allows users to get a IVirtualComponent for a given entry in an application.xml
 	 * 
 	 * @return - a IVirtualComponent for module name
+	 * @deprecated - see {@link J2EEProjectUtilities.getModule(IVirtualComponent earComponent, String moduleName)}
 	 */
 	public IVirtualComponent getModule(String moduleName) {
 		if (moduleName == null)
diff --git a/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/internal/project/J2EEProjectUtilities.java b/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/internal/project/J2EEProjectUtilities.java
index 551d328..b668937 100644
--- a/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/internal/project/J2EEProjectUtilities.java
+++ b/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/internal/project/J2EEProjectUtilities.java
@@ -69,6 +69,7 @@
 import org.eclipse.jst.j2ee.internal.componentcore.JCABinaryComponentHelper;
 import org.eclipse.jst.j2ee.internal.componentcore.WebBinaryComponentHelper;
 import org.eclipse.jst.j2ee.internal.moduleextension.EarModuleManager;
+import org.eclipse.jst.j2ee.internal.plugin.IJ2EEModuleConstants;
 import org.eclipse.jst.j2ee.project.facet.IJ2EEFacetConstants;
 import org.eclipse.jst.j2ee.project.facet.IJavaProjectMigrationDataModelProperties;
 import org.eclipse.jst.j2ee.project.facet.JavaProjectMigrationDataModelProvider;
@@ -990,6 +991,28 @@
 	}
 
 	/**
+	 * This method will return the an IVirtualComponent for the given module name. The method take
+	 * either moduleName or moduleName + ".module_extension" (module_extension = ".jar" || ".war" ||
+	 * ".rar") which allows users to get a IVirtualComponent for a given entry in an application.xml
+	 * 
+	 * @return - a IVirtualComponent for module name
+	 */
+	public static IVirtualComponent getModule(IVirtualComponent earComponent, String moduleName) {
+		if (moduleName == null)
+			return null;
+		if (moduleName.endsWith(IJ2EEModuleConstants.JAR_EXT) || moduleName.endsWith(IJ2EEModuleConstants.WAR_EXT) || moduleName.endsWith(IJ2EEModuleConstants.RAR_EXT))
+			moduleName = moduleName.substring(0, (moduleName.length() - IJ2EEModuleConstants.JAR_EXT.length()));
+		IVirtualReference[] references = getComponentReferences(earComponent);
+		for (int i = 0; i < references.length; i++) {
+			IVirtualComponent component = references[i].getReferencedComponent();
+			if (component.getName().equals(moduleName)) {
+				return component;
+			}
+		}
+		return null;
+	}
+
+	/**
 	 * This method will return the list of IVirtualReferences for the J2EE module components
 	 * contained in this EAR application.
 	 *