Example usage for org.eclipse.jdt.core JavaCore createJarPackageFragmentRootFrom

List of usage examples for org.eclipse.jdt.core JavaCore createJarPackageFragmentRootFrom

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore createJarPackageFragmentRootFrom.

Prototype

public static IPackageFragmentRoot createJarPackageFragmentRootFrom(IFile file) 

Source Link

Document

Creates and returns a handle for the given JAR file.

Usage

From source file:com.google.gwt.eclipse.core.speedtracer.ViewSourceServlet.java

License:Open Source License

private IPackageFragmentRoot getPackageFragmentRoot(String jarPathString, String preferredProjectName)
        throws IOException {
    try {//from w  ww.  j ava  2 s .  com
        IFile jarIFile = getFile(jarPathString);

        // The JAR is in the workspace
        return JavaCore.createJarPackageFragmentRootFrom(jarIFile);
    } catch (IOException e) {
        // JAR must not be in the workspace (or is not a file), continue..
    }

    File jarFile = new File(jarPathString);

    // Iterate projects to find the external JAR
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    List<IProject> projects = new ArrayList<IProject>(Arrays.asList(root.getProjects()));
    IProject preferredProject = preferredProjectName != null ? root.getProject(preferredProjectName) : null;
    if (preferredProject != null && preferredProject.exists()) {
        // Search the preferred project first
        projects.remove(preferredProject);
        projects.add(0, preferredProject);
    }

    for (IProject project : projects) {
        IJavaProject javaProject = JavaCore.create(project);
        if (!javaProject.exists()) {
            continue;
        }

        try {
            IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
            for (IClasspathEntry classpathEntry : classpathEntries) {
                IPath path = classpathEntry.getPath();
                if (jarFile.equals(path.toFile())) {
                    return javaProject.getPackageFragmentRoot(path.toOSString());
                }
            }

        } catch (JavaModelException e) {
            GWTPluginLog.logWarning(e, "Could not check " + project.getName() + " for JAR file");
            continue;
        }
    }

    return null;
}

From source file:org.eclipse.emf.editor.provider.ClasspathUriResolver.java

License:Open Source License

private URI findResourceInWorkspace(IJavaProject javaProject, URI classpathUri)
        throws JavaModelException, CoreException {
    Path fullPath = new Path(classpathUri.path());
    String projectRelativePath = fullPath.toString();
    if (javaProject.exists()) {
        IPackageFragmentRoot[] allPackageFragmentRoots = javaProject.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot packageFragmentRoot : allPackageFragmentRoots) {
            IResource correspondingResource = packageFragmentRoot.getCorrespondingResource();
            if (correspondingResource != null) {
                if (correspondingResource instanceof IFile) {
                    // jar file
                    JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) JavaCore
                            .createJarPackageFragmentRootFrom((IFile) correspondingResource);
                    if (jarPackageFragmentRoot != null) {
                        ZipFile zipFile = jarPackageFragmentRoot.getJar();
                        if (zipFile != null) {
                            ZipEntry zipEntry = zipFile.getEntry(projectRelativePath.substring(1));
                            if (zipEntry != null) {
                                return URI.createURI("jar:" + "platform:/resource"
                                        + correspondingResource.getFullPath() + "!" + projectRelativePath,
                                        true);
                            }//w w w  .j ava2  s.co m
                        }
                    }
                } else {
                    // plain file
                    IFolder rootFolder = (IFolder) correspondingResource;
                    IResource modelFile = rootFolder.findMember(projectRelativePath);
                    if (modelFile != null && modelFile.exists() && modelFile instanceof IFile) {
                        URI platformResourceUri = URI
                                .createPlatformResourceURI(modelFile.getFullPath().toString(), true);
                        return platformResourceUri;
                    }
                }
            }
        }
    }
    return classpathUri;
}

From source file:org.eclipse.jpt.jpa.core.internal.JarResourceModelProvider.java

License:Open Source License

/**
 * NB: Despite the check in GenericJpaProject.addJpaFile_(IFile),
 * we can get here and//w  w w  .  j  a  v  a 2s  . com
 * the PFR will be null if the JAR is underneath a directory on the
 * classpath but the JAR itself is not on the classpath.
 * Returning null should be OK.
 */
public JptResourceModel buildResourceModel(JpaProject jpaProject, IFile file) {
    IPackageFragmentRoot pfr = JavaCore.createJarPackageFragmentRootFrom(file);
    return (pfr == null) ? null
            : new BinaryPackageFragmentRoot(pfr, jpaProject.getJpaPlatform().getAnnotationProvider());
}

From source file:org.wso2.developerstudio.eclipse.artifact.registry.handler.project.export.RegistryHandlerArtifactHandler.java

License:Open Source License

