| /******************************************************************************* |
| * Copyright (c) 2005, 2008 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 |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| * Stefan Dimov, stefan.dimov@sap.com - bug 207826 |
| *******************************************************************************/ |
| package org.eclipse.jst.j2ee.internal; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.core.resources.IContainer; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IWorkspaceRoot; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.jem.util.logger.proxy.Logger; |
| import org.eclipse.jface.viewers.ILabelProviderListener; |
| import org.eclipse.jface.viewers.IStructuredContentProvider; |
| import org.eclipse.jface.viewers.ITableLabelProvider; |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.jst.j2ee.application.internal.operations.ClassPathSelection; |
| import org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil; |
| import org.eclipse.jst.j2ee.classpathdep.IClasspathDependencyConstants; |
| import org.eclipse.jst.j2ee.componentcore.J2EEModuleVirtualComponent; |
| import org.eclipse.jst.j2ee.internal.common.J2EEVersionUtil; |
| import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities; |
| import org.eclipse.jst.j2ee.model.IEARModelProvider; |
| import org.eclipse.jst.j2ee.model.IModelProvider; |
| import org.eclipse.jst.j2ee.model.ModelProviderManager; |
| import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities; |
| import org.eclipse.jst.javaee.application.Application; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.wst.common.componentcore.ComponentCore; |
| import org.eclipse.wst.common.componentcore.ModuleCoreNature; |
| import org.eclipse.wst.common.componentcore.internal.resources.VirtualArchiveComponent; |
| import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; |
| import org.eclipse.wst.common.componentcore.resources.IVirtualReference; |
| |
| public class AvailableJ2EEComponentsForEARContentProvider implements IStructuredContentProvider, ITableLabelProvider { |
| |
| final static String PATH_SEPARATOR = String.valueOf(IPath.SEPARATOR); |
| private final static int COMPONENT_NUM_OF_SEGMENTS = 1; |
| |
| private int j2eeVersion; |
| private IVirtualComponent earComponent; |
| private boolean isEE5 = false; |
| private String libDir = null; |
| |
| |
| public AvailableJ2EEComponentsForEARContentProvider(IVirtualComponent aEarComponent, int j2eeVersion) { |
| super(); |
| this.j2eeVersion = j2eeVersion; |
| earComponent = aEarComponent; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
| */ |
| public Object[] getElements(Object inputElement) { |
| if (null != earComponent){ |
| isEE5 = J2EEProjectUtilities.isJEEProject(earComponent.getProject()); |
| } |
| Object[] empty = new Object[0]; |
| if (!(inputElement instanceof IWorkspaceRoot)) |
| return empty; |
| IProject[] projects = ((IWorkspaceRoot) inputElement).getProjects(); |
| if (projects == null || projects.length == 0) |
| return empty; |
| List validCompList = new ArrayList(); |
| Map pathToComp = new HashMap(); |
| for (int i = 0; i < projects.length; i++) { |
| // get flexible project |
| IProject project = projects[i]; |
| if(ModuleCoreNature.isFlexibleProject(project)){ |
| IVirtualComponent component = ComponentCore.createComponent(project); |
| if (J2EEProjectUtilities.isApplicationClientProject(project) || |
| J2EEProjectUtilities.isEJBProject(project) || |
| J2EEProjectUtilities.isDynamicWebProject(project) || |
| J2EEProjectUtilities.isJCAProject(project) || |
| J2EEProjectUtilities.isUtilityProject(project) ){ |
| int compJ2EEVersion = J2EEVersionUtil.convertVersionStringToInt(component); |
| if( compJ2EEVersion <= j2eeVersion){ |
| validCompList.add(component); |
| } else if(isEE5){ |
| validCompList.add(component); |
| } |
| }else if(null != earComponent && J2EEProjectUtilities.isEARProject(project)){ |
| //find the ArchiveComponent |
| if (component.equals( earComponent )) { |
| if (isEE5) { |
| String earDDVersion = JavaEEProjectUtilities.getJ2EEDDProjectVersion(project); |
| boolean isDDVersion5 = earDDVersion.equals(J2EEVersionConstants.VERSION_5_0_TEXT) ? true : false; |
| if (isDDVersion5) { |
| Application app = (Application)ModelProviderManager.getModelProvider(project).getModelObject(); |
| if (libDir == null) |
| libDir = app.getLibraryDirectory(); |
| if (libDir == null) |
| libDir = J2EEConstants.EAR_DEFAULT_LIB_DIR; |
| } |
| } |
| IVirtualReference[] newrefs = component.getReferences(); |
| for( int k=0; k< newrefs.length; k++ ){ |
| IVirtualReference tmpref = newrefs[k]; |
| IVirtualComponent referencedcomp = tmpref.getReferencedComponent(); |
| boolean isBinary = referencedcomp.isBinary(); |
| if( isBinary ){ |
| if (shouldShow(referencedcomp)) |
| validCompList.add(referencedcomp); |
| } else { |
| addClasspathComponentDependencies(validCompList, pathToComp, referencedcomp); |
| } |
| } |
| } |
| } |
| } else |
| try { |
| if (project.exists() && project.isAccessible() && project.hasNature("org.eclipse.jdt.core.javanature") ){ //$NON-NLS-1$ |
| if( !project.getName().startsWith(".") ) //$NON-NLS-1$ |
| validCompList.add(project); |
| } |
| } catch (CoreException e) { |
| Logger.getLogger().log(e); |
| } |
| } |
| return validCompList.toArray(); |
| } |
| |
| public void setCurrentLibDir(String libDir) { |
| this.libDir = libDir; |
| } |
| |
| private boolean shouldShow(IVirtualComponent component) { |
| if (!(component instanceof VirtualArchiveComponent)) |
| return true; |
| |
| VirtualArchiveComponent comp = (VirtualArchiveComponent)component; |
| // 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) |
| // there's no point in showing it. Second logic expression: the first segment should be the current EAR, if not the component shouldn't be shown. |
| if(comp.getWorkspaceRelativePath() == null || !comp.getWorkspaceRelativePath().segment(0).equals(earComponent.getName())) |
| return false; |
| |
| IPath p = null; |
| try { |
| p = comp.getProjectRelativePath(); |
| } catch (IllegalArgumentException e) { |
| return false; |
| } |
| |
| // If the path relative to the project is null it means that the file doesn't exist, component shouldn't be shown. |
| if (p == null) |
| return false; |
| |
| // Get the content directory |
| IContainer contentDir = earComponent.getRootFolder().getUnderlyingFolder(); |
| // Obtain the number of segments of the content directory |
| int ContentDirSegmentCount = contentDir.getProjectRelativePath().segmentCount(); |
| // Sum the content directory number of segments plus the number of segments that represent the component (should be only one) |
| int numOfSegmentsToCompare = ContentDirSegmentCount + COMPONENT_NUM_OF_SEGMENTS; |
| |
| // If the EAR has a content folder, check if the project relative path starts with it, if not do not show. |
| if ((ContentDirSegmentCount > 0) && !startWithSameSegments(p, contentDir.getProjectRelativePath())) |
| return false; |
| |
| // At this point we know that the resource is inside the ear, now if it is anywhere |
| // outside the lib dir we must show it (we'll take care of the ones that are in the lib dir later) |
| if (!startWithSameSegments(p, Path.fromOSString(contentDir.getProjectRelativePath().toString() + IPath.SEPARATOR + libDir))) |
| return true; |
| |
| // Show the element if it is in the EAR's root folder. If the EAR has a content folder, the number of segments will be "content folder segments" + "component name's segment" |
| // If the EAR does not have content folder, the number of segments will be 1 (the component name only) |
| if (p.segmentCount() == numOfSegmentsToCompare) |
| return true; |
| |
| if (isEE5) { |
| // Obtain libDir, remove leading and ending unnecessary characters and split into segments |
| String strippedLibDir = stripSeparators(libDir); |
| String[] libDirSegs = strippedLibDir.split(PATH_SEPARATOR); |
| // Verify if lib dir segments' number match |
| if (p.segmentCount() - numOfSegmentsToCompare != libDirSegs.length) |
| return false; |
| // Verify if lib dir segments' values match |
| for (int i = 0; i < libDirSegs.length; i++) |
| if (!libDirSegs[i].equals(p.segment(i + ContentDirSegmentCount))) // If the EAR has no content folder, offset will be 0 |
| return false; |
| return true; |
| } |
| return false; |
| } |
| |
| // This method determines if the provided IPaths start with the same segments |
| private boolean startWithSameSegments(IPath iPath1, IPath iPath2){ |
| |
| // Validate the provided paths |
| if (iPath1 == null || iPath2 == null) |
| return false; |
| |
| // Determine which one of the paths is shorter, if equal then doesn't matter. |
| int minor = iPath1.segmentCount() > iPath2.segmentCount() ? iPath2.segmentCount() : iPath1.segmentCount(); |
| // Compares "minor" amount of segments |
| for (int i = 0; i < minor; i++){ |
| if (!iPath1.segment(i).equals(iPath2.segment(i))) |
| return false; |
| } |
| return true; |
| } |
| |
| private String stripSeparators(String dir) { |
| if (dir.startsWith(PATH_SEPARATOR)) |
| dir = dir.substring(1); |
| if (dir.endsWith(PATH_SEPARATOR)) |
| dir = dir.substring(0, dir.length() - 1); |
| return dir; |
| } |
| |
| public static void addClasspathComponentDependencies(final List componentList, final Map pathToComp, final IVirtualComponent referencedComponent) { |
| if (referencedComponent instanceof J2EEModuleVirtualComponent) { |
| J2EEModuleVirtualComponent j2eeComp = (J2EEModuleVirtualComponent) referencedComponent; |
| IVirtualReference[] cpRefs = j2eeComp.getJavaClasspathReferences(); |
| for (int j=0; j < cpRefs.length; j++) { |
| String unresolvedURI = null; |
| // only ../ mappings supported at this level |
| if (!cpRefs[j].getRuntimePath().equals(IClasspathDependencyConstants.RUNTIME_MAPPING_INTO_CONTAINER_PATH)) { |
| continue; |
| } |
| // if the absolute path for this component already has a mapping, skip (the comp might be contributed by more than |
| // one child module) |
| final IPath path = ClasspathDependencyUtil.getClasspathVirtualReferenceLocation(cpRefs[j]); |
| final IVirtualComponent comp = (IVirtualComponent) pathToComp.get(path); |
| if (comp != null) { |
| // replace with a temp VirtualArchiveComponent whose IProject is set to a new pseudo name that is |
| // the concatenation of all project contributions for that archive |
| if (comp instanceof VirtualArchiveComponent) { |
| final VirtualArchiveComponent oldComp = (VirtualArchiveComponent) comp; |
| componentList.remove(comp); |
| final VirtualArchiveComponent newComponent = ClassPathSelection.updateDisplayVirtualArchiveComponent(oldComp, cpRefs[j]); |
| pathToComp.put(path, newComponent); |
| componentList.add(newComponent); |
| } |
| continue; |
| } else { |
| pathToComp.put(path, cpRefs[j].getReferencedComponent()); |
| } |
| componentList.add(cpRefs[j].getReferencedComponent()); |
| } |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int) |
| */ |
| public Image getColumnImage(Object element, int columnIndex) { |
| return null; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int) |
| */ |
| public String getColumnText(Object element, int columnIndex) { |
| if (element instanceof IVirtualComponent) { |
| IVirtualComponent comp = (IVirtualComponent)element; |
| String name = ""; //$NON-NLS-1$ |
| if( columnIndex == 0 ){ |
| if (ClasspathDependencyUtil.isClasspathComponentDependency(comp)) { |
| return ClasspathDependencyUtil.getClasspathComponentDependencyDisplayString(comp); |
| } |
| IModelProvider provider = ModelProviderManager.getModelProvider(earComponent.getProject()); |
| if (provider instanceof IEARModelProvider) |
| { |
| name = ((IEARModelProvider)provider).getModuleURI(comp); |
| //name will be null if it has not yet been added to the EAR |
| //both component projects will be equal when adding jars, external jars, or variables. |
| if(name == null && earComponent.getProject().equals(comp.getProject())){ |
| name = comp.getName(); |
| if(name != null){ |
| //strip off everything but last segment to avoid confusion so instead of |
| //displaying 'lib/c:\temp\foo.jar' to the user, the user will see 'foo.jar' |
| //which is exactly what will be displayed the second time the user opens the page |
| IPath path = new Path(name); |
| name = path.lastSegment(); |
| } |
| } |
| } |
| |
| if( name == null || name == "" ){ //$NON-NLS-1$ |
| name = comp.getName(); |
| } |
| return name; |
| } else if (columnIndex == 1) { |
| return comp.getProject().getName(); |
| } else if (columnIndex == 2) { |
| return ""; //$NON-NLS-1$ |
| } |
| } else if (element instanceof IProject){ |
| if (columnIndex != 2) { |
| return ((IProject)element).getName(); |
| } else { |
| return ""; //$NON-NLS-1$ |
| } |
| } |
| return null; |
| } |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, |
| * java.lang.Object, java.lang.Object) |
| */ |
| public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| //do nothing |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener) |
| */ |
| public void addListener(ILabelProviderListener listener) { |
| //do nothing |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, |
| * java.lang.String) |
| */ |
| public boolean isLabelProperty(Object element, String property) { |
| return false; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) |
| */ |
| public void removeListener(ILabelProviderListener listener) { |
| //do nothing |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.viewers.IContentProvider#dispose() |
| */ |
| public void dispose() { |
| } |
| } |