KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > launcher > PDESourcePathProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.pde.ui.launcher;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragmentRoot;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
25 import org.eclipse.jdt.launching.IVMInstall;
26 import org.eclipse.jdt.launching.JavaRuntime;
27 import org.eclipse.jdt.launching.StandardSourcePathProvider;
28 import org.eclipse.pde.internal.core.util.PDEJavaHelper;
29 import org.eclipse.pde.internal.ui.PDEPlugin;
30 import org.eclipse.pde.internal.ui.launcher.LaunchPluginValidator;
31 import org.eclipse.pde.internal.ui.launcher.VMHelper;
32
33 /**
34  * Generates a source lookup path for all PDE-based launch configurations
35  * <p>
36  * Clients may subclass this class.
37  * </p>
38  * @since 3.3
39  */

40 public class PDESourcePathProvider extends StandardSourcePathProvider {
41
42     public static final String JavaDoc ID = "org.eclipse.pde.ui.workbenchClasspathProvider"; //$NON-NLS-1$
43

44     /*
45      * (non-Javadoc)
46      * @see org.eclipse.jdt.launching.StandardSourcePathProvider#computeUnresolvedClasspath(org.eclipse.debug.core.ILaunchConfiguration)
47      */

48     public IRuntimeClasspathEntry[] computeUnresolvedClasspath(ILaunchConfiguration configuration) throws CoreException {
49         List JavaDoc sourcePath = new ArrayList JavaDoc();
50         sourcePath.add(getJREEntry(configuration));
51         IProject[] projects = getJavaProjects(configuration);
52         for (int i = 0; i < projects.length; i++) {
53             sourcePath.add(JavaRuntime.newProjectRuntimeClasspathEntry(JavaCore.create(projects[i])));
54         }
55         return (IRuntimeClasspathEntry[]) sourcePath.toArray(new IRuntimeClasspathEntry[sourcePath.size()]);
56     }
57     
58     /**
59      * Returns a JRE runtime classpath entry
60      *
61      * @param configuration
62      * the launch configuration
63      * @return a JRE runtime classpath entry
64      * @throws CoreException
65      * if the JRE associated with the launch configuration cannot be found
66      * or if unable to retrieve the launch configuration attributes
67      */

68     private IRuntimeClasspathEntry getJREEntry(ILaunchConfiguration configuration) throws CoreException {
69         IVMInstall jre = VMHelper.createLauncher(configuration);
70         IPath containerPath = new Path(JavaRuntime.JRE_CONTAINER);
71         containerPath = containerPath.append(jre.getVMInstallType().getId());
72         containerPath = containerPath.append(jre.getName());
73         return JavaRuntime.newRuntimeContainerClasspathEntry(containerPath, IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
74     }
75     
76     /**
77      * Returns an array of sorted plug-in projects that represent plug-ins participating
78      * in the launch
79      *
80      * @param configuration
81      * the launch configuration
82      * @return an array of ordered projects
83      * @throws CoreException
84      * if unable to retrieve attributes from the launch configuration or if
85      * an error occurs when checking the nature of the project
86      *
87      */

88     private IProject[] getJavaProjects(ILaunchConfiguration configuration) throws CoreException {
89         IProject[] projects = LaunchPluginValidator.getAffectedProjects(configuration);
90         return PDEPlugin.getWorkspace().computeProjectOrder(projects).projects;
91     }
92
93     /*
94      * (non-Javadoc)
95      * @see org.eclipse.jdt.launching.StandardSourcePathProvider#resolveClasspath(org.eclipse.jdt.launching.IRuntimeClasspathEntry[], org.eclipse.debug.core.ILaunchConfiguration)
96      */

97     public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration)
98             throws CoreException {
99         List JavaDoc all = new ArrayList JavaDoc(entries.length);
100         for (int i = 0; i < entries.length; i++) {
101             if (entries[i].getType() == IRuntimeClasspathEntry.PROJECT) {
102                 // a project resolves to itself for source lookup (rather than
103
// the class file output locations)
104
all.add(entries[i]);
105                 // also add non-JRE libraries
106
IResource resource = entries[i].getResource();
107                 if (resource instanceof IProject) {
108                     addBinaryPackageFragmentRoots(JavaCore.create((IProject)resource), all);
109                 }
110             } else {
111                 IRuntimeClasspathEntry[] resolved = JavaRuntime.resolveRuntimeClasspathEntry(entries[i], configuration);
112                 for (int j = 0; j < resolved.length; j++) {
113                     all.add(resolved[j]);
114                 }
115             }
116         }
117         return (IRuntimeClasspathEntry[]) all.toArray(new IRuntimeClasspathEntry[all.size()]);
118     }
119     
120     /**
121      * Adds runtime classpath entries for binary package fragment roots contained within
122      * the project
123      *
124      * @param jProject
125      * the Java project whose roots are to be enumerated
126      * @param all
127      * a list of accumulated runtime classpath entries
128      * @throws CoreException
129      * if unable to evaluate the package fragment roots
130      */

131     private void addBinaryPackageFragmentRoots(IJavaProject jProject, List JavaDoc all) throws CoreException {
132         IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
133         for (int j = 0; j < roots.length; j++) {
134             if (roots[j].getKind() == IPackageFragmentRoot.K_BINARY && !PDEJavaHelper.isJRELibrary(roots[j])) {
135                 IRuntimeClasspathEntry rte = JavaRuntime.newArchiveRuntimeClasspathEntry(roots[j].getPath());
136                 IPath path = roots[j].getSourceAttachmentPath();
137                 if (path != null) {
138                     rte.setSourceAttachmentPath(path);
139                     rte.setSourceAttachmentRootPath(roots[j].getSourceAttachmentRootPath());
140                 }
141                 if (!all.contains(rte))
142                     all.add(rte);
143             }
144         }
145         
146     }
147 }
Popular Tags