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.j2ee.internal.project.WTPJETEmitter.java

License:Open Source License

@Override
public void initialize(IProgressMonitor progressMonitor) throws JETException {

    progressMonitor.beginTask("", 10); //$NON-NLS-1$
    progressMonitor.subTask(CodeGenPlugin.getPlugin().getString("_UI_GeneratingJETEmitterFor_message", //$NON-NLS-1$
            new Object[] { templateURI }));

    try {//  www  .j a v  a2s  .  co  m
        // This ensures that the JRE variables are initialized.
        try {
            JavaRuntime.getDefaultVMInstall();
        } catch (Throwable throwable) {
            // This is kind of nasty to come here.
            URL jreURL = Platform.find(Platform.getBundle("org.eclipse.emf.codegen"), new Path("plugin.xml")); //$NON-NLS-1$ //$NON-NLS-2$
            IPath jrePath = new Path(Platform.asLocalURL(jreURL).getFile());
            jrePath = jrePath.removeLastSegments(1).append(new Path("../../jre/lib/rt.jar")); //$NON-NLS-1$
            if (!jrePath.equals(JavaCore.getClasspathVariable(JavaRuntime.JRELIB_VARIABLE))) {
                JavaCore.setClasspathVariable(JavaRuntime.JRELIB_VARIABLE, jrePath, null);
            }
        }

        /*
         * final JETCompiler jetCompiler = templateURIPath == null ? new
         * MyJETCompiler(templateURI, encoding) :
         */

        getJetCompiler();

        progressMonitor.subTask(CodeGenPlugin.getPlugin().getString("_UI_JETParsing_message", //$NON-NLS-1$
                new Object[] { jetCompiler.getResolvedTemplateURI() }));
        jetCompiler.parse();
        progressMonitor.worked(1);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        jetCompiler.generate(outputStream);
        final InputStream contents = new ByteArrayInputStream(outputStream.toByteArray());

        final IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        if (!javaModel.isOpen()) {
            javaModel.open(new SubProgressMonitor(progressMonitor, 1));
        } else {
            progressMonitor.worked(1);
        }

        final IProject project = workspace.getRoot().getProject(getProjectName());
        progressMonitor.subTask(CodeGenPlugin.getPlugin().getString("_UI_JETPreparingProject_message", //$NON-NLS-1$
                new Object[] { project.getName() }));

        IJavaProject javaProject;
        if (!project.exists()) {
            javaProject = createJavaProject(progressMonitor, workspace, project);

            initializeJavaProject(progressMonitor, project, javaProject);

            javaProject.close();
        } else {
            project.open(new SubProgressMonitor(progressMonitor, 5));
            javaProject = JavaCore.create(project);
            List rawClassPath = Arrays.asList(javaProject.getRawClasspath());
            for (int i = 0; i < rawClassPath.size(); i++) {
                IClasspathEntry entry = (IClasspathEntry) rawClassPath.get(i);
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
                    classpathEntries.add(entry);
            }
        }

        IPackageFragmentRoot sourcePackageFragmentRoot = openJavaProjectIfNecessary(progressMonitor, project,
                javaProject);

        String packageName = jetCompiler.getSkeleton().getPackageName();
        StringTokenizer stringTokenizer = new StringTokenizer(packageName, "."); //$NON-NLS-1$
        IProgressMonitor subProgressMonitor = new SubProgressMonitor(progressMonitor, 1);
        subProgressMonitor.beginTask("", stringTokenizer.countTokens() + 4); //$NON-NLS-1$
        subProgressMonitor.subTask(CodeGenPlugin.getPlugin().getString("_UI_CreateTargetFile_message")); //$NON-NLS-1$
        IContainer sourceContainer = (IContainer) sourcePackageFragmentRoot.getCorrespondingResource();
        while (stringTokenizer.hasMoreElements()) {
            String folderName = stringTokenizer.nextToken();
            sourceContainer = sourceContainer.getFolder(new Path(folderName));
            if (!sourceContainer.exists()) {
                ((IFolder) sourceContainer).create(false, true, new SubProgressMonitor(subProgressMonitor, 1));
            }
        }
        IFile targetFile = sourceContainer
                .getFile(new Path(jetCompiler.getSkeleton().getClassName() + ".java")); //$NON-NLS-1$
        if (!targetFile.exists()) {
            subProgressMonitor.subTask(CodeGenPlugin.getPlugin().getString("_UI_JETCreating_message", //$NON-NLS-1$
                    new Object[] { targetFile.getFullPath() }));
            targetFile.create(contents, true, new SubProgressMonitor(subProgressMonitor, 1));
        } else {
            subProgressMonitor.subTask(CodeGenPlugin.getPlugin().getString("_UI_JETUpdating_message", //$NON-NLS-1$
                    new Object[] { targetFile.getFullPath() }));
            targetFile.setContents(contents, true, true, new SubProgressMonitor(subProgressMonitor, 1));
        }

        boolean errors = performBuild(project, subProgressMonitor, targetFile);

        if (!errors) {
            loadClass(workspace, project, javaProject, packageName, subProgressMonitor);
        }

        subProgressMonitor.done();
    } catch (CoreException exception) {
        throw new JETException(exception);
    } catch (Exception exception) {
        throw new JETException(exception);
    } finally {
        progressMonitor.done();
    }

}

