KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > JRERuntimeClasspathEntryResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching;
12
13
14 import java.io.File JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.debug.core.ILaunchConfiguration;
25 import org.eclipse.jdt.core.IAccessRule;
26 import org.eclipse.jdt.core.IClasspathAttribute;
27 import org.eclipse.jdt.core.IClasspathEntry;
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.JavaCore;
30 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
31 import org.eclipse.jdt.launching.IRuntimeClasspathEntryResolver2;
32 import org.eclipse.jdt.launching.IVMInstall;
33 import org.eclipse.jdt.launching.JavaRuntime;
34 import org.eclipse.jdt.launching.LibraryLocation;
35
36 /**
37  * Resolves for JRELIB_VARIABLE and JRE_CONTAINER
38  */

39 public class JRERuntimeClasspathEntryResolver implements IRuntimeClasspathEntryResolver2 {
40     
41     private static IAccessRule[] EMPTY_RULES = new IAccessRule[0];
42
43     /**
44      * @see IRuntimeClasspathEntryResolver#resolveRuntimeClasspathEntry(IRuntimeClasspathEntry, ILaunchConfiguration)
45      */

46     public IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, ILaunchConfiguration configuration) throws CoreException {
47         IVMInstall jre = null;
48         if (entry.getType() == IRuntimeClasspathEntry.CONTAINER && entry.getPath().segmentCount() > 1) {
49             // a specific VM
50
jre = JREContainerInitializer.resolveVM(entry.getPath());
51         } else {
52             // default VM for config
53
jre = JavaRuntime.computeVMInstall(configuration);
54         }
55         if (jre == null) {
56             // cannot resolve JRE
57
return new IRuntimeClasspathEntry[0];
58         }
59         return resolveLibraryLocations(jre, entry.getClasspathProperty());
60     }
61     
62     /**
63      * @see IRuntimeClasspathEntryResolver#resolveRuntimeClasspathEntry(IRuntimeClasspathEntry, IJavaProject)
64      */

65     public IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, IJavaProject project) throws CoreException {
66         IVMInstall jre = null;
67         if (entry.getType() == IRuntimeClasspathEntry.CONTAINER && entry.getPath().segmentCount() > 1) {
68             // a specific VM
69
jre = JREContainerInitializer.resolveVM(entry.getPath());
70         } else {
71             // default VM for project
72
jre = JavaRuntime.getVMInstall(project);
73         }
74         if (jre == null) {
75             // cannot resolve JRE
76
return new IRuntimeClasspathEntry[0];
77         }
78         return resolveLibraryLocations(jre, entry.getClasspathProperty());
79     }
80
81     /**
82      * Resolves libray locations for the given VM install
83      */

84     protected IRuntimeClasspathEntry[] resolveLibraryLocations(IVMInstall vm, int kind) {
85         LibraryLocation[] libs = vm.getLibraryLocations();
86         LibraryLocation[] defaultLibs = vm.getVMInstallType().getDefaultLibraryLocations(vm.getInstallLocation());
87         boolean overrideJavadoc = false;
88         if (libs == null) {
89             // default system libs
90
libs = defaultLibs;
91             overrideJavadoc = true;
92         } else if (!isSameArchives(libs, defaultLibs)) {
93             // determine if bootpath should be explicit
94
kind = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES;
95         }
96         if (kind == IRuntimeClasspathEntry.BOOTSTRAP_CLASSES) {
97             File JavaDoc vmInstallLocation= vm.getInstallLocation();
98             if (vmInstallLocation != null) {
99                 LibraryInfo libraryInfo= LaunchingPlugin.getLibraryInfo(vmInstallLocation.getAbsolutePath());
100                 if (libraryInfo != null) {
101                     // only return endorsed and bootstrap classpath entries if we have the info
102
// libs in the ext dirs are not loaded by the boot class loader
103
String JavaDoc[] extensionDirsArray = libraryInfo.getExtensionDirs();
104                     Set JavaDoc extensionDirsSet = new HashSet JavaDoc();
105                     for (int i = 0; i < extensionDirsArray.length; i++) {
106                         extensionDirsSet.add(extensionDirsArray[i]);
107                     }
108                     List JavaDoc resolvedEntries = new ArrayList JavaDoc(libs.length);
109                     for (int i = 0; i < libs.length; i++) {
110                         LibraryLocation location = libs[i];
111                         IPath libraryPath = location.getSystemLibraryPath();
112                         String JavaDoc dir = libraryPath.toFile().getParent();
113                         // exclude extension directory entries
114
if (!extensionDirsSet.contains(dir)) {
115                             resolvedEntries.add(resolveLibraryLocation(vm, location, kind, overrideJavadoc));
116                         }
117                     }
118                     return (IRuntimeClasspathEntry[]) resolvedEntries.toArray(new IRuntimeClasspathEntry[resolvedEntries.size()]);
119                 }
120             }
121         }
122         List JavaDoc resolvedEntries = new ArrayList JavaDoc(libs.length);
123         for (int i = 0; i < libs.length; i++) {
124             IPath systemLibraryPath = libs[i].getSystemLibraryPath();
125             if (systemLibraryPath.toFile().exists()) {
126                 resolvedEntries.add(resolveLibraryLocation(vm, libs[i], kind, overrideJavadoc));
127             }
128         }
129         return (IRuntimeClasspathEntry[]) resolvedEntries.toArray(new IRuntimeClasspathEntry[resolvedEntries.size()]);
130     }
131         
132     /**
133      * Return whether the given list of libraries refer to the same archives in the same
134      * order. Only considers the binary archive (not source or javadoc locations).
135      *
136      * @param libs
137      * @param defaultLibs
138      * @return whether the given list of libraries refer to the same archives in the same
139      * order
140      */

