Example usage for org.eclipse.jdt.core IClasspathEntry CPE_CONTAINER

List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_CONTAINER

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry CPE_CONTAINER.

Prototype

int CPE_CONTAINER

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_CONTAINER.

Click Source Link

Document

Entry kind constant describing a classpath entry representing a name classpath container.

Usage

From source file:org.eclipse.jst.jsf.core.internal.tld.LoadBundleUtil.java

License:Open Source License

private static IFile getSourceFile(IJavaProject javaProject, String baseName) throws JavaModelException {
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    for (int i = 0; i < classpathEntries.length; i++) {
        if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            final IFile file = getFile(javaProject, baseName, classpathEntries, i);
            if (file.exists()) {
                return file;
            }/* w w  w.  j a v a 2s.c om*/
        } else if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IProject project = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(classpathEntries[i].getPath().toString());
            IJavaProject javaProject3 = JavaCore.create(project);
            final IFile file = getSourceFile(javaProject3, baseName);
            if (file != null && file.exists()) {
                return file;
            }
        } else if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER && classpathEntries[i]
                .getPath().equals(new Path("org.eclipse.jst.j2ee.internal.module.container"))) //$NON-NLS-1$
        {
            IClasspathContainer container = JavaCore.getClasspathContainer(classpathEntries[i].getPath(),
                    javaProject);
            IClasspathEntry[] classpathEntries2 = container.getClasspathEntries();
            for (int j = 0; j < classpathEntries2.length; j++) {
                if (classpathEntries2[j].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    IProject project = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(classpathEntries2[j].getPath().toString());
                    IJavaProject javaProject3 = JavaCore.create(project);
                    final IFile file = getSourceFile(javaProject3, baseName);
                    if (file != null && file.exists()) {
                        return file;
                    }
                }
            }
        }
    }
    return null;
}

From source file:org.eclipse.jst.jsf.core.internal.tld.LoadBundleUtil.java

License:Open Source License

