Search in sources :

Example 1 with IVirtualResource

use of org.eclipse.wst.common.componentcore.resources.IVirtualResource in project liferay-ide by liferay.

the class ComponentUtil method getSources.

private static IPackageFragmentRoot[] getSources(IProject project) {
    IJavaProject jProject = JavaCore.create(project);
    if (jProject == null) {
        return new IPackageFragmentRoot[0];
    }
    List<IPackageFragmentRoot> list = new ArrayList<IPackageFragmentRoot>();
    IVirtualComponent vc = ComponentCore.createComponent(project);
    IPackageFragmentRoot[] roots;
    try {
        roots = jProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].getKind() != IPackageFragmentRoot.K_SOURCE) {
                continue;
            }
            IResource resource = roots[i].getResource();
            if (null != resource) {
                IVirtualResource[] vResources = ComponentCore.createResources(resource);
                boolean found = false;
                for (int j = 0; !found && j < vResources.length; j++) {
                    if (vResources[j].getComponent().equals(vc)) {
                        if (!list.contains(roots[i])) {
                            list.add(roots[i]);
                        }
                        found = true;
                    }
                }
            }
        }
        if (list.size() == 0) {
            for (IPackageFragmentRoot root : roots) {
                if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    if (!list.contains(root)) {
                        list.add(root);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        LiferayServerCore.logError(e);
    }
    return list.toArray(new IPackageFragmentRoot[list.size()]);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ArrayList(java.util.ArrayList) IVirtualComponent(org.eclipse.wst.common.componentcore.resources.IVirtualComponent) IVirtualResource(org.eclipse.wst.common.componentcore.resources.IVirtualResource) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 2 with IVirtualResource

use of org.eclipse.wst.common.componentcore.resources.IVirtualResource in project liferay-ide by liferay.

the class ProjectUtil method fixExtProjectSrcFolderLinks.

public static void fixExtProjectSrcFolderLinks(IProject extProject) throws JavaModelException {
    if (extProject == null) {
        return;
    }
    IJavaProject javaProject = JavaCore.create(extProject);
    if (javaProject == null) {
        return;
    }
    IVirtualComponent c = ComponentCore.createComponent(extProject, false);
    if (c == null) {
        return;
    }
    IVirtualFolder jsrc = c.getRootFolder().getFolder("/WEB-INF/classes");
    if (jsrc == null) {
        return;
    }
    IClasspathEntry[] cp = javaProject.getRawClasspath();
    for (int i = 0; i < cp.length; i++) {
        IClasspathEntry cpe = cp[i];
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = cpe.getPath().removeFirstSegments(1);
            if (path.segmentCount() > 0) {
                try {
                    IFolder srcFolder = CoreUtil.getWorkspaceRoot().getFolder(cpe.getPath());
                    IVirtualResource[] virtualResource = ComponentCore.createResources(srcFolder);
                    if (virtualResource.length == 0) {
                        jsrc.createLink(cpe.getPath().removeFirstSegments(1), 0, null);
                    }
                } catch (Exception e) {
                    ProjectCore.logError(e);
                }
            }
        }
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IVirtualFolder(org.eclipse.wst.common.componentcore.resources.IVirtualFolder) IVirtualComponent(org.eclipse.wst.common.componentcore.resources.IVirtualComponent) IVirtualResource(org.eclipse.wst.common.componentcore.resources.IVirtualResource) CoreException(org.eclipse.core.runtime.CoreException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with IVirtualResource

use of org.eclipse.wst.common.componentcore.resources.IVirtualResource in project webtools.servertools by eclipse.

the class Tomcat85PublishModuleVisitor method endVisitWebComponent.

/**
 * {@inheritDoc}
 */
@Override
public void endVisitWebComponent(IVirtualComponent component) throws CoreException {
    // track context changes, don't rewrite if not needed
    boolean dirty = false;
    IModule module = ServerUtil.getModule(component.getProject());
    // we need this for the user-specified context path
    Context context = findContext(module);
    if (context == null) {
        String name = module != null ? module.getName() : component.getName();
        Trace.trace(Trace.SEVERE, "Could not find context for module " + name);
        throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishContextNotFound, name), null));
    }
    dirty = includeProjectContextXml(component, context);
    dirty = updateDocBaseAndPath(component, context);
    // Add WEB-INF/classes elements as PreResources
    for (Iterator iterator = virtualClassClasspathElements.iterator(); iterator.hasNext(); ) {
        Object virtualClassClasspathElement = iterator.next();
        PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
        preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
        preResources.setBase(virtualClassClasspathElement.toString());
        preResources.setWebAppMount("/WEB-INF/classes");
        preResources.setInternalPath("/");
        preResources.setClassLoaderOnly("false");
    }
    virtualClassClasspathElements.clear();
    // Add Jars as JarResources if a jar, or as PostResources if a utility project
    for (Iterator iterator = virtualJarClasspathElements.iterator(); iterator.hasNext(); ) {
        Object virtualJarClassClasspathElement = iterator.next();
        String jarPath = virtualJarClassClasspathElement.toString();
        if (jarPath.endsWith(".jar")) {
            JarResources jarResources = (JarResources) context.getResources().createElement("JarResources");
            jarResources.setClassName("org.apache.catalina.webresources.JarResourceSet");
            jarResources.setBase(jarPath);
            jarResources.setWebAppMount("/WEB-INF/classes");
            jarResources.setInternalPath("/");
            jarResources.setClassLoaderOnly("true");
        } else {
            PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
            postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
            postResources.setBase(jarPath);
            postResources.setWebAppMount("/WEB-INF/classes");
            postResources.setInternalPath("/");
            postResources.setClassLoaderOnly("false");
            // Map META-INF tld files to WEB-INF
            File metaInfDir = new File(jarPath + "/META-INF");
            if (metaInfDir.isDirectory() && metaInfDir.exists()) {
                // Map META-INF directory directly to /META-INF
                postResources = (PostResources) context.getResources().createElement("PostResources");
                postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                postResources.setBase(metaInfDir.getPath());
                postResources.setWebAppMount("/META-INF");
                postResources.setInternalPath("/");
                postResources.setClassLoaderOnly("false");
                File[] tldFiles = metaInfDir.listFiles(new FileFilter() {

                    public boolean accept(File file) {
                        if (file.isFile() && file.getName().endsWith(".tld")) {
                            return true;
                        }
                        return false;
                    }
                });
                for (int i = 0; i < tldFiles.length; i++) {
                    postResources = (PostResources) context.getResources().createElement("PostResources");
                    postResources.setClassName("org.apache.catalina.webresources.FileResourceSet");
                    postResources.setBase(tldFiles[0].getPath());
                    postResources.setWebAppMount("/WEB-INF/" + tldFiles[0].getName());
                    postResources.setInternalPath("/");
                    postResources.setClassLoaderOnly("false");
                }
            }
        }
    }
    virtualJarClasspathElements.clear();
    Set<String> rtPathsProcessed = new HashSet<String>();
    Set<String> locationsIncluded = new HashSet<String>();
    String docBase = context.getDocBase();
    locationsIncluded.add(docBase);
    Map<String, String> retryLocations = new HashMap<String, String>();
    IVirtualResource[] virtualResources = component.getRootFolder().getResources("");
    // Loop over the module's resources
    for (int i = 0; i < virtualResources.length; i++) {
        String rtPath = virtualResources[i].getRuntimePath().toString();
        // If this runtime path has not yet been processed
        if (!rtPathsProcessed.contains(rtPath)) {
            // If not a Java related resource
            if (!"/WEB-INF/classes".equals(rtPath)) {
                // Get all resources for this runtime path
                IResource[] underlyingResources = virtualResources[i].getUnderlyingResources();
                // to a mapping in the .components file
                if ("/".equals(rtPath)) {
                    for (int j = 0; j < underlyingResources.length; j++) {
                        IPath resLoc = underlyingResources[j].getLocation();
                        String location = resLoc.toOSString();
                        if (!location.equals(docBase)) {
                            PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                            preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                            preResources.setBase(location);
                            preResources.setWebAppMount("/");
                            preResources.setInternalPath("/");
                            preResources.setClassLoaderOnly("false");
                            // Add to the set of locations included
                            locationsIncluded.add(location);
                        }
                    }
                } else // Else this runtime path is something other than "/"
                {
                    int idx = rtPath.lastIndexOf('/');
                    // If a "normal" runtime path
                    if (idx >= 0) {
                        // Get the name of the last segment in the runtime path
                        String lastSegment = rtPath.substring(idx + 1);
                        // Check the underlying resources to determine which correspond to mappings
                        for (int j = 0; j < underlyingResources.length; j++) {
                            IPath resLoc = underlyingResources[j].getLocation();
                            String location = resLoc.toOSString();
                            // from the .contents file.
                            if (!lastSegment.equals(resLoc.lastSegment())) {
                                PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                                preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                                preResources.setBase(location);
                                preResources.setWebAppMount(rtPath);
                                preResources.setInternalPath("/");
                                preResources.setClassLoaderOnly("false");
                                // Add to the set of locations included
                                locationsIncluded.add(location);
                            } else // Else last segment of runtime path did match the last segment
                            // of the location.  We likely have a subfolder of a mapping
                            // that matches a portion of the runtime path.
                            {
                                // Since we can't be sure, save so it can be check again later
                                retryLocations.put(location, rtPath);
                            }
                        }
                    }
                }
            }
            // Add the runtime path to those already processed
            rtPathsProcessed.add(rtPath);
        }
    }
    // If there are locations to retry, add any not yet included in extra paths setting
    if (!retryLocations.isEmpty()) {
        // Remove retry locations already included in the extra paths
        for (Iterator iterator = retryLocations.keySet().iterator(); iterator.hasNext(); ) {
            String location = (String) iterator.next();
            for (Iterator iterator2 = locationsIncluded.iterator(); iterator2.hasNext(); ) {
                String includedLocation = (String) iterator2.next();
                if (location.equals(includedLocation) || location.startsWith(includedLocation + File.separator)) {
                    iterator.remove();
                    break;
                }
            }
        }
        // If any entries are left, include them in the extra paths
        if (!retryLocations.isEmpty()) {
            for (Iterator iterator = retryLocations.entrySet().iterator(); iterator.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String location = (String) entry.getKey();
                String rtPath = (String) entry.getValue();
                PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                preResources.setBase(location);
                preResources.setWebAppMount(rtPath);
                preResources.setInternalPath("/");
                preResources.setClassLoaderOnly("false");
            }
        }
    }
    if (!virtualDependentResources.isEmpty()) {
        for (Map.Entry<String, List<String>> entry : virtualDependentResources.entrySet()) {
            String rtPath = entry.getKey();
            List<String> locations = entry.getValue();
            for (String location : locations) {
                PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
                postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                postResources.setBase(location);
                postResources.setWebAppMount(rtPath.length() > 0 ? rtPath : "/");
                postResources.setInternalPath("/");
                postResources.setClassLoaderOnly("false");
            }
        }
    }
    virtualDependentResources.clear();
    if (dirty) {
    // TODO If writing to separate context XML files, save "dirty" status for later use
    }
}
Also used : IModule(org.eclipse.wst.server.core.IModule) HashMap(java.util.HashMap) Iterator(java.util.Iterator) List(java.util.List) FileFilter(java.io.FileFilter) JarResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.JarResources) HashSet(java.util.HashSet) Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IVirtualResource(org.eclipse.wst.common.componentcore.resources.IVirtualResource) PreResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.PreResources) CoreException(org.eclipse.core.runtime.CoreException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PostResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.PostResources) IResource(org.eclipse.core.resources.IResource)

