001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso;
006:
007: import org.apache.commons.io.FileUtils;
008: import org.eclipse.core.runtime.IPath;
009: import org.eclipse.core.runtime.Path;
010: import org.eclipse.debug.core.ILaunchConfiguration;
011: import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
012: import org.eclipse.jdt.launching.JavaRuntime;
013: import org.eclipse.jdt.launching.StandardClasspathProvider;
014: import org.xml.sax.Attributes;
015: import org.xml.sax.helpers.DefaultHandler;
016:
017: import java.io.File;
018: import java.io.FileInputStream;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.xml.parsers.SAXParser;
026: import javax.xml.parsers.SAXParserFactory;
027:
028: /**
029: * ClasspathProvider for Terracotta processes, such as TCServer and BootJarTool.
030: * Not used for DSO clients.
031: */
032: public class ClasspathProvider extends StandardClasspathProvider {
033: public ClasspathProvider() {
034: super ();
035: }
036:
037: public IRuntimeClasspathEntry[] computeUnresolvedClasspath(
038: ILaunchConfiguration configuration) {
039: IPath jarPath = TcPlugin.getDefault().getLibDirPath().append(
040: "tc.jar");
041:
042: if (jarPath.toFile().exists()) {
043: return new IRuntimeClasspathEntry[] { JavaRuntime
044: .newArchiveRuntimeClasspathEntry(jarPath
045: .makeAbsolute()) };
046: } else {
047: ArrayList<IRuntimeClasspathEntry> list = new ArrayList<IRuntimeClasspathEntry>();
048: IPath[] paths = gatherDevClasspathEntries();
049:
050: for (int i = 0; i < paths.length; i++) {
051: list.add(JavaRuntime
052: .newArchiveRuntimeClasspathEntry(paths[i]));
053: }
054:
055: return list.toArray(new IRuntimeClasspathEntry[0]);
056: }
057: }
058:
059: public static String makeDevClasspath() {
060: IPath[] paths = gatherDevClasspathEntries();
061: StringBuffer sb = new StringBuffer();
062: String sep = System.getProperty("path.separator");
063:
064: for (int i = 0; i < paths.length; i++) {
065: if (i > 0)
066: sb.append(sep);
067: sb.append(paths[i].toOSString());
068: }
069:
070: return sb.toString();
071: }
072:
073: private static Collection<File> listArchives(File dir) {
074: Collection c = FileUtils.listFiles(dir, new String[] { "jar" },
075: false);
076: Collection<File> result = new HashSet<File>();
077: Iterator iter = c.iterator();
078:
079: while (iter.hasNext()) {
080: result.add((File) iter.next());
081: }
082:
083: return result;
084: }
085:
086: private static IPath[] gatherDevClasspathEntries() {
087: IPath location = TcPlugin.getDefault().getLocation();
088: List<IPath> list = new ArrayList<IPath>();
089: IPath buildPath = location.append("..");
090:
091: String[] dirs = { "deploy", "common", "common-api",
092: "management", "aspectwerkz", "thirdparty",
093: "thirdparty-api", "dso-common", "dso-common-jdk15",
094: "dso-l1", "dso-l1-api", "dso-l1-jdk15", "dso-l2",
095: "dso-l2-common", "dso-spring" };
096:
097: for (int i = 0; i < dirs.length; i++) {
098: list.add(buildPath.append(dirs[i]).append("build.eclipse")
099: .append("src.classes"));
100: }
101:
102: final List<File> fileList = new ArrayList<File>();
103: File libDir;
104:
105: for (int i = 0; i < dirs.length; i++) {
106: libDir = location.append("..").append(dirs[i])
107: .append("lib").toFile();
108:
109: if (libDir.exists()) {
110: fileList.addAll(listArchives(libDir));
111: }
112: }
113:
114: final File dependencies = location.append("..").append(
115: "dependencies").append("lib").toFile();
116: File ivy = null;
117: File projectDir = null;
118: String dir = null;
119: try {
120: for (int i = 0; i < dirs.length; i++) {
121: dir = dirs[i];
122: projectDir = location.append("..").append(dir).toFile();
123: ivy = new File(projectDir + File.separator + "ivy.xml");
124: if (ivy.exists()) {
125: SAXParserFactory factory = SAXParserFactory
126: .newInstance();
127: SAXParser parser = factory.newSAXParser();
128: parser.parse(new FileInputStream(ivy),
129: new DefaultHandler() {
130: public void startElement(String uri,
131: String localName, String qName,
132: Attributes attributes) {
133: if (qName.equals("dependency")) {
134: String jar = attributes
135: .getValue("name")
136: + "-"
137: + attributes
138: .getValue("rev");
139: File jarFile = new File(
140: dependencies
141: + File.separator
142: + jar + ".jar");
143: fileList.add(jarFile);
144: }
145: }
146: });
147: }
148: }
149: } catch (Throwable e) {
150: TcPlugin.getDefault().openError(
151: "Problem Parsing ivy.xml file in: " + dir, e);
152: }
153:
154: Iterator fileIter = fileList.iterator();
155: File file;
156:
157: while (fileIter.hasNext()) {
158: file = (File) fileIter.next();
159: if (!file.getName().startsWith("org.eclipse")) {
160: list.add(new Path(file.getAbsolutePath()));
161: }
162: }
163:
164: return list.toArray(new IPath[list.size()]);
165: }
166: }
|