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

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

Introduction

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

Prototype

int CPE_LIBRARY

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

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a library.

Usage

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

License:Open Source License

/**
 * @param entry//  w  w  w  .j av  a  2 s  .  co 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.server.tomcat.core.internal.TomcatServerUtil.java

License:Open Source License

/**
 * Returns the kind of a <code>PackageFragmentRoot</code> from its <code>String</code> form.
 *///from  www . ja  v  a  2 s  .co  m
protected static int getClasspathKindFromString(String kindStr) {
    //if (kindStr.equalsIgnoreCase("prj"))
    //   return IClasspathEntry.CPE_PROJECT;
    if (kindStr.equalsIgnoreCase("var"))
        return IClasspathEntry.CPE_VARIABLE;
    //if (kindStr.equalsIgnoreCase("src"))
    //   return IClasspathEntry.CPE_SOURCE;
    if (kindStr.equalsIgnoreCase("lib"))
        return IClasspathEntry.CPE_LIBRARY;
    return -1;
}

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

License:Open Source License

/**
 * Returns a <code>String</code> for the kind of a class path entry.
 *///w ww .j  a  v a2 s  . c  o m
protected static String getClasspathKindToString(int kind) {
    switch (kind) {
    //case IClasspathEntry.CPE_PROJECT :
    //   return "prj";
    //case IClasspathEntry.CPE_SOURCE :
    //   return "src";
    case IClasspathEntry.CPE_LIBRARY:
        return "lib";
    case IClasspathEntry.CPE_VARIABLE:
        return "var";
    default:
        return "unknown";
    }
}

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

License:Open Source License

/**
 * Create's a classpath entry of the specified kind.
 *
 * Returns null if unable to create a valid entry.
 *//*from  w  ww . ja v a2 s  .  c  o  m*/
protected static IClasspathEntry createClasspathEntry(IPath path, int kind, IPath sourceAttachmentPath,
        IPath sourceAttachmentRootPath) {
    switch (kind) {
    /*case IClasspathEntry.CPE_PROJECT:
    if (!path.isAbsolute())
       return null;
    else
       return JavaCore.newProjectEntry(path);*/

    case IClasspathEntry.CPE_LIBRARY:
        if (!path.isAbsolute())
            return null;

        return JavaCore.newLibraryEntry(path, sourceAttachmentPath, sourceAttachmentRootPath);

    case IClasspathEntry.CPE_VARIABLE:
        return JavaCore.newVariableEntry(path, sourceAttachmentPath, sourceAttachmentRootPath);

    default:
        return null;
    }
}

From source file:org.eclipse.jst.ws.internal.axis.consumption.ui.util.ClasspathUtils.java

License:Open Source License

private String[] classpathEntry2String(IClasspathEntry entry, IProject project) {
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_LIBRARY: {
        return new String[] { path2String(entry.getPath()) };
    }/*www.ja  v  a2s  . co m*/
    case IClasspathEntry.CPE_PROJECT: {
        return getClasspath(ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().lastSegment()),
                true);
    }
    case IClasspathEntry.CPE_SOURCE: {
        IPath path = entry.getPath();
        if (path.segment(0).equals(project.getName()))
            path = path.removeFirstSegments(1);
        return new String[] { path2String(project.getLocation().addTrailingSeparator().append(path)) };
    }
    case IClasspathEntry.CPE_VARIABLE: {
        return classpathEntry2String(JavaCore.getResolvedClasspathEntry(entry), project);
    }
    default: {
        return new String[] { path2String(entry.getPath()) };
    }
    }
}

From source file:org.eclipse.jst.ws.internal.consumption.ui.widgets.test.wssample.AddModuleDependenciesCommand.java

License:Open Source License

/**
 * Execute WebServerDefaultingTask Set the default server name and id given a
 * deployable./*w  w  w .j a v  a  2s  . c o m*/
 */
