KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > ClasspathProvider


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package org.terracotta.dso;
6
7 import org.apache.commons.io.FileUtils;
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IPath;
10 import org.eclipse.core.runtime.Path;
11 import org.eclipse.debug.core.ILaunchConfiguration;
12 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
13 import org.eclipse.jdt.launching.JavaRuntime;
14 import org.eclipse.jdt.launching.StandardClasspathProvider;
15 import org.xml.sax.Attributes JavaDoc;
16 import org.xml.sax.helpers.DefaultHandler JavaDoc;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.xml.parsers.SAXParser JavaDoc;
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29
30 public class ClasspathProvider extends StandardClasspathProvider {
31   public ClasspathProvider() {
32     super();
33   }
34
35   public IRuntimeClasspathEntry[] computeUnresolvedClasspath(ILaunchConfiguration configuration) throws CoreException {
36     IRuntimeClasspathEntry[] cpe = super.computeUnresolvedClasspath(configuration);
37     IPath jarPath = TcPlugin.getDefault().getLibDirPath().append("tc.jar");
38
39     if (jarPath.toFile().exists()) {
40       IRuntimeClasspathEntry[] result = new IRuntimeClasspathEntry[cpe.length + 1];
41
42       System.arraycopy(cpe, 0, result, 0, cpe.length);
43       result[cpe.length] = JavaRuntime.newArchiveRuntimeClasspathEntry(jarPath.makeAbsolute());
44
45       return result;
46     } else {
47       ArrayList JavaDoc<IRuntimeClasspathEntry> list = new ArrayList JavaDoc<IRuntimeClasspathEntry>(Arrays.asList(cpe));
48       IPath[] paths = gatherDevClasspathEntries();
49
50       for (int i = 0; i < paths.length; i++) {
51         list.add(JavaRuntime.newArchiveRuntimeClasspathEntry(paths[i]));
52       }
53
54       return list.toArray(new IRuntimeClasspathEntry[0]);
55     }
56   }
57
58   public static String JavaDoc makeDevClasspath() {
59     IPath[] paths = gatherDevClasspathEntries();
60     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
61     String JavaDoc sep = System.getProperty("path.separator");
62
63     for (int i = 0; i < paths.length; i++) {
64       if (i > 0) sb.append(sep);
65       sb.append(paths[i].toOSString());
66     }
67
68     return sb.toString();
69   }
70
71   private static Collection JavaDoc<File JavaDoc> listArchives(File JavaDoc dir) {
72     Collection JavaDoc c = FileUtils.listFiles(dir, new String JavaDoc[] { "jar" }, false);
73     Collection JavaDoc<File JavaDoc> result = new HashSet JavaDoc<File JavaDoc>();
74     Iterator JavaDoc iter = c.iterator();
75
76     while (iter.hasNext()) {
77       result.add((File JavaDoc) iter.next());
78     }
79
80     return result;
81   }
82   
83   private static IPath[] gatherDevClasspathEntries() {
84     IPath location = TcPlugin.getDefault().getLocation();
85     List JavaDoc<IPath> list = new ArrayList JavaDoc<IPath>();
86     IPath buildPath = location.append("..");
87
88     String JavaDoc[] dirs = { "deploy", "common", "management", "aspectwerkz", "thirdparty", "dso-common", "dso-common-jdk15",
89         "dso-l1", "dso-l1-jdk15", "dso-l2" };
90
91     for (int i = 0; i < dirs.length; i++) {
92       list.add(buildPath.append(dirs[i]).append("build.eclipse").append("src.classes"));
93     }
94
95     final List JavaDoc<File JavaDoc> fileList = new ArrayList JavaDoc<File JavaDoc>();
96     File JavaDoc libDir;
97
98     for (int i = 0; i < dirs.length; i++) {
99       libDir = location.append("..").append(dirs[i]).append("lib").toFile();
100
101       if (libDir.exists()) {
102         fileList.addAll(listArchives(libDir));
103       }
104     }
105
106     final File JavaDoc dependencies = location.append("..").append("dependencies").append("lib").toFile();
107     File JavaDoc ivy = null;
108     File JavaDoc projectDir = null;
109     String JavaDoc dir = null;
110     try {
111       for (int i = 0; i < dirs.length; i++) {
112         dir = dirs[i];
113         projectDir = location.append("..").append(dir).toFile();
114         ivy = new File JavaDoc(projectDir + File.separator + "ivy.xml");
115         if (ivy.exists()) {
116           SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
117           SAXParser JavaDoc parser = factory.newSAXParser();
118           parser.parse(new FileInputStream JavaDoc(ivy), new DefaultHandler JavaDoc() {
119             public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) {
120               if (qName.equals("dependency")) {
121                 String JavaDoc jar = attributes.getValue("name") + "-" + attributes.getValue("rev");
122                 File JavaDoc jarFile = new File JavaDoc(dependencies + File.separator + jar + ".jar");
123                 fileList.add(jarFile);
124               }
125             }
126           });
127         }
128       }
129     } catch (Throwable JavaDoc e) {
130       TcPlugin.getDefault().openError("Problem Parsing ivy.xml file in: " + dir, e);
131     }
132
133     Iterator JavaDoc fileIter = fileList.iterator();
134     File JavaDoc file;
135
136     while (fileIter.hasNext()) {
137       file = (File JavaDoc) fileIter.next();
138       if (!file.getName().startsWith("org.eclipse")) {
139         list.add(new Path(file.getAbsolutePath()));
140       }
141     }
142
143     return list.toArray(new IPath[list.size()]);
144   }
145 }
146
Popular Tags