[359143] Java EE Module Dependencies" page does not reflect changes in the component file for Imported Utilities jar entries
[358729] External utility jars and variables are not shown in EAR Java EE Module Dependencies page
diff --git a/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html b/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
index c32244a..3f02117 100644
--- a/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
+++ b/features/org.eclipse.jst.enterprise_ui.feature.patch/buildnotes_org.eclipse.jst.enterprise_ui.feature.patch.html
@@ -41,6 +41,8 @@
 <p>Bug <a href='https://bugs.eclipse.org/349741'>349741</a>. Utility jars in EAR50/lib or EAR/ not listed in in EAR Java EE Module Dependencies</p>
 <p>Bug <a href='https://bugs.eclipse.org/355244'>355244</a>. EAR5.0 Java EE Module Dependencies, selecting "In Lib Dir" deletes imported utility jar from workspace and it cause a NPE.</p>
 <p>Bug <a href='https://bugs.eclipse.org/357421'>357421</a>. Utility jars in EAR50/lib or EAR/ cannot be edited in in EAR Java EE Module Dependencies page</p>
+<p>Bug <a href='https://bugs.eclipse.org/359143'>359143</a>. Java EE Module Dependencies" page does not reflect changes in the component file for Imported Utilities jar entries</p>
+<p>Bug <a href='https://bugs.eclipse.org/358729'>358729</a>. External utility jars and variables are not shown in EAR Java EE Module Dependencies page</p>
 
 </body>
 </html>
\ No newline at end of file
diff --git a/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties b/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
index b633fdb..934c929 100644
--- a/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
+++ b/features/org.eclipse.jst.enterprise_ui.feature.patch/feature.properties
@@ -58,6 +58,8 @@
 Bug https://bugs.eclipse.org/349741 Utility jars in EAR50/lib or EAR/ not listed in in EAR Java EE Module Dependencies\n\
 Bug https://bugs.eclipse.org/355244 EAR5.0 Java EE Module Dependencies, selecting "In Lib Dir" deletes imported utility jar from workspace and it cause a NPE.\n\
 Bug https://bugs.eclipse.org/357421 Utility jars in EAR50/lib or EAR/ cannot be edited in in EAR Java EE Module Dependencies page\n\
+Bug https://bugs.eclipse.org/359143 Java EE Module Dependencies" page does not reflect changes in the component file for Imported Utilities jar entries\n\
+Bug https://bugs.eclipse.org/358729 External utility jars and variables are not shown in EAR Java EE Module Dependencies page \n\
 \n\
 
 # "copyright" property - text of the "Feature Update Copyright"
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 d1aab0c..27a364a 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
@@ -465,8 +465,8 @@
 				IVirtualReference ref = oldrefs[j];
 				IVirtualComponent handle = ref.getReferencedComponent();
 				if(!j2eeComponentList.contains(handle) && (isVersion5 ? !j2eeLibElementList.contains(handle) : true)){
-					if ((handle instanceof VirtualArchiveComponent) && (isPhysicallyAdded((VirtualArchiveComponent)handle)))
-						continue;
+//					if ((handle instanceof VirtualArchiveComponent) && (isPhysicallyAdded((VirtualArchiveComponent)handle)))
+//						continue;
 					list.add(handle);
 				}
 			}
@@ -485,11 +485,11 @@
 			for (int j = 0; j < oldrefs.length; j++) {
 				IVirtualReference ref = oldrefs[j];
 				IVirtualComponent handle = ref.getReferencedComponent();
-				if (handle instanceof VirtualArchiveComponent) {
-					VirtualArchiveComponent comp = (VirtualArchiveComponent)handle;
-					if (isPhysicallyAdded(comp))
-						continue;
-				}
+//				if (handle instanceof VirtualArchiveComponent) {
+//					VirtualArchiveComponent comp = (VirtualArchiveComponent)handle;
+//					if (isPhysicallyAdded(comp))
+//						continue;
+//				}
 				if(!j2eeComponentList.contains(handle) && ref.getRuntimePath().isRoot()) {
 					list[0].add(handle);
 				}
@@ -1079,6 +1079,7 @@
 			return true;
 	}
 	
+	
 	/**
 	 * This method removes leading and ending separators from the given string wich is supposed to
 	 * contain a path
@@ -1157,9 +1158,10 @@
 					}
 					boolean shouldBeDisabled = false;
 					if (element instanceof VirtualArchiveComponent) {
-						if (isPhysicallyAdded((VirtualArchiveComponent)element) && isInLibDir((VirtualArchiveComponent)element))
+						if (isPhysicallyAdded((VirtualArchiveComponent)element) && (!isInComponentFile((IVirtualComponent)element)))
 							shouldBeDisabled = true;
-						
+						if(!shouldBeDisabled && isVersion5 && isInLibDir((VirtualArchiveComponent)element))							
+							shouldBeDisabled = true;		
 						if (shouldBeDisabled) {
 							items[i].setChecked(true);
 							items[i].setGrayed(true);
@@ -1191,7 +1193,20 @@
 		buttonColumn.setLayoutData(btndata);
 
 	}
-
+	
+	
+	private boolean isInComponentFile(IVirtualComponent comp){
+		EARVirtualComponent earVirtualCompoent = (EARVirtualComponent) earComponent;
+		IVirtualReference [] existingRefs = earVirtualCompoent.getHardReferences();
+		for(int i=0;i<existingRefs.length;i++){
+			IVirtualComponent tempoCompo = existingRefs[i].getReferencedComponent();
+			if( tempoCompo.equals(comp)){
+				return true;
+			}			
+		}		
+		return false;		
+	}
+	
 	private boolean isConflict(Object lib) {
 		IProject libProj = (lib instanceof IProject) ? (IProject)lib : ((IVirtualComponent)lib).getProject(); 
 		IProject earProject = earComponent.getProject();	
diff --git a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
index acf2bca..64fcddf 100644
--- a/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
+++ b/plugins/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/AvailableJ2EEComponentsForEARContentProvider.java
@@ -11,6 +11,7 @@
  *******************************************************************************/
 package org.eclipse.jst.j2ee.internal;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -24,6 +25,7 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jem.util.logger.proxy.Logger;
 import org.eclipse.jface.viewers.ILabelProviderListener;
 import org.eclipse.jface.viewers.IStructuredContentProvider;