From source file:org.eclipse.jst.j2ee.internal.servertarget.ServerTargetHelper.java

License:Open Source License

public static List getExistingNonServerTargetClasspath(IProject project) {
    IJavaProject javaProject = null;/*w  w  w . j  a  v  a 2 s  .co m*/
    List list = new ArrayList();
    try {
        javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
    } catch (Exception e) {
    }
    if (javaProject != null) {
        try {
            IClasspathEntry[] cp = javaProject.getRawClasspath();
            int size = cp.length;
            for (int i = 0; i < size; i++) {
                int entryKind = cp[i].getEntryKind();
                if (entryKind != IClasspathEntry.CPE_SOURCE && entryKind != IClasspathEntry.CPE_LIBRARY
                        && entryKind != IClasspathEntry.CPE_PROJECT
                        && (entryKind == IClasspathEntry.CPE_VARIABLE && isWASVariable(cp[i]))
                        && (entryKind != IClasspathEntry.CPE_CONTAINER
                                || !cp[i].getPath().segment(0).equals(SERVER_CONTAINER))) {
                    list.add(cp[i]);
                }
            }
        } catch (Exception e) {
        }
        return list;
    }
    return list;
}

From source file:org.eclipse.jst.j2ee.internal.ui.AddJavaBuildPathEntriesWizardFragment.java

License:Open Source License

public static String getClasspathEntryText(final IJavaProject jproj, final IClasspathEntry cpe) {
    final int type = cpe.getEntryKind();

    if (type == IClasspathEntry.CPE_CONTAINER) {
        try {/*from  w  w  w .  j av a2s  . c om*/
            final IClasspathContainer container = JavaCore.getClasspathContainer(cpe.getPath(), jproj);
            return container.getDescription();
        } catch (Exception e) {
            J2EEUIPlugin.logError(e);
        }
    } else if (type == IClasspathEntry.CPE_LIBRARY) {
        final IPath path = cpe.getPath();

        final StringBuilder buf = new StringBuilder();
        buf.append(path.lastSegment());
        buf.append(" - "); //$NON-NLS-1$
        buf.append(path.removeLastSegments(1).toOSString());

        return buf.toString();
    }

    return cpe.getPath().toPortableString();
}

From source file:org.eclipse.jst.j2ee.internal.ui.AddJavaBuildPathEntriesWizardFragment.java

License:Open Source License

