001: package org.drools.eclipse.util;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.ArrayList;
006: import java.util.List;
007:
008: import org.drools.eclipse.DroolsEclipsePlugin;
009: import org.eclipse.core.runtime.FileLocator;
010: import org.eclipse.core.runtime.IPath;
011: import org.eclipse.core.runtime.Path;
012: import org.eclipse.core.runtime.Platform;
013: import org.eclipse.jdt.core.IClasspathContainer;
014: import org.eclipse.jdt.core.IClasspathEntry;
015: import org.eclipse.jdt.core.IJavaProject;
016: import org.eclipse.jdt.core.JavaCore;
017:
018: public class DroolsClasspathContainer implements IClasspathContainer {
019:
020: IClasspathEntry droolsLibraryEntries[];
021: IPath path;
022: IJavaProject javaProject;
023:
024: public DroolsClasspathContainer(IJavaProject project, IPath path) {
025: javaProject = null;
026: javaProject = project;
027: this .path = path;
028: }
029:
030: public IClasspathEntry[] getClasspathEntries() {
031: if (droolsLibraryEntries == null) {
032: droolsLibraryEntries = createDroolsLibraryEntries(javaProject);
033: }
034: return droolsLibraryEntries;
035: }
036:
037: public String getDescription() {
038: return "Drools Library";
039: }
040:
041: public int getKind() {
042: return 1;
043: }
044:
045: public IPath getPath() {
046: return path;
047: }
048:
049: private IClasspathEntry[] createDroolsLibraryEntries(
050: IJavaProject project) {
051: List jarNames = getJarNames();
052: List list = new ArrayList();
053: for (int i = 0; i < jarNames.size(); i++) {
054: Path path = new Path((String) jarNames.get(i));
055: list.add(JavaCore.newLibraryEntry(path, path, null));
056: }
057: // also add jdt core jar from eclipse itself
058: String pluginRootString = Platform.getInstallLocation()
059: .getURL().getPath()
060: + "plugins/";
061: File pluginRoot = new Path(pluginRootString).toFile();
062: File[] files = pluginRoot.listFiles();
063: for (int i = 0; i < files.length; i++) {
064: if (files[i].getAbsolutePath().indexOf(
065: "org.eclipse.jdt.core_3.2") > -1) {
066: Path path = new Path(files[i].getAbsolutePath());
067: list.add(JavaCore.newLibraryEntry(path, path, null));
068: break;
069: }
070: }
071: return (IClasspathEntry[]) list
072: .toArray(new IClasspathEntry[list.size()]);
073: }
074:
075: private List getJarNames() {
076: String s = getDroolsLocation();
077: List list = new ArrayList();
078: File file = (new Path(s)).toFile();
079: addJarNames(file, list);
080: return list;
081: }
082:
083: private void addJarNames(File file, List list) {
084: File[] files = file.listFiles();
085: for (int i = 0; i < files.length; i++) {
086: if (files[i].isDirectory()
087: && files[i].getName().equals("lib")) {
088: File[] jarFiles = files[i].listFiles();
089: for (int j = 0; j < jarFiles.length; j++) {
090: if (jarFiles[j].getPath().endsWith(".jar")) {
091: list.add(jarFiles[j].getAbsolutePath());
092: }
093: }
094: }
095: }
096: }
097:
098: private String getDroolsLocation() {
099: try {
100: return FileLocator.toFileURL(
101: Platform.getBundle("org.drools.eclipse").getEntry(
102: "/")).getFile().toString();
103: } catch (IOException e) {
104: DroolsEclipsePlugin.log(e);
105: }
106: return null;
107: }
108: }
|