/** * Copyright (C) 2011 Eric Van Dewoestine * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.eclim.eclipse; import java.io.File; import java.io.FileInputStream; import java.io.FilenameFilter; import java.io.IOException; import java.net.URL; import java.security.CodeSource; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashSet; import org.eclim.logging.Logger; import org.eclim.plugin.core.util.ProjectUtils; import org.eclim.util.IOUtils; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.jdt.core.ClasspathContainerInitializer; import org.eclipse.jdt.core.IClasspathContainer; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.osgi.util.ManifestElement; import org.eclipse.swt.SWT; import org.osgi.framework.Bundle; public class EclimClasspathInitializer extends ClasspathContainerInitializer { private static final Logger logger = Logger.getLogger(EclimClasspathInitializer.class); /** * {@inheritDoc} * @see ClasspathContainerInitializer#initialize(IPath,IJavaProject) */ @Override public void initialize(IPath path, IJavaProject javaProject) throws CoreException { if (javaProject.getProject().getName().equals("eclim")){ EclimClasspathContainer container = new EclimClasspathContainer(path, computeClasspathEntries(javaProject)); JavaCore.setClasspathContainer( path, new IJavaProject[]{javaProject}, new IClasspathContainer[]{container}, new NullProgressMonitor()); } } private IClasspathEntry[] computeClasspathEntries(IJavaProject javaProject) { ArrayList<IClasspathEntry> list = new ArrayList<IClasspathEntry>(); try{ final LinkedHashSet<String> bundleNames = new LinkedHashSet<String>(); final ArrayList<String> jarPaths = new ArrayList<String>(); String projectPath = ProjectUtils.getPath(javaProject.getProject()); new File(projectPath).list(new FilenameFilter(){ public boolean accept(File dir, String name) { if (name.startsWith("org.eclim")){ // first pull bundle dependencies from the manifest File manifest = new File(dir.getPath() + '/' + name + "/META-INF/MANIFEST.MF"); if (manifest.exists()){ FileInputStream fin = null; try{ fin = new FileInputStream(manifest); HashMap<String,String> headers = new HashMap<String,String>(); ManifestElement.parseBundleManifest(fin, headers); String requiredBundles = headers.get("Require-Bundle"); for (String bname : requiredBundles.split(",\\s*")){ if (bname.startsWith("org.eclim")){ continue; } bundleNames.add(bname); } }catch(Exception e){ logger.error("Failed to load manifest: " + manifest, e); }finally{ IOUtils.closeQuietly(fin); } } // then look for plugin jar files File lib = new File(dir.getPath() + '/' + name + "/lib"); if (lib.exists()){ for (String jar : lib.list()){ if (jar.endsWith(".jar")){ jarPaths.add(name + "/lib/" + jar); } } } } return false; } }); // load platform dependent swt jar CodeSource source = SWT.class.getProtectionDomain().getCodeSource(); if (source != null){ URL swt = source.getLocation(); if (swt != null){ logger.debug("adding swt to classpath: {}", swt.getPath()); list.add(JavaCore.newLibraryEntry( new Path(swt.getPath()), null, null, null, null, false)); } } for (String name : bundleNames){ logger.debug("adding bundle to classpath: {}", name); try{ Bundle bundle = Platform.getBundle(name); if (bundle != null){ String path = FileLocator.getBundleFile(bundle).getPath(); list.add(JavaCore.newLibraryEntry( new Path(path), null, null, null, null, false)); } }catch(IOException ioe){ logger.error("Failed to locate bundle: " + name, ioe); } } for (String jarPath : jarPaths){ logger.debug("adding jar to classpath: {}", jarPath); list.add(JavaCore.newLibraryEntry( new Path(projectPath + '/' + jarPath), null, null, null, null, false)); } }catch(Exception e){ logger.error("Failed to load eclim classpath container", e); } return (IClasspathEntry[])list.toArray(new IClasspathEntry[list.size()]); } }