141     public static boolean isSameArchives(LibraryLocation[] libs, LibraryLocation[] defaultLibs) {
142         if (libs.length != defaultLibs.length) {
143             return false;
144         }
145         IPath dpath = null, lpath = null;
146         for (int i = 0; i < defaultLibs.length; i++) {
147             dpath = defaultLibs[i].getSystemLibraryPath();
148             lpath = libs[i].getSystemLibraryPath();
149             if(Platform.getOS().equals(Platform.OS_WIN32)) {
150                 //the .equals method of IPath ignores trailing seperators so we must as well
151
if (!dpath.removeTrailingSeparator().toOSString().equalsIgnoreCase(lpath.removeTrailingSeparator().toOSString())) {
152                     return false;
153                 }
154             } else if (!dpath.equals(lpath)) {
155                 return false;
156             }
157         }
158         return true;
159     }
160
161     /**
162      * @see IRuntimeClasspathEntryResolver#resolveVMInstall(IClasspathEntry)
163      */

164     public IVMInstall resolveVMInstall(IClasspathEntry entry) {
165         switch (entry.getEntryKind()) {
166             case IClasspathEntry.CPE_VARIABLE:
167                 if (entry.getPath().segment(0).equals(JavaRuntime.JRELIB_VARIABLE)) {
168                     return JavaRuntime.getDefaultVMInstall();
169                 }
170                 break;
171             case IClasspathEntry.CPE_CONTAINER:
172                 if (entry.getPath().segment(0).equals(JavaRuntime.JRE_CONTAINER)) {
173                     return JREContainerInitializer.resolveVM(entry.getPath());
174                 }
175                 break;
176             default:
177                 break;
178         }
179         return null;
180     }
181
182     /* (non-Javadoc)
183      * @see org.eclipse.jdt.launching.IRuntimeClasspathEntryResolver2#isVMInstallReference(org.eclipse.jdt.core.IClasspathEntry)
184      */

185     public boolean isVMInstallReference(IClasspathEntry entry) {
186         switch (entry.getEntryKind()) {
187             case IClasspathEntry.CPE_VARIABLE:
188                 if (entry.getPath().segment(0).equals(JavaRuntime.JRELIB_VARIABLE)) {
189                     return true;
190                 }
191                 break;
192             case IClasspathEntry.CPE_CONTAINER:
193                 if (entry.getPath().segment(0).equals(JavaRuntime.JRE_CONTAINER)) {
194                     return true;
195                 }
196                 break;
197             default:
198                 break;
199         }
200         return false;
201     }
202     
203     /**
204      * Returns a runtime classpath entry for the given library in the specified VM.
205      *
206      * @param vm
207      * @param location
208      * @param kind
209      * @return runtime classpath entry
210      * @since 3.2
211      */

212     private IRuntimeClasspathEntry resolveLibraryLocation(IVMInstall vm, LibraryLocation location, int kind, boolean overrideJavaDoc) {
213         IPath libraryPath = location.getSystemLibraryPath();
214         URL JavaDoc javadocLocation = location.getJavadocLocation();
215         if (overrideJavaDoc && javadocLocation == null) {
216             javadocLocation = vm.getJavadocLocation();
217         }
218         IClasspathAttribute[] attributes = null;
219         if (javadocLocation == null) {
220             attributes = new IClasspathAttribute[0];
221         } else {
222             attributes = new IClasspathAttribute[]{JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation.toExternalForm())};
223         }
224         IClasspathEntry cpe = JavaCore.newLibraryEntry(libraryPath, location.getSystemLibraryPath(), location.getPackageRootPath(), EMPTY_RULES, attributes, false);
225         IRuntimeClasspathEntry resolved = new RuntimeClasspathEntry(cpe);
226         resolved.setClasspathProperty(kind);
227         IPath sourcePath = location.getSystemLibrarySourcePath();
228         if (sourcePath != null && !sourcePath.isEmpty()) {
229             resolved.setSourceAttachmentPath(sourcePath);
230             resolved.setSourceAttachmentRootPath(location.getPackageRootPath());
231         }
232         return resolved;
233     }
234
235 }
236
Popular Tags