public List<IResource> exportArtifact(IProject project) throws Exception {
    List<IResource> exportResources = new ArrayList<IResource>();
    List<String> exportedPackageList = new ArrayList<String>();
    String activatorClass = new String();
    String existingActivatorClass = new String();

    ArchiveManipulator archiveManipulator = new ArchiveManipulator();

    // getting maven details
    MavenProject mavenProject = MavenUtils.getMavenProject(project.getFile("pom.xml").getLocation().toFile());

    // Get the output location
    IJavaProject javaProject = JavaCore.create(project);

    clearTarget(project);/*from www.jav a 2 s. c om*/
    IPath outPutPath = buildJavaProject(project);

    // get resource location
    IPath resources = getResourcePath(project);

    // getting export packages
    for (IPackageFragment pkg : javaProject.getPackageFragments()) {
        if (pkg.getKind() == IPackageFragmentRoot.K_SOURCE) {
            if (pkg.hasChildren()) {
                exportedPackageList.add(pkg.getElementName());
                for (ICompilationUnit compilationUnit : pkg.getCompilationUnits()) {
                    IType[] types = compilationUnit.getTypes();
                    for (IType type : types) {
                        if (type.getSuperInterfaceNames().length > 0
                                && Arrays.asList(type.getSuperInterfaceNames()).contains("BundleActivator")) {
                            activatorClass = type.getFullyQualifiedName();
                        }
                    }
                }
            }
        }
    }

    List<Plugin> plugins = mavenProject.getBuild().getPlugins();
    for (Plugin plugin : plugins) {
        if ("maven-bundle-plugin".equalsIgnoreCase(plugin.getArtifactId())) {
            Xpp3Dom configurationNode = (Xpp3Dom) plugin.getConfiguration();
            Xpp3Dom[] instructions = configurationNode.getChildren("instructions");
            if (instructions.length == 1) {
                Xpp3Dom[] exportPackage = instructions[0].getChildren("Export-Package");
                if (exportPackage.length == 1) {
                    exportedPackageList.clear(); //clear default configuration (All packages by default)
                    String packages = exportPackage[0].getValue();
                    if (packages != null) {
                        exportedPackageList.addAll(Arrays.asList(packages.split(",")));
                    }
                } else {
                    log.warn("Invalid configuration for <Export-Package> entry"
                            + " using default configuration for <Export-Package>");
                }
            } else {
                log.warn("Invalid instructions configuration for plugin : maven-bundle-plugin"
                        + " using default configuration for <Export-Package>");
            }
            break; //not considering multiple versions of the maven-bundle-plugin
        }
    }

    File tempProject = createTempProject();

    File bundleResources = createTempDir(tempProject, "bundle_resources");

    if (exportedPackageList.size() > 0) {
        FileUtils.copyDirectoryContents(outPutPath.toFile(), bundleResources); // copy
                                                                               // binaries
    }

    if (resources.toFile().exists()) {
        FileUtils.copyDirectoryContents(resources.toFile(), bundleResources); // copy
        // resources
    }

    @SuppressWarnings("unchecked")
    List<Dependency> dependencies = mavenProject.getDependencies();
    Iterator<Dependency> iterator = dependencies.iterator();
    while (iterator.hasNext()) {
        Dependency dependency = iterator.next();
        if ("system".equals(dependency.getScope())) {
            String systemPath = dependency.getSystemPath();
            if (systemPath != null && !systemPath.trim().isEmpty()) {
                systemPath = systemPath.replaceAll("^".concat(Pattern.quote("${basedir}/")), "");
                IFile jarFile = project.getFile(systemPath);
                if (jarFile.exists()) {
                    archiveManipulator.extract(jarFile.getLocation().toFile(), bundleResources);
                    // getting export packages
                    IPackageFragmentRoot rootPkg = JavaCore.createJarPackageFragmentRootFrom(jarFile);

                    for (IJavaElement item : rootPkg.getChildren()) {
                        if (item instanceof IPackageFragment) {
                            IPackageFragment pkg = (IPackageFragment) item;
                            if (pkg.hasChildren()) {
                                exportedPackageList.add(pkg.getElementName());
                                for (IClassFile clazz : pkg.getClassFiles()) {
                                    IType type = clazz.getType();
                                    if (type.getSuperInterfaceNames().length > 0 && Arrays
                                            .asList(type.getSuperInterfaceNames()).contains(ACTIVATOR_FQN)) {
                                        existingActivatorClass = type.getFullyQualifiedName();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }

    if (activatorClass.trim().isEmpty()) {
        activatorClass = existingActivatorClass;
    }

    /* writing manifest */
    BundleManifest manifest = new BundleManifest();
    manifest.setBundleName(project.getName());
    manifest.setBundleSymbolicName(project.getName());
    if (null != mavenProject.getModel().getDescription()
            && !"".equals(mavenProject.getModel().getDescription())) {
        manifest.setBundleDescription(mavenProject.getModel().getDescription());
    } else {
        manifest.setBundleDescription(project.getName());
    }
    if (null != mavenProject.getModel().getVersion() && !"".equals(mavenProject.getDescription())) {
        manifest.setBundleVersion(mavenProject.getModel().getVersion());
    } else {
        manifest.setBundleVersion("1.0.0");
    }
    manifest.setBundleActivatorName(activatorClass);
    manifest.setExportPackagesList(exportedPackageList);
    File metaInfDir = new File(bundleResources, "META-INF");
    if (!metaInfDir.exists())
        metaInfDir.mkdir();
    File manifestFile = new File(metaInfDir, "MANIFEST.MF");
    FileUtils.createFile(manifestFile, manifest.toString());

    File tmpArchive = new File(tempProject, project.getName().concat(".jar"));
    archiveManipulator.archiveDir(tmpArchive.toString(), bundleResources.toString());

    IFile bundleArchive = getTargetArchive(project, "jar");
    FileUtils.copy(tmpArchive, bundleArchive.getLocation().toFile());
    exportResources.add((IResource) bundleArchive);

    TempFileUtils.cleanUp();

    return exportResources;

}

From source file:org.wso2.developerstudio.eclipse.artifact.registry.handler.ui.wizard.RegistryHandlerCreationWizard.java

License:Open Source License

public void updatePom(IProject project) throws Exception {
    File mavenProjectPomLocation = project.getFile("pom.xml").getLocation().toFile();
    MavenProject mavenProject = MavenUtils.getMavenProject(mavenProjectPomLocation);
    StringBuffer sb = new StringBuffer();
    String activatorClass = new String();

    Properties properties = mavenProject.getModel().getProperties();
    properties.put("CApp.type", "lib/registry/handlers");
    mavenProject.getModel().setProperties(properties);

    Plugin plugin = MavenUtils.createPluginEntry(mavenProject, "org.apache.felix", "maven-bundle-plugin",
            "2.3.4", true);

    // getting export packages
    IPackageFragmentRoot rootPkg = JavaCore
            .createJarPackageFragmentRootFrom(project.getFile(regModel.getExternalJar().getName()));

    for (IJavaElement item : rootPkg.getChildren()) {
        if (item instanceof IPackageFragment) {
            IPackageFragment pkg = (IPackageFragment) item;
            if (pkg.hasChildren()) {
                sb.append(pkg.getElementName()).append(",");
                for (IClassFile clazz : pkg.getClassFiles()) {
                    IType type = clazz.getType();
                    if (type.getSuperInterfaceNames().length > 0
                            && Arrays.asList(type.getSuperInterfaceNames()).contains(ACTIVATOR_FQN)) {
                        activatorClass = type.getFullyQualifiedName();
                    }/*from  w  w w .jav  a 2s. c om*/
                }
            }
        }
    }
    String exportedPackageList = sb.toString().replaceAll(",$", "");

    Xpp3Dom configurationNode = MavenUtils.createMainConfigurationNode(plugin);
    Xpp3Dom instructionNode = MavenUtils.createXpp3Node("instructions");
    Xpp3Dom bundleSymbolicNameNode = MavenUtils.createXpp3Node(instructionNode, "Bundle-SymbolicName");
    Xpp3Dom bundleNameNode = MavenUtils.createXpp3Node(instructionNode, "Bundle-Name");
    Xpp3Dom bundleActivatorNode = MavenUtils.createXpp3Node(instructionNode, "Bundle-Activator");
    Xpp3Dom exportPackageNode = MavenUtils.createXpp3Node(instructionNode, "Export-Package");
    Xpp3Dom dynamicImportNode = MavenUtils.createXpp3Node(instructionNode, "DynamicImport-Package");
    bundleSymbolicNameNode.setValue(project.getName());
    bundleNameNode.setValue(project.getName());
    bundleActivatorNode.setValue(activatorClass);
    exportPackageNode.setValue(exportedPackageList);
    dynamicImportNode.setValue("*");

    configurationNode.addChild(instructionNode);

    Repository repo = new Repository();
    repo.setUrl("http://maven.wso2.org/nexus/content/groups/wso2-public/");
    repo.setId("wso2-maven2-repository-1");

    mavenProject.getModel().addRepository(repo);
    mavenProject.getModel().addPluginRepository(repo);

    List<Dependency> dependencyList = new ArrayList<Dependency>();

    Dependency dependency = new Dependency();
    dependency.setArtifactId(regModel.getExternalJar().getName());
    dependency.setGroupId("dummy.groupid");
    dependency.setVersion("1.0.0");
    dependency.setScope("system");
    dependency.setSystemPath("${basedir}/" + regModel.getExternalJar().getName());
    dependencyList.add(dependency);
    dependencyList.add(dependency);

    MavenUtils.addMavenDependency(mavenProject, dependencyList);
    MavenUtils.saveMavenProject(mavenProject, mavenProjectPomLocation);

}