view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/BundleFinder.java @ 119:07602eec599d

Make unit test run without the eclipse runtime
author Omair Majid <omajid@redhat.com>
date Fri, 14 Mar 2014 15:24:53 -0400
parents 4d787bcff36c
children
line wrap: on
line source

package com.redhat.thermostat.tools.eclipse.plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * Finds bundles in all open projects.
 * <p>
 * There is no way to do this reliably using an API. So fall back to finding
 * jars and checking for bundle information in them.
 */
public class BundleFinder {

    // TODO add progress monitor support

    private static final String SYMBOLIC_NAME = "Bundle-SymbolicName";
    private static final String VERSION = "Bundle-Version";

    private File workspaceRoot;
    private IProject[] projects;

    public BundleFinder(IProject[] projects, IProgressMonitor progressMonitor) {
        this(ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile(), projects, progressMonitor);
    }

    public BundleFinder(File root, IProject[] projects, IProgressMonitor progressMonitor) {
        this.projects = projects;
        workspaceRoot = root;
    }

    public Collection<BundleInformation> find() {
        final List<IPath> jarPaths = new ArrayList<>();

        for (IProject project : this.projects) {
            try {
                project.accept(new IResourceVisitor() {
                    @Override
                    public boolean visit(IResource resource) throws CoreException {
                        if (resource.getName().endsWith(".jar")) {
                            jarPaths.add(resource.getFullPath());
                        }
                        return true;
                    }
                });
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }

        List<BundleInformation> result = new ArrayList<>();

        for (IPath jarPath : jarPaths) {
            File jarFile = jarPath.toFile();
            File completeFile = new File(workspaceRoot, jarFile.toString());
            try (JarInputStream in = new JarInputStream(new FileInputStream(completeFile))) {
                Manifest manifest = in.getManifest();
                Attributes attrs = manifest.getMainAttributes();
                String symbolicName = attrs.getValue(SYMBOLIC_NAME);
                String version = attrs.getValue(VERSION);
                if (symbolicName != null && version != null) {
                    result.add(new BundleInformation(symbolicName, version));
                }
            } catch (FileNotFoundException e) {
                throw new AssertionError("A file that was found is now gone: " + completeFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return result;
    }
}