public static Image getClasspathEntryImage(final IJavaProject jproj, final IClasspathEntry cpe) {
    final int type = cpe.getEntryKind();
    final String imgId;

    if (type == IClasspathEntry.CPE_CONTAINER) {
        imgId = ISharedImages.IMG_OBJS_LIBRARY;
    } else if (type == IClasspathEntry.CPE_LIBRARY) {
        imgId = ISharedImages.IMG_OBJS_JAR;
    } else if (type == IClasspathEntry.CPE_VARIABLE) {
        imgId = ISharedImages.IMG_OBJS_CLASSPATH_VAR_ENTRY;
    } else {// w w  w.ja  va2s . co m
        imgId = null;
    }

    return (imgId == null ? null : JavaUI.getSharedImages().getImage(imgId));
}

From source file:org.eclipse.jst.j2ee.internal.web.operations.WebPropertiesUtil.java

License:Open Source License

/**
 * Update the classpath entries and Server Root Name for this web project only.
 * //from  w  w w  .  j ava  2s  .  c o  m
 * @param project
 * @param webContentName
 * @return
 */
public static void updateWebContentNamePropertiesOnly(IProject project, String webContentName,
        IProgressMonitor progressMonitor) throws CoreException {
    IPath newPath = new Path(webContentName);
    if (getModuleServerRoot(project).equals(newPath))
        return;

    if (!getModuleServerRoot(project).equals(webContentName)) {

        // if (webModuleArtifact.isJ2EE) {
        // Update the library references
        IJavaProject javaProject = JemProjectUtilities.getJavaProject(project);

        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        IClasspathEntry[] newClasspath = new IClasspathEntry[classpath.length];

        for (int i = 0; i < classpath.length; i++) {
            if (classpath[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IClasspathEntry library = classpath[i];
                IPath libpath = library.getPath();
                IPath modServerRootPath = getModuleServerRoot(project).getFullPath();
                if (modServerRootPath.isPrefixOf(libpath)) {
                    IPath prunedPath = libpath.removeFirstSegments(modServerRootPath.segmentCount());
                    IPath relWebContentPath = new Path(webContentName + "/" + prunedPath.toString()); //$NON-NLS-1$
                    IResource absWebContentPath = project.getFile(relWebContentPath);

                    IPath srcAttachmentPath = library.getSourceAttachmentPath();
                    if (null != srcAttachmentPath) {
                        prunedPath = srcAttachmentPath.removeFirstSegments(modServerRootPath.segmentCount());
                    }
                    IResource absWebContentSrcAttachmentPath = project.getFile(relWebContentPath);

                    newClasspath[i] = JavaCore.newLibraryEntry(absWebContentPath.getFullPath(),
                            absWebContentSrcAttachmentPath.getFullPath(), library.getSourceAttachmentRootPath(),
                            library.isExported());

                } else {
                    newClasspath[i] = classpath[i];
                }

            } else {
                newClasspath[i] = classpath[i];
            }
            // }

            // Set the java output folder
            IFolder outputFolder = project.getFolder(getModuleServerRoot(project).getFullPath());
            javaProject.setRawClasspath(newClasspath, outputFolder.getFullPath(),
                    new SubProgressMonitor(progressMonitor, 1));
        }
        // update websettings
        // TODO add to WebArtifactEdit
        // webNature.setModuleServerRootName(webContentName);
    }
}

From source file:org.eclipse.jst.jsf.common.facet.libraryprovider.jsf.JsfLibraryUtil.java

License:Open Source License

/**
 * @param cpe//from  ww  w .j  ava2 s.c  o  m
 * @return True if the classpath entry is a library.
 */
public static boolean isLibrary(final IClasspathEntry cpe) {
    return cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY;
}

From source file:org.eclipse.jst.jsf.common.facet.libraryprovider.UserLibraryVersionValidator.java

License:Open Source License

private boolean isLibrary(final IClasspathEntry cpe) {
    return cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY;
}

From source file:org.eclipse.jst.jsf.core.internal.project.facet.JSFFacetInstallDataModelProvider.java

License:Open Source License

private IStatus validateClasspath() {
    Set jars = new HashSet();
    if (doesProjectExist()) {
        //validate actual classpath by loading jars from cp
        try {//from w  ww. j  a v  a 2 s .c  o m
            IClasspathEntry[] entries = getJavaProject().getResolvedClasspath(true);
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    jars.add(entry.getPath().makeAbsolute().toString());
                }
            }
        } catch (JavaModelException ignore) {
            /*
             * Bug 330819 - Project Properties - Facets - Change Configuration - Receive npe error
             * 
             * Don't log error, as this is an expected exception if the Java project has not yet
             * been created - continue processing.
             */
            //JSFCorePlugin.log(e, "Error searching class path"); //$NON-NLS-1$
        }
    }
    //else as we do not have a javaProject yet, all we can do is validate that there is no duplicate jars (absolute path)

    IStatus status = null;

    JSFLibraryInternalReference[] compLibs = (JSFLibraryInternalReference[]) getProperty(
            IJSFFacetInstallDataModelProperties.COMPONENT_LIBRARIES);
    if (compLibs != null) {
        for (int i = 0; i < compLibs.length; i++) {
            JSFLibrary lib = compLibs[i].getLibrary();
            status = checkForDupeArchiveFiles(jars, lib);
            if (!OK_STATUS.equals(status)) {
                return status;
            }
        }
    }
    return OK_STATUS;
}

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 w w.  ja va2  s .  c o  m*/
        }
        //         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.jsfappconfig.JSFAppConfigUtils.java