@@ -150,29 +152,33 @@
 		
 		VirtualArchiveComponent comp = (VirtualArchiveComponent)component;
 		
-		// If getWorkspaceRelativePath() returns null it means whether the file doesn't exist or the method is misbehaving
-		// (This because we noticed that getWorkspaceRelativePath method returns path including ear when the jar 
-		// is "dragged and dropped" and doesn't include ear when it is imported and we didn't want to move the code there)
-		// then we try again but appending the ear name in the beginning.
-		if(comp.getWorkspaceRelativePath() != null ){
-			if(!comp.getWorkspaceRelativePath().segment(0).equals(earComponent.getName()))
-				// the first segment should be the current EAR, if not the component shouldn't be shown
-				return false;
-		}else{
-			IFile aFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(comp.getProject().getName() + IPath.SEPARATOR + comp.getName().substring(4)));
-			// First logic expression: workspace relative path will be null only if file doesn't exist or the path  has not more than one segment (is in root of workpsace)
-			if( aFile == null || ( aFile != null && !aFile.exists()) )
-				return false;
-			else
-				p = aFile.getProjectRelativePath();
-		}
+		// The ear of the component must be the same as the ear's local variable 
+		if(!comp.getProject().getName().equals(earComponent.getName()))
+			return false;
 		
-		if (p == null){
+		//if the workspace relative path is null then it can mean that the file is not in the workspace or that the file doesn't exist.  
+		if (getComponentWorkspaceRelativePath(comp) != null){
 			try {
-				p = comp.getProjectRelativePath();
+				p = getComponentProjectRelativePath(comp);
 			} catch (IllegalArgumentException e) {
 				return false;
 			}
+		}else{
+			// Determine if archive is a lib archive or a variable
+			if (comp.getArchiveType().equals(VirtualArchiveComponent.LIBARCHIVETYPE)){
+			
+			// Check if component's name describes an absolute path, meaning that the file is outside workspace
+			Path bPath = new Path(component.getName().substring(4));
+			File bFile = bPath.toFile();
+			if(bFile.exists())
+				return true;
+			}else if (comp.getArchiveType().equals(VirtualArchiveComponent.VARARCHIVETYPE)){
+				
+				IPath resolvedPath= JavaCore.getResolvedVariablePath(new Path(component.getName().substring(4)));
+				java.io.File file = new java.io.File(resolvedPath.toOSString());
+				if( file.isFile() && file.exists())
+					return true;
+			}
 		}
 		
 		// If the path relative to the project is null it means that the file doesn't exist, component shouldn't be shown.
@@ -216,6 +222,37 @@
 		return false;
 	}
 	
+	/* This method only calls the getWorkspaceRelativePath of a VirtualArchiveComponent which is now deprecated, it returns inconsistent
+	 * values so needed shielding against those inconsistencies is needed.
+	 */
+	private IPath getComponentWorkspaceRelativePath(VirtualArchiveComponent component){
+		
+		if(component.getWorkspaceRelativePath() != null){
+			// Get segments of component's IPath
+			return component.getWorkspaceRelativePath();
+		}else{
+			IFile aFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(component.getProject().getName() + IPath.SEPARATOR + component.getName().substring(4)));
+			
+			// First logic expression: workspace relative path will be null only if file doesn't exist or the path  has not more than one segment (is in root of workpsace)
+			if( aFile == null || ( aFile != null && !aFile.exists()) )
+					return null;
+			else
+				return aFile.getFullPath();
+		}
+	}
+	// This method replaces the getProjectRelativePath method of the VirtualArchiveComponent which is now deprecated, it adds some shielding. 
+	private IPath getComponentProjectRelativePath(VirtualArchiveComponent component){
+		IPath CompWorkspaceRelPath = getComponentWorkspaceRelativePath(component);
+	
+		if (CompWorkspaceRelPath != null){
+			IFile aFile = ResourcesPlugin.getWorkspace().getRoot().getFile(CompWorkspaceRelPath);
+			if (aFile.exists())
+				return aFile.getProjectRelativePath();
+		}
+		
+		return null;
+	}
+	
 	// This method determines if the provided IPaths start with the same segments 
 	private boolean startWithSameSegments(IPath iPath1, IPath iPath2){