[87474]commited for NA
diff --git a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
index e52ad7b..af6f91c 100644
--- a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
+++ b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AddModulestoEARPropertiesPage.java
@@ -49,6 +49,7 @@
 import org.eclipse.wst.common.componentcore.ComponentCore;
 import org.eclipse.wst.common.componentcore.datamodel.properties.ICreateReferenceComponentsDataModelProperties;
 import org.eclipse.wst.common.componentcore.internal.operation.CreateReferenceComponentsDataModelProvider;
+import org.eclipse.wst.common.componentcore.internal.resources.VirtualArchiveComponent;
 import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
 import org.eclipse.wst.common.componentcore.resources.ComponentHandle;
 import org.eclipse.wst.common.componentcore.resources.IFlexibleProject;
@@ -242,7 +243,8 @@
 		if (selected != null) {
 			for (int i= 0; i < selected.length; i++) {
 				
-				IVirtualComponent archive = ComponentCore.createArchiveComponent( earComponent.getProject(), "lib/" +
+				String type = VirtualArchiveComponent.LIBARCHIVETYPE + IPath.SEPARATOR;
+				IVirtualComponent archive = ComponentCore.createArchiveComponent( earComponent.getProject(), type +
 							selected[i].toString());
 				
 				ArrayList vlist = new ArrayList();
@@ -276,39 +278,38 @@
 			refresh();
 			ArrayList result= new ArrayList();
 			for (int i = 0; i < paths.length; i++) {
-				//CPListElement elem= new CPListElement(fCurrJProject, IClasspathEntry.CPE_VARIABLE, paths[i], null);
 				IPath resolvedPath= JavaCore.getResolvedVariablePath(paths[i]);
 
+				java.io.File file = new java.io.File(resolvedPath.toOSString());
+				if( file.isFile() && file.exists()){
+					String type = VirtualArchiveComponent.VARARCHIVETYPE + IPath.SEPARATOR;
+					
+					IVirtualComponent archive = ComponentCore.createArchiveComponent( earComponent.getProject(), type +
+								paths[i].toString());
+					
+					ArrayList vlist = new ArrayList();
+					IVirtualReference[] oldrefs = earComponent.getReferences();
+					for (int j = 0; j < oldrefs.length; j++) {
+						IVirtualReference ref = (IVirtualReference) oldrefs[j];
+						vlist.add(ref);
+					}		
 				
-				IVirtualComponent archive = ComponentCore.createArchiveComponent( earComponent.getProject(), "var/" +
-							paths[i].toString());
-				
-				ArrayList vlist = new ArrayList();
-				IVirtualReference[] oldrefs = earComponent.getReferences();
-				for (int j = 0; j < oldrefs.length; j++) {
-					IVirtualReference ref = (IVirtualReference) oldrefs[j];
-					vlist.add(ref);
-				}		
-			
-				//To do: check if archive component already exists
-				IVirtualReference ref = ComponentCore.createReference( earComponent, archive );
-				vlist.add(ref);	
-				
-				IVirtualReference[] refs = new IVirtualReference[vlist.size()];
-				for (int j = 0; j < vlist.size(); j++) {
-					IVirtualReference tmpref = (IVirtualReference) vlist.get(j);
-					refs[j] = tmpref;
-				}				
-				earComponent.setReferences(refs);
-				
-//				elem.setIsMissing((resolvedPath == null) || !resolvedPath.toFile().exists());
-//				if (!existingElements.contains(elem)) {
-//					result.add(elem);
-//				}
+					//To do: check if archive component already exists
+					IVirtualReference ref = ComponentCore.createReference( earComponent, archive );
+					vlist.add(ref);	
+					
+					IVirtualReference[] refs = new IVirtualReference[vlist.size()];
+					for (int j = 0; j < vlist.size(); j++) {
+						IVirtualReference tmpref = (IVirtualReference) vlist.get(j);
+						refs[j] = tmpref;
+					}				
+					earComponent.setReferences(refs);
+				}else{
+					//display error
+				}
 			}
-			//return (CPListElement[]) result.toArray(new CPListElement[result.size()]);
+			refresh();
 		}	
-
 	}
 	
 	protected void createTableComposite(Composite parent) {
@@ -339,7 +340,7 @@
 		selectAllButton = createPushButton(SELECT_ALL_BUTTON);
 		deselectAllButton = createPushButton(DE_SELECT_ALL_BUTTON);
 		externalJarButton = createPushButton(J2EEUIMessages.getResourceString("EXTERNAL_JAR"));//$NON-NLS-1$
-		//addVariableButton = createPushButton(J2EEUIMessages.getResourceString("ADDVARIABLE"));//$NON-NLS-1$
+		addVariableButton = createPushButton(J2EEUIMessages.getResourceString("ADDVARIABLE"));//$NON-NLS-1$
 	}
 
 	protected Button createPushButton(String label) {
diff --git a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/VirtualArchiveComponentAdapterFactory.java b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/VirtualArchiveComponentAdapterFactory.java
new file mode 100644
index 0000000..2e71462
--- /dev/null
+++ b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/VirtualArchiveComponentAdapterFactory.java
@@ -0,0 +1,25 @@
+package org.eclipse.jst.j2ee.internal.common;
+
+import org.eclipse.core.runtime.IAdapterFactory;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jst.common.componentcore.util.ComponentUtilities;
+import org.eclipse.wst.common.componentcore.internal.resources.VirtualArchiveComponent;
+
+public class VirtualArchiveComponentAdapterFactory implements IAdapterFactory {
+
+	public Object getAdapter(Object adaptableObject, Class adapterType) {
+		if (adapterType == VirtualArchiveComponent.ADAPTER_TYPE ) {
+			VirtualArchiveComponent component = (VirtualArchiveComponent) adaptableObject;
+
+			IPath path = ComponentUtilities.getResolvedPathForArchiveComponent(component.getComponentHandle().getName());
+			return path;
+		}
+		return null;
+	}
+
+	
+	public Class[] getAdapterList() {
+		return new Class[]{VirtualArchiveComponent.class};
+	}	
+
+}
diff --git a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/operations/UpdateJavaBuildPathOperation.java b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/operations/UpdateJavaBuildPathOperation.java
index 1e8c01c..bd35a3d 100644
--- a/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/operations/UpdateJavaBuildPathOperation.java
+++ b/plugins/org.eclipse.jst.j2ee/common/org/eclipse/jst/j2ee/internal/common/operations/UpdateJavaBuildPathOperation.java
@@ -82,6 +82,8 @@
 			case IClasspathEntry.CPE_LIBRARY :
 				newEntry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), true);
 				break;