public IStatus execute(IProgressMonitor monitor, IAdaptable adaptable) {
    IEnvironment env = getEnvironment();
    //1. Create a Web project for the sample if one does not already exist.
    sampleIProject = ProjectUtilities.getProject(testInfo.getGenerationProject());
    boolean createdSampleProject = false;
    ValidationUtils vu = new ValidationUtils();
    boolean serverNeedsEAR = vu.serverNeedsEAR(testInfo.getClientServerTypeID());
    if (serverNeedsEAR) {
        if (testInfo.getClientEARProject() == null || testInfo.getClientEARProject().length() == 0) {
            sampleEARProject = testInfo.getGenerationProject() + DEFAULT_SAMPLE_EAR_PROJECT_EXT;
        } else {
            sampleEARProject = testInfo.getClientEARProject();
        }
    }
    if (!sampleIProject.exists()) {
        CreateFacetedProjectCommand command = new CreateFacetedProjectCommand();
        command.setProjectName(testInfo.getGenerationProject());
        command.setTemplateId(IJ2EEModuleConstants.JST_WEB_TEMPLATE);
        // RequiredFacetVersions is set to an empty array because we don't need to impose any additional constraints.
        // We just want to create the highest level of Web project that the selected server supports.
        command.setRequiredFacetVersions(new RequiredFacetVersion[0]);
        command.setServerFactoryId(testInfo.getClientServerTypeID());
        command.setServerInstanceId(testInfo.getClientExistingServer().getId());
        command.setEarProjectName(sampleEARProject);
        IStatus status = command.execute(monitor, adaptable);
        if (status.getSeverity() == Status.ERROR) {
            env.getStatusHandler().reportError(status);
            return status;
        }
        createdSampleProject = true;
    }

    //2. If the selected server requires an EAR and no EAR name
    //has been provided, choose an EAR name and create it if it doesn't exist.
    if (serverNeedsEAR) {
        sampleEARIProject = ProjectUtilities.getProject(sampleEARProject);
        if (sampleEARIProject == null || !sampleEARIProject.exists()) {
            CreateFacetedProjectCommand command = new CreateFacetedProjectCommand();
            command.setProjectName(sampleEARProject);
            command.setTemplateId(IJ2EEModuleConstants.JST_EAR_TEMPLATE);
            // RequiredFacetVersions is set to an empty array because we don't need to impose any additional constraints.
            // We just want to create the highest level of Web project that the selected server supports.
            command.setRequiredFacetVersions(new RequiredFacetVersion[0]);
            command.setServerFactoryId(testInfo.getClientServerTypeID());
            command.setServerInstanceId(testInfo.getClientExistingServer().getId());
            IStatus status = command.execute(monitor, adaptable);
            if (status.getSeverity() == Status.ERROR) {
                env.getStatusHandler().reportError(status);
                return status;
            }
        }

    }

    // 3. If the selected server requires an EAR, and the sample project has
    // not already been added to the EAR, add it.
    if (serverNeedsEAR) {
        AssociateModuleWithEARCommand associateCommand = new AssociateModuleWithEARCommand();
        associateCommand.setProject(testInfo.getGenerationProject());
        associateCommand.setEARProject(sampleEARProject);
        associateCommand.setEar(sampleEARProject);
        associateCommand.setEnvironment(env);
        IStatus status = associateCommand.execute(monitor, null);
        if (status.getSeverity() == Status.ERROR) {
            env.getStatusHandler().reportError(status);
        }
    }

    // 4. If server requires an EAR, and the sample EAR has not already been
    // added to the server, add it.
    //   If no EAR is required, and sample project has not been added to the server add it.
    if (serverNeedsEAR && testInfo.getClientExistingServer() != null) {
        //Add sampleEARIProject to the server if needed.
        AddModuleToServerCommand modToServer = new AddModuleToServerCommand();
        modToServer.setModule(sampleEARProject);
        modToServer.setProject(sampleEARProject);
        modToServer.setServerInstanceId(testInfo.getClientExistingServer().getId());
        modToServer.setEnvironment(env);
        IStatus status = modToServer.execute(monitor, null);
        if (status.getSeverity() == Status.ERROR) {
            env.getStatusHandler().reportError(status);
        }
    } else {
        //add sampleIProject directly to the server if needed.
        if (testInfo.getClientExistingServer() != null) {
            AddModuleToServerCommand addToServer = new AddModuleToServerCommand();
            addToServer.setModule(testInfo.getGenerationProject());
            addToServer.setProject(testInfo.getGenerationProject());
            addToServer.setServerInstanceId(testInfo.getClientExistingServer().getId());
            addToServer.setEnvironment(env);
            IStatus status = addToServer.execute(monitor, null);
            if (status.getSeverity() == Status.ERROR) {
                env.getStatusHandler().reportError(status);
            }
        }
    }

    //5. Call StartServerCommand if this command had to create the sample project.
    if (createdSampleProject) {
        StartServerCommand startServer = new StartServerCommand(true);
        startServer.setServerInstanceId(testInfo.getClientExistingServer().getId());
        startServer.setEnvironment(env);
        IStatus status = startServer.execute(monitor, null);
        if (status.getSeverity() == Status.ERROR) {
            env.getStatusHandler().reportError(status);
        }
    }

    //6. Establish all necessary dependencies between client project, sample project, and EAR

    clientIProject = ProjectUtilities.getProject(testInfo.getClientProject());

    if (clientIProject != null && !J2EEUtils.isWebComponent(clientIProject)) {
        if (J2EEUtils.isJavaComponent(clientIProject)) {
            J2EEUtils.addJavaProjectAsUtilityJar(clientIProject, sampleEARIProject, monitor);
            J2EEUtils.addJavaProjectAsUtilityJar(clientIProject, sampleIProject, monitor);
        }

        try {
            String uri = clientIProject.getName() + ".jar";
            J2EEUtils.addJAROrModuleDependency(sampleIProject, uri);

            // Adding the attribute to the referenced project's classpath entries of 'lib' kind
            // so that these libs will be bundled along with the project when exported
            IClasspathEntry[] classPath = JavaCore.create(clientIProject).getRawClasspath();
            for (int i = 0; i < classPath.length; i++) {
                IClasspathEntry classpathEntry = classPath[i];
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    UpdateClasspathAttributeUtil.addDependencyAttribute(monitor, clientIProject.getName(),
                            classpathEntry);
                }
            }

        } catch (CoreException ce) {
            String errorMessage = NLS.bind(ConsumptionUIMessages.MSG_ERROR_MODULE_DEPENDENCY,
                    new String[] { sampleIProject.getName(), clientIProject.getName() });
            IStatus errorStatus = StatusUtils.errorStatus(errorMessage);
            env.getStatusHandler().reportError(errorStatus);
        } catch (IOException ioe) {
            String errorMessage = NLS.bind(ConsumptionUIMessages.MSG_ERROR_MODULE_DEPENDENCY,
                    new String[] { sampleIProject.getName(), clientIProject.getName() });
            IStatus errorStatus = StatusUtils.errorStatus(errorMessage);
            env.getStatusHandler().reportError(errorStatus);
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return Status.OK_STATUS;
}

From source file:org.eclipse.m2e.internal.launch.MavenLauncherConfigurationHandler.java

License:Open Source License

public void addProjectEntry(IMavenProjectFacade facade) {
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IFolder output = root.getFolder(facade.getOutputLocation());
    if (output.isAccessible()) {
        addArchiveEntry(output.getLocation().toFile().getAbsolutePath());
    }/*from   ww  w .j a v  a  2  s . c o m*/
    // add custom classpath entries
    final IJavaProject javaProject = JavaCore.create(facade.getProject());
    try {
        for (IClasspathEntry cpe : javaProject.getRawClasspath()) {
            if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IResource resource = root.findMember(cpe.getPath());
                if (resource != null) {
                    // workspace resource
                    addArchiveEntry(resource.getLocation().toOSString());
                } else {
                    // external 
                    File file = cpe.getPath().toFile();
                    if (file.exists()) {
                        addArchiveEntry(file.getAbsolutePath());
                    }
                }
            }
        }
    } catch (JavaModelException ex) {
        // XXX to do what to do about this
    }
}