Example 4 with IVirtualResource

use of org.eclipse.wst.common.componentcore.resources.IVirtualResource in project webtools.servertools by eclipse.

the class Tomcat80PublishModuleVisitor method endVisitWebComponent.

/**
 * {@inheritDoc}
 */
@Override
public void endVisitWebComponent(IVirtualComponent component) throws CoreException {
    // track context changes, don't rewrite if not needed
    boolean dirty = false;
    IModule module = ServerUtil.getModule(component.getProject());
    // we need this for the user-specified context path
    Context context = findContext(module);
    if (context == null) {
        String name = module != null ? module.getName() : component.getName();
        Trace.trace(Trace.SEVERE, "Could not find context for module " + name);
        throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishContextNotFound, name), null));
    }
    dirty = includeProjectContextXml(component, context);
    dirty = updateDocBaseAndPath(component, context);
    // Add WEB-INF/classes elements as PreResources
    for (Iterator iterator = virtualClassClasspathElements.iterator(); iterator.hasNext(); ) {
        Object virtualClassClasspathElement = iterator.next();
        PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
        preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
        preResources.setBase(virtualClassClasspathElement.toString());
        preResources.setWebAppMount("/WEB-INF/classes");
        preResources.setInternalPath("/");
        preResources.setClassLoaderOnly("false");
    }
    virtualClassClasspathElements.clear();
    // Add Jars as JarResources if a jar, or as PostResources if a utility project
    for (Iterator iterator = virtualJarClasspathElements.iterator(); iterator.hasNext(); ) {
        Object virtualJarClassClasspathElement = iterator.next();
        String jarPath = virtualJarClassClasspathElement.toString();
        if (jarPath.endsWith(".jar")) {
            JarResources jarResources = (JarResources) context.getResources().createElement("JarResources");
            jarResources.setClassName("org.apache.catalina.webresources.JarResourceSet");
            jarResources.setBase(jarPath);
            jarResources.setWebAppMount("/WEB-INF/classes");
            jarResources.setInternalPath("/");
            jarResources.setClassLoaderOnly("true");
        } else {
            PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
            postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
            postResources.setBase(jarPath);
            postResources.setWebAppMount("/WEB-INF/classes");
            postResources.setInternalPath("/");
            postResources.setClassLoaderOnly("false");
            // Map META-INF tld files to WEB-INF
            File metaInfDir = new File(jarPath + "/META-INF");
            if (metaInfDir.isDirectory() && metaInfDir.exists()) {
                // Map META-INF directory directly to /META-INF
                postResources = (PostResources) context.getResources().createElement("PostResources");
                postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                postResources.setBase(metaInfDir.getPath());
                postResources.setWebAppMount("/META-INF");
                postResources.setInternalPath("/");
                postResources.setClassLoaderOnly("false");
                File[] tldFiles = metaInfDir.listFiles(new FileFilter() {

                    public boolean accept(File file) {
                        if (file.isFile() && file.getName().endsWith(".tld")) {
                            return true;
                        }
                        return false;
                    }
                });
                for (int i = 0; i < tldFiles.length; i++) {
                    postResources = (PostResources) context.getResources().createElement("PostResources");
                    postResources.setClassName("org.apache.catalina.webresources.FileResourceSet");
                    postResources.setBase(tldFiles[0].getPath());
                    postResources.setWebAppMount("/WEB-INF/" + tldFiles[0].getName());
                    postResources.setInternalPath("/");
                    postResources.setClassLoaderOnly("false");
                }
            }
        }
    }
    virtualJarClasspathElements.clear();
    Set<String> rtPathsProcessed = new HashSet<String>();
    Set<String> locationsIncluded = new HashSet<String>();
    String docBase = context.getDocBase();
    locationsIncluded.add(docBase);
    Map<String, String> retryLocations = new HashMap<String, String>();
    IVirtualResource[] virtualResources = component.getRootFolder().getResources("");
    // Loop over the module's resources
    for (int i = 0; i < virtualResources.length; i++) {
        String rtPath = virtualResources[i].getRuntimePath().toString();
        // If this runtime path has not yet been processed
        if (!rtPathsProcessed.contains(rtPath)) {
            // If not a Java related resource
            if (!"/WEB-INF/classes".equals(rtPath)) {
                // Get all resources for this runtime path
                IResource[] underlyingResources = virtualResources[i].getUnderlyingResources();
                // to a mapping in the .components file
                if ("/".equals(rtPath)) {
                    for (int j = 0; j < underlyingResources.length; j++) {
                        IPath resLoc = underlyingResources[j].getLocation();
                        String location = resLoc.toOSString();
                        if (!location.equals(docBase)) {
                            PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                            preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                            preResources.setBase(location);
                            preResources.setWebAppMount("/");
                            preResources.setInternalPath("/");
                            preResources.setClassLoaderOnly("false");
                            // Add to the set of locations included
                            locationsIncluded.add(location);
                        }
                    }
                } else // Else this runtime path is something other than "/"
                {
                    int idx = rtPath.lastIndexOf('/');
                    // If a "normal" runtime path
                    if (idx >= 0) {
                        // Get the name of the last segment in the runtime path
                        String lastSegment = rtPath.substring(idx + 1);
                        // Check the underlying resources to determine which correspond to mappings
                        for (int j = 0; j < underlyingResources.length; j++) {
                            IPath resLoc = underlyingResources[j].getLocation();
                            String location = resLoc.toOSString();
                            // from the .contents file.
                            if (!lastSegment.equals(resLoc.lastSegment())) {
                                PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                                preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                                preResources.setBase(location);
                                preResources.setWebAppMount(rtPath);
                                preResources.setInternalPath("/");
                                preResources.setClassLoaderOnly("false");
                                // Add to the set of locations included
                                locationsIncluded.add(location);
                            } else // Else last segment of runtime path did match the last segment
                            // of the location.  We likely have a subfolder of a mapping
                            // that matches a portion of the runtime path.
                            {
                                // Since we can't be sure, save so it can be check again later
                                retryLocations.put(location, rtPath);
                            }
                        }
                    }
                }
            }
            // Add the runtime path to those already processed
            rtPathsProcessed.add(rtPath);
        }
    }
    // If there are locations to retry, add any not yet included in extra paths setting
    if (!retryLocations.isEmpty()) {
        // Remove retry locations already included in the extra paths
        for (Iterator iterator = retryLocations.keySet().iterator(); iterator.hasNext(); ) {
            String location = (String) iterator.next();
            for (Iterator iterator2 = locationsIncluded.iterator(); iterator2.hasNext(); ) {
                String includedLocation = (String) iterator2.next();
                if (location.equals(includedLocation) || location.startsWith(includedLocation + File.separator)) {
                    iterator.remove();
                    break;
                }
            }
        }
        // If any entries are left, include them in the extra paths
        if (!retryLocations.isEmpty()) {
            for (Iterator iterator = retryLocations.entrySet().iterator(); iterator.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String location = (String) entry.getKey();
                String rtPath = (String) entry.getValue();
                PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                preResources.setBase(location);
                preResources.setWebAppMount(rtPath);
                preResources.setInternalPath("/");
                preResources.setClassLoaderOnly("false");
            }
        }
    }
    if (!virtualDependentResources.isEmpty()) {
        for (Map.Entry<String, List<String>> entry : virtualDependentResources.entrySet()) {
            String rtPath = entry.getKey();
            List<String> locations = entry.getValue();
            for (String location : locations) {
                PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
                postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                postResources.setBase(location);
                postResources.setWebAppMount(rtPath.length() > 0 ? rtPath : "/");
                postResources.setInternalPath("/");
                postResources.setClassLoaderOnly("false");
            }
        }
    }
    virtualDependentResources.clear();
    if (dirty) {
    // TODO If writing to separate context XML files, save "dirty" status for later use
    }
}
Also used : IModule(org.eclipse.wst.server.core.IModule) HashMap(java.util.HashMap) Iterator(java.util.Iterator) List(java.util.List) FileFilter(java.io.FileFilter) JarResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.JarResources) HashSet(java.util.HashSet) Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IVirtualResource(org.eclipse.wst.common.componentcore.resources.IVirtualResource) PreResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.PreResources) CoreException(org.eclipse.core.runtime.CoreException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PostResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.PostResources) IResource(org.eclipse.core.resources.IResource)

