001: /*******************************************************************************
002: * Copyright (c) 2000, 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.jdt.launching;
011:
012: import java.util.Collections;
013: import java.util.Iterator;
014: import java.util.LinkedHashSet;
015: import java.util.List;
016: import java.util.Set;
017:
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.debug.core.ILaunchConfiguration;
020: import org.eclipse.jdt.core.IJavaProject;
021:
022: /**
023: * Default implementation for classpath provider.
024: * <p>
025: * This class may be subclassed.
026: * </p>
027: * @since 2.0
028: */
029: public class StandardClasspathProvider implements
030: IRuntimeClasspathProvider {
031:
032: /* (non-Javadoc)
033: * @see org.eclipse.jdt.launching.IRuntimeClasspathProvider#computeUnresolvedClasspath(org.eclipse.debug.core.ILaunchConfiguration)
034: */
035: public IRuntimeClasspathEntry[] computeUnresolvedClasspath(
036: ILaunchConfiguration configuration) throws CoreException {
037: boolean useDefault = configuration
038: .getAttribute(
039: IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,
040: true);
041: if (useDefault) {
042: IJavaProject proj = JavaRuntime
043: .getJavaProject(configuration);
044: IRuntimeClasspathEntry jreEntry = JavaRuntime
045: .computeJREEntry(configuration);
046: if (proj == null) {
047: //no project - use default libraries
048: if (jreEntry == null) {
049: return new IRuntimeClasspathEntry[0];
050: }
051: return new IRuntimeClasspathEntry[] { jreEntry };
052: }
053: IRuntimeClasspathEntry[] entries = JavaRuntime
054: .computeUnresolvedRuntimeClasspath(proj);
055: // replace project JRE with config's JRE
056: IRuntimeClasspathEntry projEntry = JavaRuntime
057: .computeJREEntry(proj);
058: if (jreEntry != null && projEntry != null) {
059: if (!jreEntry.equals(projEntry)) {
060: for (int i = 0; i < entries.length; i++) {
061: IRuntimeClasspathEntry entry = entries[i];
062: if (entry.equals(projEntry)) {
063: entries[i] = jreEntry;
064: return entries;
065: }
066: }
067: }
068: }
069: return entries;
070: }
071: // recover persisted classpath
072: return recoverRuntimePath(configuration,
073: IJavaLaunchConfigurationConstants.ATTR_CLASSPATH);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.jdt.launching.IRuntimeClasspathProvider#resolveClasspath(org.eclipse.jdt.launching.IRuntimeClasspathEntry[], org.eclipse.debug.core.ILaunchConfiguration)
078: */
079: public IRuntimeClasspathEntry[] resolveClasspath(
080: IRuntimeClasspathEntry[] entries,
081: ILaunchConfiguration configuration) throws CoreException {
082: // use an ordered set to avoid duplicates
083: Set all = new LinkedHashSet(entries.length);
084: for (int i = 0; i < entries.length; i++) {
085: IRuntimeClasspathEntry[] resolved = JavaRuntime
086: .resolveRuntimeClasspathEntry(entries[i],
087: configuration);
088: for (int j = 0; j < resolved.length; j++) {
089: all.add(resolved[j]);
090: }
091: }
092: return (IRuntimeClasspathEntry[]) all
093: .toArray(new IRuntimeClasspathEntry[all.size()]);
094: }
095:
096: /**
097: * Returns a collection of runtime classpath entries that are defined in the
098: * specified attribute of the given launch configuration. When present,
099: * the attribute must contain a list of runtime classpath entry mementos.
100: *
101: * @param configuration launch configuration
102: * @param attribute attribute name containing the list of entries
103: * @return collection of runtime classpath entries that are defined in the
104: * specified attribute of the given launch configuration
105: * @exception CoreException if unable to retrieve the list
106: */
107: protected IRuntimeClasspathEntry[] recoverRuntimePath(
108: ILaunchConfiguration configuration, String attribute)
109: throws CoreException {
110: List entries = configuration.getAttribute(attribute,
111: Collections.EMPTY_LIST);
112: IRuntimeClasspathEntry[] rtes = new IRuntimeClasspathEntry[entries
113: .size()];
114: Iterator iter = entries.iterator();
115: int i = 0;
116: while (iter.hasNext()) {
117: rtes[i] = JavaRuntime
118: .newRuntimeClasspathEntry((String) iter.next());
119: i++;
120: }
121: return rtes;
122: }
123:
124: }
|