From source file:org.eclipse.m2e.jdt.internal.BuildPathManager.java

License:Open Source License

private void configureAttchedSourcesAndJavadoc(IMavenProjectFacade facade, Properties sourceAttachment,
        ClasspathDescriptor classpath, IProgressMonitor monitor) throws CoreException {
    for (IClasspathEntryDescriptor desc : classpath.getEntryDescriptors()) {
        if (IClasspathEntry.CPE_LIBRARY == desc.getEntryKind() && desc.getSourceAttachmentPath() == null) {
            ArtifactKey a = desc.getArtifactKey();
            String key = desc.getPath().toPortableString();

            IPath srcPath = desc.getSourceAttachmentPath();
            IPath srcRoot = desc.getSourceAttachmentRootPath();
            if (srcPath == null && sourceAttachment != null
                    && sourceAttachment.containsKey(key + PROPERTY_SRC_PATH)) {
                srcPath = Path.fromPortableString((String) sourceAttachment.get(key + PROPERTY_SRC_PATH));
                if (sourceAttachment.containsKey(key + PROPERTY_SRC_ROOT)) {
                    srcRoot = Path.fromPortableString((String) sourceAttachment.get(key + PROPERTY_SRC_ROOT));
                }/*from  w  ww . j a v a 2 s  .  c om*/
            }
            if (srcPath == null && a != null) {
                srcPath = getSourcePath(a);
            }

            // configure javadocs if available
            String javaDocUrl = desc.getJavadocUrl();
            if (javaDocUrl == null && sourceAttachment != null
                    && sourceAttachment.containsKey(key + PROPERTY_JAVADOC_URL)) {
                javaDocUrl = (String) sourceAttachment.get(key + PROPERTY_JAVADOC_URL);
            }
            if (javaDocUrl == null && a != null) {
                javaDocUrl = getJavaDocUrl(a);
            }

            desc.setSourceAttachment(srcPath, srcRoot);
            desc.setJavadocUrl(javaDocUrl);

            ArtifactKey aKey = desc.getArtifactKey();
            if (aKey != null) { // maybe we should try to find artifactKey little harder here?
                boolean downloadSources = desc.getSourceAttachmentPath() == null && srcPath == null
                        && mavenConfiguration.isDownloadSources();
                boolean downloadJavaDoc = desc.getJavadocUrl() == null && javaDocUrl == null
                        && mavenConfiguration.isDownloadJavaDoc();

                scheduleDownload(facade.getProject(), facade.getMavenProject(), aKey, downloadSources,
                        downloadJavaDoc);
            }
        }
    }
}

