001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.ui.launcher;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.debug.core.ILaunchConfiguration;
021: import org.eclipse.jdt.core.IJavaProject;
022: import org.eclipse.jdt.core.IPackageFragmentRoot;
023: import org.eclipse.jdt.core.JavaCore;
024: import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
025: import org.eclipse.jdt.launching.IVMInstall;
026: import org.eclipse.jdt.launching.JavaRuntime;
027: import org.eclipse.jdt.launching.StandardSourcePathProvider;
028: import org.eclipse.pde.internal.core.util.PDEJavaHelper;
029: import org.eclipse.pde.internal.ui.PDEPlugin;
030: import org.eclipse.pde.internal.ui.launcher.LaunchPluginValidator;
031: import org.eclipse.pde.internal.ui.launcher.VMHelper;
032:
033: /**
034: * Generates a source lookup path for all PDE-based launch configurations
035: * <p>
036: * Clients may subclass this class.
037: * </p>
038: * @since 3.3
039: */
040: public class PDESourcePathProvider extends StandardSourcePathProvider {
041:
042: public static final String ID = "org.eclipse.pde.ui.workbenchClasspathProvider"; //$NON-NLS-1$
043:
044: /*
045: * (non-Javadoc)
046: * @see org.eclipse.jdt.launching.StandardSourcePathProvider#computeUnresolvedClasspath(org.eclipse.debug.core.ILaunchConfiguration)
047: */
048: public IRuntimeClasspathEntry[] computeUnresolvedClasspath(
049: ILaunchConfiguration configuration) throws CoreException {
050: List sourcePath = new ArrayList();
051: sourcePath.add(getJREEntry(configuration));
052: IProject[] projects = getJavaProjects(configuration);
053: for (int i = 0; i < projects.length; i++) {
054: sourcePath.add(JavaRuntime
055: .newProjectRuntimeClasspathEntry(JavaCore
056: .create(projects[i])));
057: }
058: return (IRuntimeClasspathEntry[]) sourcePath
059: .toArray(new IRuntimeClasspathEntry[sourcePath.size()]);
060: }
061:
062: /**
063: * Returns a JRE runtime classpath entry
064: *
065: * @param configuration
066: * the launch configuration
067: * @return a JRE runtime classpath entry
068: * @throws CoreException
069: * if the JRE associated with the launch configuration cannot be found
070: * or if unable to retrieve the launch configuration attributes
071: */
072: private IRuntimeClasspathEntry getJREEntry(
073: ILaunchConfiguration configuration) throws CoreException {
074: IVMInstall jre = VMHelper.createLauncher(configuration);
075: IPath containerPath = new Path(JavaRuntime.JRE_CONTAINER);
076: containerPath = containerPath.append(jre.getVMInstallType()
077: .getId());
078: containerPath = containerPath.append(jre.getName());
079: return JavaRuntime
080: .newRuntimeContainerClasspathEntry(containerPath,
081: IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
082: }
083:
084: /**
085: * Returns an array of sorted plug-in projects that represent plug-ins participating
086: * in the launch
087: *
088: * @param configuration
089: * the launch configuration
090: * @return an array of ordered projects
091: * @throws CoreException
092: * if unable to retrieve attributes from the launch configuration or if
093: * an error occurs when checking the nature of the project
094: *
095: */
096: private IProject[] getJavaProjects(
097: ILaunchConfiguration configuration) throws CoreException {
098: IProject[] projects = LaunchPluginValidator
099: .getAffectedProjects(configuration);
100: return PDEPlugin.getWorkspace().computeProjectOrder(projects).projects;
101: }
102:
103: /*
104: * (non-Javadoc)
105: * @see org.eclipse.jdt.launching.StandardSourcePathProvider#resolveClasspath(org.eclipse.jdt.launching.IRuntimeClasspathEntry[], org.eclipse.debug.core.ILaunchConfiguration)
106: */
107: public IRuntimeClasspathEntry[] resolveClasspath(
108: IRuntimeClasspathEntry[] entries,
109: ILaunchConfiguration configuration) throws CoreException {
110: List all = new ArrayList(entries.length);
111: for (int i = 0; i < entries.length; i++) {
112: if (entries[i].getType() == IRuntimeClasspathEntry.PROJECT) {
113: // a project resolves to itself for source lookup (rather than
114: // the class file output locations)
115: all.add(entries[i]);
116: // also add non-JRE libraries
117: IResource resource = entries[i].getResource();
118: if (resource instanceof IProject) {
119: addBinaryPackageFragmentRoots(JavaCore
120: .create((IProject) resource), all);
121: }
122: } else {
123: IRuntimeClasspathEntry[] resolved = JavaRuntime
124: .resolveRuntimeClasspathEntry(entries[i],
125: configuration);
126: for (int j = 0; j < resolved.length; j++) {
127: all.add(resolved[j]);
128: }
129: }
130: }
131: return (IRuntimeClasspathEntry[]) all
132: .toArray(new IRuntimeClasspathEntry[all.size()]);
133: }
134:
135: /**
136: * Adds runtime classpath entries for binary package fragment roots contained within
137: * the project
138: *
139: * @param jProject
140: * the Java project whose roots are to be enumerated
141: * @param all
142: * a list of accumulated runtime classpath entries
143: * @throws CoreException
144: * if unable to evaluate the package fragment roots
145: */
146: private void addBinaryPackageFragmentRoots(IJavaProject jProject,
147: List all) throws CoreException {
148: IPackageFragmentRoot[] roots = jProject
149: .getPackageFragmentRoots();
150: for (int j = 0; j < roots.length; j++) {
151: if (roots[j].getKind() == IPackageFragmentRoot.K_BINARY
152: && !PDEJavaHelper.isJRELibrary(roots[j])) {
153: IRuntimeClasspathEntry rte = JavaRuntime
154: .newArchiveRuntimeClasspathEntry(roots[j]
155: .getPath());
156: IPath path = roots[j].getSourceAttachmentPath();
157: if (path != null) {
158: rte.setSourceAttachmentPath(path);
159: rte.setSourceAttachmentRootPath(roots[j]
160: .getSourceAttachmentRootPath());
161: }
162: if (!all.contains(rte))
163: all.add(rte);
164: }
165: }
166:
167: }
168: }
|