Example 5 with IVirtualResource

use of org.eclipse.wst.common.componentcore.resources.IVirtualResource in project webtools.servertools by eclipse.

the class Tomcat90PublishModuleVisitor method endVisitWebComponent.

/**
 * {@inheritDoc}
 */
@Override
public void endVisitWebComponent(IVirtualComponent component) throws CoreException {
    // track context changes, don't rewrite if not needed
    boolean dirty = false;
    IModule module = ServerUtil.getModule(component.getProject());
    // we need this for the user-specified context path
    Context context = findContext(module);
    if (context == null) {
        String name = module != null ? module.getName() : component.getName();
        Trace.trace(Trace.SEVERE, "Could not find context for module " + name);
        throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishContextNotFound, name), null));
    }
    dirty = includeProjectContextXml(component, context);
    dirty = updateDocBaseAndPath(component, context);
    // Add WEB-INF/classes elements as PreResources
    for (Iterator iterator = virtualClassClasspathElements.iterator(); iterator.hasNext(); ) {
        Object virtualClassClasspathElement = iterator.next();
        PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
        preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
        preResources.setBase(virtualClassClasspathElement.toString());
        preResources.setWebAppMount("/WEB-INF/classes");
        preResources.setInternalPath("/");
        preResources.setClassLoaderOnly("false");
    }
    virtualClassClasspathElements.clear();
    // Add Jars as JarResources if a jar, or as PostResources if a utility project
    for (Iterator iterator = virtualJarClasspathElements.iterator(); iterator.hasNext(); ) {
        Object virtualJarClassClasspathElement = iterator.next();
        String jarPath = virtualJarClassClasspathElement.toString();
        if (jarPath.endsWith(".jar")) {
            JarResources jarResources = (JarResources) context.getResources().createElement("JarResources");
            jarResources.setClassName("org.apache.catalina.webresources.JarResourceSet");
            jarResources.setBase(jarPath);
            jarResources.setWebAppMount("/WEB-INF/classes");
            jarResources.setInternalPath("/");
            jarResources.setClassLoaderOnly("true");
        } else {
            PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
            postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
            postResources.setBase(jarPath);
            postResources.setWebAppMount("/WEB-INF/classes");
            postResources.setInternalPath("/");
            postResources.setClassLoaderOnly("false");
            // Map META-INF tld files to WEB-INF
            File metaInfDir = new File(jarPath + "/META-INF");
            if (metaInfDir.isDirectory() && metaInfDir.exists()) {
                // Map META-INF directory directly to /META-INF
                postResources = (PostResources) context.getResources().createElement("PostResources");
                postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                postResources.setBase(metaInfDir.getPath());
                postResources.setWebAppMount("/META-INF");
                postResources.setInternalPath("/");
                postResources.setClassLoaderOnly("false");
                File[] tldFiles = metaInfDir.listFiles(new FileFilter() {

                    public boolean accept(File file) {
                        if (file.isFile() && file.getName().endsWith(".tld")) {
                            return true;
                        }
                        return false;
                    }
                });
                for (int i = 0; i < tldFiles.length; i++) {
                    postResources = (PostResources) context.getResources().createElement("PostResources");
                    postResources.setClassName("org.apache.catalina.webresources.FileResourceSet");
                    postResources.setBase(tldFiles[0].getPath());
                    postResources.setWebAppMount("/WEB-INF/" + tldFiles[0].getName());
                    postResources.setInternalPath("/");
                    postResources.setClassLoaderOnly("false");
                }
            }
        }
    }
    virtualJarClasspathElements.clear();
    Set<String> rtPathsProcessed = new HashSet<String>();
    Set<String> locationsIncluded = new HashSet<String>();
    String docBase = context.getDocBase();
    locationsIncluded.add(docBase);
    Map<String, String> retryLocations = new HashMap<String, String>();
    IVirtualResource[] virtualResources = component.getRootFolder().getResources("");
    // Loop over the module's resources
    for (int i = 0; i < virtualResources.length; i++) {
        String rtPath = virtualResources[i].getRuntimePath().toString();
        // If this runtime path has not yet been processed
        if (!rtPathsProcessed.contains(rtPath)) {
            // If not a Java related resource
            if (!"/WEB-INF/classes".equals(rtPath)) {
                // Get all resources for this runtime path
                IResource[] underlyingResources = virtualResources[i].getUnderlyingResources();
                // to a mapping in the .components file
                if ("/".equals(rtPath)) {
                    for (int j = 0; j < underlyingResources.length; j++) {
                        IPath resLoc = underlyingResources[j].getLocation();
                        String location = resLoc.toOSString();
                        if (!location.equals(docBase)) {
                            PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                            preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                            preResources.setBase(location);
                            preResources.setWebAppMount("/");
                            preResources.setInternalPath("/");
                            preResources.setClassLoaderOnly("false");
                            // Add to the set of locations included
                            locationsIncluded.add(location);
                        }
                    }
                } else // Else this runtime path is something other than "/"
                {
                    int idx = rtPath.lastIndexOf('/');
                    // If a "normal" runtime path
                    if (idx >= 0) {
                        // Get the name of the last segment in the runtime path
                        String lastSegment = rtPath.substring(idx + 1);
                        // Check the underlying resources to determine which correspond to mappings
                        for (int j = 0; j < underlyingResources.length; j++) {
                            IPath resLoc = underlyingResources[j].getLocation();
                            String location = resLoc.toOSString();
                            // from the .contents file.
                            if (!lastSegment.equals(resLoc.lastSegment())) {
                                PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                                preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                                preResources.setBase(location);
                                preResources.setWebAppMount(rtPath);
                                preResources.setInternalPath("/");
                                preResources.setClassLoaderOnly("false");
                                // Add to the set of locations included
                                locationsIncluded.add(location);
                            } else // Else last segment of runtime path did match the last segment
                            // of the location.  We likely have a subfolder of a mapping
                            // that matches a portion of the runtime path.
                            {
                                // Since we can't be sure, save so it can be check again later
                                retryLocations.put(location, rtPath);
                            }
                        }
                    }
                }
            }
            // Add the runtime path to those already processed
            rtPathsProcessed.add(rtPath);
        }
    }
    // If there are locations to retry, add any not yet included in extra paths setting
    if (!retryLocations.isEmpty()) {
        // Remove retry locations already included in the extra paths
        for (Iterator iterator = retryLocations.keySet().iterator(); iterator.hasNext(); ) {
            String location = (String) iterator.next();
            for (Iterator iterator2 = locationsIncluded.iterator(); iterator2.hasNext(); ) {
                String includedLocation = (String) iterator2.next();
                if (location.equals(includedLocation) || location.startsWith(includedLocation + File.separator)) {
                    iterator.remove();
                    break;
                }
            }
        }
        // If any entries are left, include them in the extra paths
        if (!retryLocations.isEmpty()) {
            for (Iterator iterator = retryLocations.entrySet().iterator(); iterator.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String location = (String) entry.getKey();
                String rtPath = (String) entry.getValue();
                PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
                preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                preResources.setBase(location);
                preResources.setWebAppMount(rtPath);
                preResources.setInternalPath("/");
                preResources.setClassLoaderOnly("false");
            }
        }
    }
    if (!virtualDependentResources.isEmpty()) {
        for (Map.Entry<String, List<String>> entry : virtualDependentResources.entrySet()) {
            String rtPath = entry.getKey();
            List<String> locations = entry.getValue();
            for (String location : locations) {
                PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
                postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
                postResources.setBase(location);
                postResources.setWebAppMount(rtPath.length() > 0 ? rtPath : "/");
                postResources.setInternalPath("/");
                postResources.setClassLoaderOnly("false");
            }
        }
    }
    virtualDependentResources.clear();
    if (dirty) {
    // TODO If writing to separate context XML files, save "dirty" status for later use
    }
}
Also used : IModule(org.eclipse.wst.server.core.IModule) HashMap(java.util.HashMap) Iterator(java.util.Iterator) List(java.util.List) FileFilter(java.io.FileFilter) JarResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.JarResources) HashSet(java.util.HashSet) Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IVirtualResource(org.eclipse.wst.common.componentcore.resources.IVirtualResource) PreResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.PreResources) CoreException(org.eclipse.core.runtime.CoreException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PostResources(org.eclipse.jst.server.tomcat.core.internal.xml.server40.PostResources) IResource(org.eclipse.core.resources.IResource)

Aggregations

IVirtualResource (org.eclipse.wst.common.componentcore.resources.IVirtualResource)17 IPath (org.eclipse.core.runtime.IPath)10 IVirtualFolder (org.eclipse.wst.common.componentcore.resources.IVirtualFolder)8 IResource (org.eclipse.core.resources.IResource)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 IProject (org.eclipse.core.resources.IProject)6 IFile (org.eclipse.core.resources.IFile)5 CoreException (org.eclipse.core.runtime.CoreException)5 IVirtualComponent (org.eclipse.wst.common.componentcore.resources.IVirtualComponent)5 IVirtualReference (org.eclipse.wst.common.componentcore.resources.IVirtualReference)5 File (java.io.File)4 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 List (java.util.List)4 IContainer (org.eclipse.core.resources.IContainer)4 IStatus (org.eclipse.core.runtime.IStatus)4 Path (org.eclipse.core.runtime.Path)4 Status (org.eclipse.core.runtime.Status)4 Context (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context)4