License:Open Source License

/**
 * Gets list of JAR file names, where each file name represents a JAR on
 * the classpath that contains a /META-INF/faces-config.xml entry. Will
 * return an empty list if no such JAR files are located.
 * /*from   w w  w.jav  a2s .c o m*/
 * @param project IProject instance for which to scan the classpath.
 * @return List of JAR file names, where each file name represents a JAR
 * on the classpath that contains a ...META-INF/faces-config.xml entry;
 * list may be empty.
 * @throws CoreException Thrown when underlying calls into JavaCore fail.
 * @throws IOException Thrown when attempt to open JAR to determine if it
 * contains a /META-INF/faces-config.xml entry fails.
 */
public static List getConfigFileJARsFromClasspath(IProject project) throws CoreException, IOException {
    ArrayList JARsList = new ArrayList();
    if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null) {
            IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
            if (classpathEntries != null && classpathEntries.length > 0) {
                IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
                for (int i = 0; i < classpathEntries.length; i++) {
                    IClasspathEntry classpathEntry = classpathEntries[i];
                    if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPath libraryPath = classpathEntry.getPath();
                        if (libraryPath.getFileExtension() != null
                                && libraryPath.getFileExtension().length() > 0) {
                            if (libraryPath.getDevice() == null
                                    && workspaceRoot.getProject(libraryPath.segment(0)).exists()) {
                                libraryPath = workspaceRoot.getFile(libraryPath).getLocation();
                            }
                            String libraryPathString = libraryPath.toString();
                            JarFile jarFile = null;
                            try {
                                //check existence first [222249]
                                File file = new File(libraryPathString);
                                if (file.exists()) {
                                    jarFile = new JarFile(file, false);
                                    if (jarFile != null) {
                                        JarEntry jarEntry = jarFile.getJarEntry(FACES_CONFIG_IN_JAR_PATH);
                                        if (jarEntry != null) {
                                            JARsList.add(libraryPathString);
                                        }
                                    }
                                }
                            } catch (FileNotFoundException fnfex) {
                                //should not get here, but eat error since this could only occur in under strange circumstances [222249]
                            } catch (IOException ioe) {
                                JSFCorePlugin.log(IStatus.ERROR,
                                        NLS.bind(Messages.JSFAppConfigUtils_ErrorOpeningJarFile,
                                                libraryPathString),
                                        ioe);
                            } finally {
                                if (jarFile != null) {
                                    jarFile.close();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return JARsList;
}