+			case IClasspathEntry.CPE_VARIABLE:
+				newEntry = JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath());
 			default :
 				break;
 		}
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 0be7acd..d0a14e2 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
@@ -41,6 +41,8 @@
 import org.eclipse.jst.j2ee.internal.archive.operations.EARComponentLoadStrategyImpl;
 import org.eclipse.jst.j2ee.internal.project.J2EEComponentUtilities;
 import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
+import org.eclipse.wst.common.componentcore.UnresolveableURIException;
+import org.eclipse.wst.common.componentcore.internal.impl.ModuleURIUtil;
 import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
 import org.eclipse.wst.common.componentcore.resources.IVirtualReference;
 
@@ -150,6 +152,18 @@
 		addClasspathElement(element,element.getProjectName());
 		return element;
 	}
+	
+	
+	protected ClasspathElement createArchiveElement(URI uri, String name, String cpEntry) {
+		ClasspathElement element = new ClasspathElement(uri);
+		element.setValid(false);
+		element.setRelativeText(name);
+		if (cpEntry != null)
+			element.setValuesSelected(cpEntry);		
+		element.setText(name);
+		element.setEarProject(earProject);
+		return element;
+	}	
 
 	/**
 	 * @param element
@@ -280,10 +294,6 @@
 			}
 			if (archiveURIString.equals(uri))
 				return anArchive;
-			
-			
-//			if (anArchive.getURI().equals(uri))
-//				return anArchive;
 		}
 		return null;
 	}
@@ -334,41 +344,59 @@
 				}
 			}
 		}
-		List classPathArchives = loadClassPathArchives();
-		for (int i = 0; i < classPathArchives.size(); i++) {
-			other = (Archive) classPathArchives.get(i);
-			if (other != archive && ArchiveUtil.isValidDependency(other, archive)) {
-				IProject project = getProject(other);
-				if (null == targetProjectName || null == project || !project.getName().equals(targetProjectName)) {
-					boolean inClassPath = false;
-					for (int j = 0; j < cp.length; j++) {
-						String cpEntry = cp[j];
-						
-						if( other != null && other.getName().equals(cpEntry)){
-							if( isClassPathArchive(cpEntry, classPathArchives) ){
-								other = getClassPathArchive(cpEntry, classPathArchives);
-								if (other != null && ArchiveUtil.isValidDependency(other, archive)) {
-									element = createElement(archive, other, cpEntry);
-									//archives.remove(other);
-									//classPathArchives.remove(other);
-									inClassPath = true;
-									break;
-								}
-							}
+		
+		if( earComponent!= null){
+			IVirtualReference[] newrefs = earComponent.getReferences();
+			for( int i=0; i < newrefs.length; i++){
+				IVirtualReference ref = newrefs[i];
+				IVirtualComponent referencedComponent = ref.getReferencedComponent();
+				boolean isBinary = referencedComponent.isBinary();
+				if( isBinary ){
+					String uri = ComponentUtilities.getResolvedPathForArchiveComponent(referencedComponent.getName()).toString();
+					String unresolvedURI = "";
+					try {
+						unresolvedURI = ModuleURIUtil.getArchiveName(URI.createURI(referencedComponent.getComponentHandle().toString()));
+					} catch (UnresolveableURIException e) {
+						e.printStackTrace();
+					}
+					URI archiveURI = URI.createURI(unresolvedURI);	
+					
+					boolean  alreadyInList = false;
+					Iterator iter = getClasspathElements().iterator();
+					while(iter.hasNext()){
+						ClasspathElement tmpelement = (ClasspathElement)iter.next();
+						if( tmpelement.getText().equals(archiveURI.lastSegment())){
+							alreadyInList = true;
+							break;
 						}
 					}
-					if( !inClassPath ){
-						element = createElement(archive, other, null);
-						element.setProject(getProject(other));
-						addClasspathElement(element, other.getURI());							
-					}					
-
+					
+					if( !alreadyInList ){
+						if( inManifest(cp, archiveURI.lastSegment())){
+							element = createArchiveElement(URI.createURI(referencedComponent.getComponentHandle().toString()), archiveURI.lastSegment(), archiveURI.lastSegment());
+							addClasspathElement(element, unresolvedURI);
+						}else{
+							element = createArchiveElement(URI.createURI(referencedComponent.getComponentHandle().toString()), archiveURI.lastSegment(), null);
+							addClasspathElement(element, unresolvedURI);							
+						}
+					}
 				}
 			}
-		}
-		
+		}	
 	}
 
+	boolean inManifest(String[] cp, String archiveName ){
+		boolean result = false;
+		String cpEntry = "";
+		for (int i = 0; i < cp.length; i++) {
+			cpEntry = cp[i];
+			if( archiveName.equals(cpEntry)){
+				result = true;
+			}
+		}
+		return result;
+	}
+		
 	protected List loadClassPathArchives(){
 		LoadStrategy loadStrat = archive.getLoadStrategy();
 		
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 9f77457..29ef818 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
@@ -16,19 +16,24 @@
 import java.util.List;
 import java.util.Set;
 
+
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.emf.common.util.URI;
 import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jem.util.logger.proxy.Logger;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.EARFile;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.File;
 import org.eclipse.jst.j2ee.commonarchivecore.internal.util.ArchiveUtil;
 import org.eclipse.jst.j2ee.internal.archive.operations.J2EEImportConstants;
 import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
+import org.eclipse.wst.common.componentcore.UnresolveableURIException;
+import org.eclipse.wst.common.componentcore.internal.impl.ModuleURIUtil;
 
 
 public class ClasspathElement {
@@ -60,6 +65,7 @@
 	protected List importedJarPaths;
 	protected IProject earProject;
 	protected ClassPathSelection parentSelection;
+	protected URI archiveURI;
 
 	public ClasspathElement(Archive anArchive) {
 		super();
@@ -70,6 +76,11 @@
 		super();
 		this.project = project;
 	}
+	
+	public ClasspathElement(URI  aArchiveURI) {
+		super();
+		archiveURI = aArchiveURI;
+	}	
 
 	protected void computeRelativeText() {
 		if (archive != null) {
@@ -169,6 +180,25 @@
 		return earProject.getFile(getText());
 	}
 
+	public IClasspathEntry[] newClasspathEntriesForArchive() {
+		if( !archiveURI.equals("")){
+			String resourcePath = "";
+			try {
+				resourcePath = ModuleURIUtil.getArchiveName(archiveURI);
+			} catch (UnresolveableURIException e) {
+				Logger.getLogger().logError(e);
+			}
+			java.io.File file = new java.io.File(resourcePath);
+			if( file.exists()){
+				return new IClasspathEntry[]{JavaCore.newLibraryEntry( new Path(resourcePath), null, null)};
+			}else{
+				return new IClasspathEntry[]{JavaCore.newVariableEntry( new Path(resourcePath), null, null)};
+			}
+		}	
+		return new IClasspathEntry[0];
+	}
+	
+	
 	/**
 	 * Adapter method to convert this manifest class path element to zero or more classpath entries
 	 * for a java build path
@@ -179,20 +209,10 @@
 		visited.add(this);
 		if (representsImportedJar())
 			return new IClasspathEntry[]{JavaCore.newLibraryEntry(getImportedJarAsIFile().getFullPath(), null, null)};
-
-		//if( archiveComponent())
-			//return new IClasspathEntry[]{JavaCore.newLibraryEntry(getImportedJarAsIFile().getFullPath(), null, null)};
 		
-		if( targetArchive != null ){
-			String uri = targetArchive.getOriginalURI();
-
-			java.io.File file = new java.io.File(uri);
-			if( file.exists()){
-				return new IClasspathEntry[]{JavaCore.newLibraryEntry( new Path(uri), null, null)};
-			}
-
-		}	
-		
+		if( archiveURI != null && !archiveURI.equals("") ){
+			return newClasspathEntriesForArchive();
+		}
 		if (!valid && isSelected())
 			return new IClasspathEntry[0];
 
diff --git a/plugins/org.eclipse.jst.j2ee/j2eeplugin/org/eclipse/jst/j2ee/internal/plugin/J2EEPlugin.java b/plugins/org.eclipse.jst.j2ee/j2eeplugin/org/eclipse/jst/j2ee/internal/plugin/J2EEPlugin.java
index 0e8c4c9..1528f94 100644
--- a/plugins/org.eclipse.jst.j2ee/j2eeplugin/org/eclipse/jst/j2ee/internal/plugin/J2EEPlugin.java
+++ b/plugins/org.eclipse.jst.j2ee/j2eeplugin/org/eclipse/jst/j2ee/internal/plugin/J2EEPlugin.java
@@ -53,6 +53,7 @@
 import org.eclipse.jst.j2ee.internal.application.impl.ApplicationFactoryImpl;
 import org.eclipse.jst.j2ee.internal.application.impl.ApplicationResourceFactory;
 import org.eclipse.jst.j2ee.internal.client.impl.ApplicationClientResourceFactory;
+import org.eclipse.jst.j2ee.internal.common.VirtualArchiveComponentAdapterFactory;
 import org.eclipse.jst.j2ee.internal.common.impl.J2EEResourceFactoryRegistry;
 import org.eclipse.jst.j2ee.internal.modulecore.util.EarEditAdapterFactory;
 import org.eclipse.jst.j2ee.internal.validation.ResourceUtil;
@@ -60,6 +61,7 @@
 import org.eclipse.wst.common.componentcore.internal.ArtifactEditModel;
 import org.eclipse.wst.common.componentcore.internal.impl.ReferencedComponentXMIResourceFactory;
 import org.eclipse.wst.common.componentcore.internal.impl.WTPResourceFactoryRegistry;
+import org.eclipse.wst.common.componentcore.internal.resources.VirtualArchiveComponent;
 import org.eclipse.wst.common.frameworks.internal.WTPPlugin;
 import org.eclipse.wst.common.frameworks.internal.operations.IHeadlessRunnableWithProgress;
 import org.eclipse.wst.common.internal.emf.resource.ReferencedXMIFactoryImpl;
@@ -501,6 +503,8 @@
 		ApplicationClientResourceFactory.register(WTPResourceFactoryRegistry.INSTANCE);
 		WSDLServiceExtensionRegistry.getInstance();
 		
+		manager.registerAdapters(new VirtualArchiveComponentAdapterFactory(), VirtualArchiveComponent.class );
+		
 	}
 
 	/*