From source file:org.eclipse.m2e.jdt.internal.BuildPathManager.java

License:Open Source License

private void addEntries(Collection<IClasspathEntry> collection, IClasspathEntry[] entries, IPath path) {
    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                && (path == null || path.equals(entry.getPath()))) {
            collection.add(entry);/*www .  j  av  a  2s  . co  m*/
        }
    }
}

From source file:org.eclipse.m2e.jdt.internal.BuildPathManager.java

License:Open Source License

/**
 * Extracts and persists custom source/javadoc attachment info
 */// ww w. ja  va2  s.c  o m
public void persistAttachedSourcesAndJavadoc(IJavaProject project, IClasspathContainer containerSuggestion,
        IProgressMonitor monitor) throws CoreException {
    IFile pom = project.getProject().getFile(IMavenConstants.POM_FILE_NAME);
    IMavenProjectFacade facade = projectManager.create(pom, false, null);
    if (facade == null) {
        return;
    }

    // collect all source/javadoc attachement
    Properties props = new Properties();
    IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (IClasspathEntry.CPE_LIBRARY == entry.getEntryKind()) {
            String path = entry.getPath().toPortableString();
            if (entry.getSourceAttachmentPath() != null) {
                props.put(path + PROPERTY_SRC_PATH, entry.getSourceAttachmentPath().toPortableString());
            }
            if (entry.getSourceAttachmentRootPath() != null) {
                props.put(path + PROPERTY_SRC_ROOT, entry.getSourceAttachmentRootPath().toPortableString());
            }
            String javadocUrl = getJavadocLocation(entry);
            if (javadocUrl != null) {
                props.put(path + PROPERTY_JAVADOC_URL, javadocUrl);
            }
        }
    }

    // eliminate all "standard" source/javadoc attachement we get from local repo
    entries = getClasspath(facade, CLASSPATH_DEFAULT, null, true, monitor);
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (IClasspathEntry.CPE_LIBRARY == entry.getEntryKind()) {
            String path = entry.getPath().toPortableString();
            String value = (String) props.get(path + PROPERTY_SRC_PATH);
            if (value != null && entry.getSourceAttachmentPath() != null
                    && value.equals(entry.getSourceAttachmentPath().toPortableString())) {
                props.remove(path + PROPERTY_SRC_PATH);
            }
            value = (String) props.get(path + PROPERTY_SRC_ROOT);
            if (value != null && entry.getSourceAttachmentRootPath() != null
                    && value.equals(entry.getSourceAttachmentRootPath().toPortableString())) {
                props.remove(path + PROPERTY_SRC_ROOT);
            }
        }
    }

    // persist custom source/javadoc attachement info
    File file = getSourceAttachmentPropertiesFile(project.getProject());
    try {
        OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
        try {
            props.store(os, null);
        } finally {
            os.close();
        }
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, MavenJdtPlugin.PLUGIN_ID, -1,
                "Can't save classpath container changes", e));
    }

    // update classpath container. suboptimal as this will re-calculate classpath
    updateClasspath(project.getProject(), monitor);
}