blob: c9afda4849fed1a4b8660aeb532944bdbcdcbb2b [file] [log] [blame]
/***********************************************************************
* Copyright (c) 2008 by SAP AG, Walldorf.
* 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:
* SAP AG - initial API and implementation
***********************************************************************/
package org.eclipse.jst.jee.ui.internal.navigator.ear;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
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.wst.common.componentcore.internal.util.IModuleConstants;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualReference;
/**
* @author gormov
*
*/
public abstract class AbstractEarNode {
public final static String EAR_DEFAULT_LIB = "lib"; //$NON-NLS-1$
public static int MODULES_TYPE = 0;
public static int LIBS_TYPE = 1;
protected int type;
protected static List implicitUtilityReferenceTypes =
Arrays.asList(new String[] {
IModuleConstants.JST_APPCLIENT_MODULE,
IModuleConstants.JST_WEB_MODULE,
IModuleConstants.JST_EJB_MODULE,
IModuleConstants.JST_CONNECTOR_MODULE});
private IProject earProject;
public AbstractEarNode(IProject earProject) {
this.earProject = earProject;
}
public IProject getEarProject() {
return earProject;
}
public abstract List getModules();
public int getType() {
return type;
}
public abstract String getText();
protected List getComponentReferencesAsList(List componentTypes, IVirtualComponent virtualComponent, IPath runtimePath) {
List components = new ArrayList();
IVirtualComponent earComponent = virtualComponent;
if (earComponent != null ) {
IVirtualReference[] refComponents = earComponent.getReferences();
for (int i = 0; i < refComponents.length; i++) {
IVirtualComponent module = refComponents[i].getReferencedComponent();
if (module == null) continue;
// if component types passed in is null then return all components
if (componentTypes == null || componentTypes.size() == 0) {
components.add(refComponents[i]);
} else {
IPath runtimePath2 = getRealRuntimePath(refComponents[i]);
if (runtimePath2.equals(runtimePath) && componentTypes.contains(JavaEEProjectUtilities.getJ2EEComponentType(module))) {
components.add(refComponents[i]);
}
}
}
}
return components;
}
/**
* This method will return complete list of the binaries contained in the lib dir.
*
* @param virtualComponent
* @param runtimePath
* @return
*/
protected List getBinariesInLibDir(IVirtualComponent virtualComponent, IPath runtimePath, List componentTypes) {
List components = new ArrayList();
IVirtualComponent earComponent = virtualComponent;
if (earComponent != null ) {
IVirtualReference[] refComponents = earComponent.getReferences();
for (int i = 0; i < refComponents.length; i++) {
IVirtualComponent module = refComponents[i].getReferencedComponent();
if (module == null) continue;
// if component types passed in is null then return all components
if (module.isBinary() && getRealRuntimePath(refComponents[i]).equals(runtimePath)) {
if (componentTypes != null && !componentTypes.contains(JavaEEProjectUtilities.getJ2EEComponentType(module)) ) {
if (!runtimePath.equals(new Path("/"))){//$NON-NLS-1$
components.add(refComponents[i]);
}
} else {
components.add(refComponents[i]);
}
}
}
}
return components;
}
/**
* This method will return all the binaries contained in the lib dir.
* It will not consider modules in lib dir as libraries for that use
* getBinariesInLibDir(IVirtualComponent virtualComponent, IPath runtimePath, List componentTypes)
*
* @param virtualComponent
* @param runtimePath
* @return
* @deprecated since it is not returning the complete list of libraries
* use: getBinariesInLibDir(IVirtualComponent virtualComponent, IPath runtimePath, List componentTypes)
*/
protected List getBinariesInLibDir(IVirtualComponent virtualComponent, IPath runtimePath) {
return getBinariesInLibDir(virtualComponent, runtimePath, null);
}
protected String recomputeLibFolder(IProject project){
IModelProvider modelProvider = ModelProviderManager.getModelProvider(project);
Object modelObject = modelProvider.getModelObject();
if (Application.class.isInstance(modelObject)){
String libraryDirectory = ((Application)modelObject).getLibraryDirectory();
return libraryDirectory != null && libraryDirectory.length()>0 ? libraryDirectory : EAR_DEFAULT_LIB;
}
return EAR_DEFAULT_LIB;
}
protected IPath getRealRuntimePath(IVirtualReference vr){
IPath runtimePath2 = vr.getRuntimePath();
if(vr.getArchiveName() != null){
IPath namePath = new Path (vr.getArchiveName());
if (namePath.segmentCount()>1 && runtimePath2.segmentCount()==0){
runtimePath2 = new Path("/"+namePath.removeLastSegments(1).toPortableString()); //$NON-NLS-1$
}
}
return runtimePath2;
}
}