private static IStorage getJarFile(IJavaProject javaProject, String baseName, IClasspathEntry[] roots)
        throws JavaModelException {
    for (int i = 0; i < roots.length; i++) {
        if (roots[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY
                || roots[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            IStorage storage = getResourceFromLibrary(javaProject, baseName, roots[i]);
            if (storage != null) {
                return storage;
            }//from   w  ww. java 2  s  . com
        }
        //         else if ( roots[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) 
        //         {
        //            IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(roots[i].getPath(), javaProject);
        //            final IClasspathEntry[] classpathEntries = 
        //               classpathContainer.getClasspathEntries();
        //            IStorage storage = getJarFile(javaProject, baseName, classpathEntries);
        //            if (storage != null)
        //            {
        //               return storage;
        //            }
        //         }
    }
    return null;
}

From source file:org.eclipse.jst.jsf.core.jsflibraryconfiguration.JSFLibraryConfigurationHelper.java

License:Open Source License

/**
 * @param cpEntry/* w w  w .j ava2  s  . c o m*/
 * @return boolean indicating that the classpath entry is a JSF Libary Classpath Container
 */
public static boolean isJSFLibraryContainer(IClasspathEntry cpEntry) {
    if (cpEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER)
        return false;

    IPath path = cpEntry.getPath();
    return path != null && path.segmentCount() == 2 && JSF_LIBRARY_CP_CONTAINER_ID.equals(path.segment(0));
}

From source file:org.eclipse.jst.jsp.core.taglib.ProjectDescription.java

License:Open Source License

/**
 * @param entry//from www .  j  a  v  a2  s . c  o  m
 */
private void indexClasspath(IClasspathEntry entry) {
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_CONTAINER: {
        IClasspathContainer container = (IClasspathContainer) entry;
        IClasspathEntry[] containedEntries = container.getClasspathEntries();
        for (int i = 0; i < containedEntries.length; i++) {
            indexClasspath(containedEntries[i]);
        }
    }
        break;
    case IClasspathEntry.CPE_LIBRARY: {
        /*
         * Ignore libs in required projects that are not exported
         */
        IPath libPath = entry.getPath();
        if (!fClasspathJars.containsKey(libPath.toString())) {
            final File file = libPath.toFile();
            if (file.exists()) {
                if (file.isDirectory()) {
                    indexDirectory(file, entry.isExported());
                } else {
                    updateClasspathLibrary(libPath.toString(), ITaglibIndexDelta.ADDED, entry.isExported());
                }
            } else {
                /*
                 * Note: .jars on the classpath inside of the project
                 * will have duplicate entries in the JAR references
                 * table that will e returned to
                 * getAvailableTaglibRecords().
                 */
                IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(libPath);
                if (resource != null && resource.isAccessible()) {
                    if (resource.getType() == IResource.FILE) {
                        if (resource.getLocation() != null) {
                            updateClasspathLibrary(resource.getLocation().toString(), ITaglibIndexDelta.ADDED,
                                    entry.isExported());
                        }
                    } else if (resource.getType() == IResource.FOLDER) {
                        try {
                            resource.accept(new Indexer(), 0);
                        } catch (CoreException e) {
                            Logger.logException(e);
                        }
                    }
                }
            }
        }
    }
        break;
    case IClasspathEntry.CPE_PROJECT: {
        /*
         * We're currently ignoring whether the project exports all of
         * its build path
         */
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().lastSegment());
        if (project != null) {
            fClasspathProjects.add(project);
        }
    }
        break;
    case IClasspathEntry.CPE_SOURCE: {
        IPath path = entry.getPath();
        try {
            IResource sourceFolder = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
            // could be a bad .classpath file
            if (sourceFolder != null) {
                sourceFolder.accept(new Indexer(), 0);
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    }
        break;
    case IClasspathEntry.CPE_VARIABLE: {
        IPath libPath = JavaCore.getResolvedVariablePath(entry.getPath());
        if (libPath != null) {
            File file = libPath.toFile();

            // file in filesystem
            if (file.exists() && !file.isDirectory()) {
                updateClasspathLibrary(libPath.toString(), ITaglibRecordEvent.ADDED, entry.isExported());
            } else {
                // workspace file
                IFile jarFile = ResourcesPlugin.getWorkspace().getRoot().getFile(libPath);
                if (jarFile.isAccessible() && jarFile.getType() == IResource.FILE
                        && jarFile.getLocation() != null) {
                    String jarPathString = jarFile.getLocation().toString();
                    updateClasspathLibrary(jarPathString, ITaglibRecordEvent.ADDED, entry.isExported());
                }
            }
        }
    }
        break;
    }
}

From source file:org.eclipse.jst.jsp.core.tests.taglibindex.BundleResourceUtil.java

License:Open Source License

public static void addWebContainer(IProject proj) {
    IJavaProject jProj = JavaCore.create(proj);
    try {/*from   w w w  . jav a2 s.c  o m*/
        IClasspathEntry[] entries = jProj.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && "org.eclipse.jst.jsp.core.tests.webContainerInitializer"
                            .equals(entries[i].getPath().segment(0))) {
                return;
            }
        }
        List newEntries = new ArrayList();
        newEntries.addAll(Arrays.asList(entries));
        newEntries.add(
                JavaCore.newContainerEntry(new Path("org.eclipse.jst.jsp.core.tests.webContainerInitializer")));
        jProj.setRawClasspath((IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]),
                null);
    } catch (JavaModelException e) {

    }
}

From source file:org.eclipse.jst.jsp.core.tests.taglibindex.BundleResourceUtil.java

License:Open Source License

public static IProject createJavaWebProject(String name) throws CoreException {
    // Create new project
    IProject project = BundleResourceUtil.createSimpleProject(name, null, new String[] { JavaCore.NATURE_ID });

    IJavaProject javaProject = JavaCore.create(project);
    List buildPath = new ArrayList(Arrays.asList(javaProject.getRawClasspath()));
    Iterator i = buildPath.iterator();
    while (i.hasNext()) {
        IClasspathEntry entry = (IClasspathEntry) i.next();
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                && "org.eclipse.jdt.launching.JRE_CONTAINER".equals(entry.getPath().segment(0))) {
            i.remove();//from   w w w  .  j a  v a  2 s .c om
        }
    }
    buildPath.add(
            JavaCore.newContainerEntry(new Path("org.eclipse.jst.jsp.core.tests.webContainerInitializer")));
    buildPath.add(JavaCore.newContainerEntry(new Path(
            "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6")));
    javaProject.setRawClasspath((IClasspathEntry[]) buildPath.toArray(new IClasspathEntry[buildPath.size()]),
            new Path("/" + name + "/WebContent/WEB-INF/classes"), new NullProgressMonitor());
    return project;
}

From source file:org.eclipse.jst.server.core.internal.JavaServerPlugin.java

License:Open Source License

/**
 * Handle a runtime change by potentially updating the classpath container.
 * //from w w  w .j  a  v a2 s . c  o m
 * @param runtime a runtime
 */
