001: package org.drools.eclipse.util;
002:
003: import java.io.File;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.net.URLClassLoader;
007: import java.util.ArrayList;
008: import java.util.HashSet;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Set;
012:
013: import org.drools.eclipse.DroolsEclipsePlugin;
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.jdt.core.IClasspathEntry;
020: import org.eclipse.jdt.core.IJavaProject;
021: import org.eclipse.jdt.core.JavaCore;
022: import org.eclipse.jdt.core.JavaModelException;
023: import org.eclipse.ui.IEditorInput;
024: import org.eclipse.ui.IEditorPart;
025: import org.eclipse.ui.IFileEditorInput;
026:
027: public class ProjectClassLoader {
028:
029: public static URLClassLoader getProjectClassLoader(
030: IEditorPart editor) {
031: IEditorInput input = editor.getEditorInput();
032: if (input instanceof IFileEditorInput) {
033: return getProjectClassLoader(((IFileEditorInput) input)
034: .getFile());
035: }
036: return null;
037: }
038:
039: public static URLClassLoader getProjectClassLoader(IFile file) {
040: IProject project = file.getProject();
041: IJavaProject javaProject = JavaCore.create(project);
042: return getProjectClassLoader(javaProject);
043: }
044:
045: public static URLClassLoader getProjectClassLoader(
046: IJavaProject project) {
047: List pathElements = getProjectClassPathURLs(project);
048: URL urlPaths[] = (URL[]) pathElements
049: .toArray(new URL[pathElements.size()]);
050: return new URLClassLoader(urlPaths, Thread.currentThread()
051: .getContextClassLoader());
052: }
053:
054: private static URL getRawLocationURL(IPath simplePath)
055: throws MalformedURLException {
056: File file = getRawLocationFile(simplePath);
057: return file.toURI().toURL();
058: }
059:
060: private static File getRawLocationFile(IPath simplePath) {
061: IResource resource = ResourcesPlugin.getWorkspace().getRoot()
062: .findMember(simplePath);
063: File file = null;
064: if (resource != null) {
065: file = ResourcesPlugin.getWorkspace().getRoot().findMember(
066: simplePath).getRawLocation().toFile();
067: } else {
068: file = simplePath.toFile();
069: }
070: return file;
071: }
072:
073: public static List getProjectClassPathURLs(IJavaProject project) {
074: List pathElements = new ArrayList();
075: try {
076: IClasspathEntry[] paths = project
077: .getResolvedClasspath(true);
078: Set outputPaths = new HashSet();
079: if (paths != null) {
080:
081: for (int i = 0; i < paths.length; i++) {
082: IClasspathEntry path = paths[i];
083: if (path.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
084: URL url = getRawLocationURL(path.getPath());
085: pathElements.add(url);
086: } else if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
087: IPath output = path.getOutputLocation();
088: if (path.getOutputLocation() != null) {
089: outputPaths.add(output);
090: }
091: }
092: }
093: }
094: IPath location = getProjectLocation(project.getProject());
095: IPath outputPath = location.append(project
096: .getOutputLocation().removeFirstSegments(1));
097: pathElements.add(outputPath.toFile().toURI().toURL());
098: for (Iterator iterator = outputPaths.iterator(); iterator
099: .hasNext();) {
100: IPath path = (IPath) iterator.next();
101: outputPath = location.append(path
102: .removeFirstSegments(1));
103: pathElements.add(outputPath.toFile().toURI().toURL());
104: }
105:
106: // also add classpath of required projects
107: String[] names = project.getRequiredProjectNames();
108: for (int i = 0; i < names.length; i++) {
109: String projectName = names[i];
110: IProject reqProject = project.getProject()
111: .getWorkspace().getRoot().getProject(
112: projectName);
113: if (reqProject != null) {
114: IJavaProject reqJavaProject = JavaCore
115: .create(reqProject);
116: pathElements
117: .addAll(getProjectClassPathURLs(reqJavaProject));
118: }
119: }
120: } catch (JavaModelException e) {
121: DroolsEclipsePlugin.log(e);
122: } catch (MalformedURLException e) {
123: DroolsEclipsePlugin.log(e);
124: }
125: return pathElements;
126: }
127:
128: public static IPath getProjectLocation(IProject project) {
129: if (project.getRawLocation() == null) {
130: return project.getLocation();
131: } else {
132: return project.getRawLocation();
133: }
134: }
135: }
|