protected void handleRuntimeChange(final IRuntime runtime, final int act) {
    if (runtime == null)
        throw new IllegalArgumentException();

    if (Trace.FINEST) {
        Trace.trace(Trace.STRING_FINEST, "Possible runtime change: " + runtime);
    }

    if (runtime.getRuntimeType() == null)
        return;

    final RuntimeClasspathProviderWrapper rcpw = findRuntimeClasspathProvider(runtime.getRuntimeType());
    if (rcpw != null && (rcpw.hasRuntimeClasspathChanged(runtime) || act != 1)) {
        final IPath serverContainerPath = new Path(RuntimeClasspathContainer.SERVER_CONTAINER)
                .append(rcpw.getId()).append(runtime.getId());

        class RebuildRuntimeReferencesJob extends Job {
            public RebuildRuntimeReferencesJob() {
                super(NLS.bind(Messages.updateClasspathContainers, runtime.getName()));
            }

            public boolean belongsTo(Object family) {
                return ServerUtil.SERVER_JOB_FAMILY.equals(family);
            }

            public IStatus run(IProgressMonitor monitor) {
                IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
                if (projects != null) {
                    for (IProject project : projects) {
                        if (project.isAccessible()) {
                            try {
                                if (!project.isNatureEnabled(JavaCore.NATURE_ID))
                                    continue;

                                IJavaProject javaProject = JavaCore.create(project);

                                boolean found = false;
                                IClasspathEntry[] ce = javaProject.getRawClasspath();
                                for (IClasspathEntry cp : ce) {
                                    if (cp.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                                        if (serverContainerPath.isPrefixOf(cp.getPath()))
                                            found = true;
                                    }
                                }

                                if (Trace.FINEST) {
                                    Trace.trace(Trace.STRING_FINEST,
                                            "Classpath change on: " + project + " " + found);
                                }

                                if (found) {
                                    IRuntime runtime2 = runtime;
                                    if (act == 2)
                                        runtime2 = null;
                                    RuntimeClasspathContainer container = new RuntimeClasspathContainer(project,
                                            serverContainerPath, rcpw, runtime2, runtime.getId());
                                    JavaCore.setClasspathContainer(serverContainerPath,
                                            new IJavaProject[] { javaProject },
                                            new IClasspathContainer[] { container }, null);
                                }
                            } catch (Exception e) {
                                if (Trace.SEVERE) {
                                    Trace.trace(Trace.STRING_SEVERE, "Could not update classpath container", e);
                                }
                            }
                        }
                    }
                }

                return Status.OK_STATUS;
            }
        }
        RebuildRuntimeReferencesJob job = new RebuildRuntimeReferencesJob();
        job.schedule();
    }
}

From source file:org.eclipse.jst.server.core.internal.RuntimeClasspathContainerInitializer.java

License:Open Source License

public static void updateClasspath(final IRuntime runtime, final IPath containerPath,
        final IClasspathContainer containerSuggestion) {
    class UpdateClasspathJob extends Job {
        public UpdateClasspathJob() {
            super(NLS.bind(Messages.updateClasspathContainers, runtime.getName()));
        }/*from   w  w  w  . ja v  a2 s.co  m*/

        public boolean belongsTo(Object family) {
            return ServerUtil.SERVER_JOB_FAMILY.equals(family);
        }

        public IStatus run(IProgressMonitor monitor) {
            IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
            List<IJavaProject> list = new ArrayList<IJavaProject>();
            if (projects != null) {
                for (IProject project : projects) {
                    if (project.isAccessible()) {
                        try {
                            if (!project.isNatureEnabled(JavaCore.NATURE_ID))
                                continue;

                            IJavaProject javaProject = JavaCore.create(project);

                            boolean found = false;
                            IClasspathEntry[] ce = javaProject.getRawClasspath();
                            for (IClasspathEntry cp : ce) {
                                if (cp.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                                    if (containerPath.isPrefixOf(cp.getPath()))
                                        found = true;
                                }
                            }

                            if (Trace.FINEST) {
                                Trace.trace(Trace.STRING_FINEST,
                                        "Classpath change on: " + project + " " + found);
                            }

                            if (found)
                                list.add(javaProject);
                        } catch (Exception e) {
                            if (Trace.SEVERE) {
                                Trace.trace(Trace.STRING_SEVERE, "Could not update classpath container", e);
                            }
                        }
                    }
                }
            }

            int size = list.size();
            if (size > 0) {
                IJavaProject[] javaProjects = new IJavaProject[size];
                list.toArray(javaProjects);
                IClasspathContainer[] containers = new IClasspathContainer[size];
                for (int i = 0; i < size; i++)
                    containers[i] = containerSuggestion;

                try {
                    JavaCore.setClasspathContainer(containerPath, javaProjects, containers, monitor);
                } catch (JavaModelException jme) {
                    return jme.getStatus();
                }
            }

            return Status.OK_STATUS;
        }
    }
    UpdateClasspathJob job = new UpdateClasspathJob();
    job.schedule();
}

From source file:org.eclipse.jst.servlet.ui.internal.navigator.CompressedJavaLibraries.java

License:Open Source License

public Object[] getChildren(ITreeContentProvider delegateContentProvider) {

    List classpathContainers = new ArrayList();
    Object[] delegateChildren = delegateContentProvider.getChildren(compressedProject.getProject());
    for (int i = 0; i < delegateChildren.length; i++) {
        if (delegateChildren[i] instanceof IPackageFragmentRoot) {
            try {
                IClasspathEntry rawClasspathEntry = ((IPackageFragmentRoot) delegateChildren[i])
                        .getRawClasspathEntry();
                if (rawClasspathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER
                        && rawClasspathEntry.getEntryKind() != IClasspathEntry.CPE_SOURCE)
                    classpathContainers.add(delegateChildren[i]);
            } catch (JavaModelException e) {

            }//from w  w w.  j  a v  a  2 s . c om
        } else if (!(delegateChildren[i] instanceof IJavaElement)
                && !(delegateChildren[i] instanceof IResource)) {
            classpathContainers.add(delegateChildren[i]);
        }
    }
    return classpathContainers.toArray();
}

From source file:org.eclipse.jst.ws.jaxrs.core.internal.jaxrslibraryconfig.JAXRSLibraryRegistryUtil.java

License:Open Source License

/**
 * Binds JAXRS Libraries to classpath containers when the library changes.
 * /*  w  w  w.  ja  v a  2  s  .c o m*/
 * This method will deal with library/cp container renames by removing the
 * old classpath container and then adding.
 * 
 * @param oldId
 * @param newId
 * @param monitor
 * @throws JavaModelException
 */
public static void rebindClasspathContainerEntries(String oldId, String newId, IProgressMonitor monitor)
        throws JavaModelException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IJavaProject[] projects = JavaCore.create(root).getJavaProjects();
    IPath containerPath = new Path(JAXRSLibraryConfigurationHelper.JAXRS_LIBRARY_CP_CONTAINER_ID).append(newId);
    IPath oldContainerPath = new Path(JAXRSLibraryConfigurationHelper.JAXRS_LIBRARY_CP_CONTAINER_ID)
            .append(oldId);

    JAXRSLibrary lib = JAXRSLibraryRegistryUtil.getInstance().getJAXRSLibraryRegistry()
            .getJAXRSLibraryByID(newId);
    List<IJavaProject> affectedProjects = new ArrayList<IJavaProject>();
    boolean removeAndAddBecauseOfRename = (!oldId.equals(newId));
    // find all projects using the old container name...
    for (int i = 0; i < projects.length; i++) {
        IJavaProject project = projects[i];
        IClasspathEntry[] entries = project.getRawClasspath();
        for (int k = 0; k < entries.length; k++) {
            IClasspathEntry curr = entries[k];
            if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (oldContainerPath.equals(curr.getPath())) {
                    affectedProjects.add(project);
                    break;
                }
            }
        }
    }

    if (!affectedProjects.isEmpty()) {
        IJavaProject[] affected = affectedProjects.toArray(new IJavaProject[affectedProjects.size()]);
        IClasspathContainer[] containers = new IClasspathContainer[affected.length];
        removeAndAddBecauseOfRename = (!oldId.equals(newId));
        if (removeAndAddBecauseOfRename) {// not very pretty... remove and
            // add new container
            IClasspathEntry newEntry = JavaCore.newContainerEntry(containerPath);
            for (int i = 0; i < affected.length; i++) {
                IJavaProject project = affected[i];
                IClasspathEntry[] entries = project.getRawClasspath();
                List<IClasspathEntry> keptEntries = new ArrayList<IClasspathEntry>();
                // keep all entries except the old one
                for (int k = 0; k < entries.length; k++) {
                    IClasspathEntry curr = entries[k];
                    if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        if (!oldContainerPath.equals(curr.getPath()))
                            keptEntries.add(curr);
                    } else {
                        keptEntries.add(curr);
                    }
                }
                // add new container entry
                keptEntries.add(newEntry);
                setRawClasspath(project, keptEntries, monitor);
            }

        } else {// rebind

            JAXRSLibraryClasspathContainer container = new JAXRSLibraryClasspathContainer(lib);
            containers[0] = container;

            JavaCore.setClasspathContainer(containerPath, affected, containers, monitor);
        }
    } else {
        if (monitor != null) {
            monitor.done();
        }
    }
}