001: /*******************************************************************************
002: * Copyright (c) 2005, 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.io.File;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.Map;
017: import java.util.Properties;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.Path;
022: import org.eclipse.debug.core.ILaunch;
023: import org.eclipse.debug.core.ILaunchConfiguration;
024: import org.eclipse.pde.core.plugin.IFragmentModel;
025: import org.eclipse.pde.core.plugin.IPluginModelBase;
026: import org.eclipse.pde.core.plugin.PluginRegistry;
027: import org.eclipse.pde.core.plugin.TargetPlatform;
028: import org.eclipse.pde.internal.core.ClasspathHelper;
029: import org.eclipse.pde.internal.core.util.CoreUtility;
030: import org.eclipse.pde.internal.ui.IPDEUIConstants;
031: import org.eclipse.pde.internal.ui.PDEUIMessages;
032: import org.eclipse.pde.internal.ui.launcher.BundleLauncherHelper;
033: import org.eclipse.pde.internal.ui.launcher.LaunchConfigurationHelper;
034: import org.eclipse.pde.internal.ui.launcher.LaunchPluginValidator;
035: import org.eclipse.pde.internal.ui.launcher.LauncherUtils;
036: import org.eclipse.pde.internal.ui.launcher.OSGiValidationOperation;
037:
038: /**
039: * A launch delegate for launching the Equinox framework
040: * <p>
041: * Clients may subclass and instantiate this class.
042: * </p>
043: * @since 3.2
044: */
045: public class EquinoxLaunchConfiguration extends
046: AbstractPDELaunchConfiguration {
047:
048: // used to generate the dev classpath entries
049: // key is bundle ID, value is a model
050: protected Map fAllBundles;
051:
052: // key is a model, value is startLevel:autoStart
053: private Map fModels;
054:
055: /*
056: * (non-Javadoc)
057: * @see org.eclipse.pde.ui.launcher.AbstractPDELaunchConfiguration#getProgramArguments(org.eclipse.debug.core.ILaunchConfiguration)
058: */
059: public String[] getProgramArguments(
060: ILaunchConfiguration configuration) throws CoreException {
061: ArrayList programArgs = new ArrayList();
062:
063: programArgs.add("-dev"); //$NON-NLS-1$
064: programArgs.add(ClasspathHelper.getDevEntriesProperties(
065: getConfigDir(configuration).toString()
066: + "/dev.properties", fAllBundles)); //$NON-NLS-1$
067:
068: saveConfigurationFile(configuration);
069: programArgs.add("-configuration"); //$NON-NLS-1$
070: programArgs
071: .add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); //$NON-NLS-1$
072:
073: String[] args = super .getProgramArguments(configuration);
074: for (int i = 0; i < args.length; i++) {
075: programArgs.add(args[i]);
076: }
077: return (String[]) programArgs.toArray(new String[programArgs
078: .size()]);
079: }
080:
081: private void saveConfigurationFile(
082: ILaunchConfiguration configuration) throws CoreException {
083: Properties properties = new Properties();
084: properties
085: .setProperty(
086: "osgi.install.area", "file:" + TargetPlatform.getLocation()); //$NON-NLS-1$ //$NON-NLS-2$
087: properties.setProperty("osgi.configuration.cascaded", "false"); //$NON-NLS-1$ //$NON-NLS-2$
088: properties
089: .put(
090: "osgi.framework", LaunchConfigurationHelper.getBundleURL("org.eclipse.osgi", fAllBundles)); //$NON-NLS-1$ //$NON-NLS-2$
091: int start = configuration.getAttribute(
092: IPDELauncherConstants.DEFAULT_START_LEVEL, 4);
093: properties
094: .put(
095: "osgi.bundles.defaultStartLevel", Integer.toString(start)); //$NON-NLS-1$
096: boolean autostart = configuration.getAttribute(
097: IPDELauncherConstants.DEFAULT_AUTO_START, true);
098: String bundles = getBundles(autostart);
099: if (bundles.length() > 0)
100: properties.put("osgi.bundles", bundles); //$NON-NLS-1$
101: if (!"3.3".equals(configuration.getAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, ""))) { //$NON-NLS-1$ //$NON-NLS-2$
102: properties.put("eclipse.ignoreApp", "true"); //$NON-NLS-1$ //$NON-NLS-2$
103: properties.put("osgi.noShutdown", "true"); //$NON-NLS-1$ //$NON-NLS-2$
104: }
105: LaunchConfigurationHelper.save(new File(
106: getConfigDir(configuration), "config.ini"), properties); //$NON-NLS-1$
107: }
108:
109: private String getBundles(boolean defaultAuto) {
110: StringBuffer buffer = new StringBuffer();
111: Iterator iter = fModels.keySet().iterator();
112: while (iter.hasNext()) {
113: IPluginModelBase model = (IPluginModelBase) iter.next();
114: String id = model.getPluginBase().getId();
115: if (!"org.eclipse.osgi".equals(id)) { //$NON-NLS-1$
116: if (buffer.length() > 0)
117: buffer.append(","); //$NON-NLS-1$
118: buffer.append("reference:"); //$NON-NLS-1$
119: buffer.append(LaunchConfigurationHelper
120: .getBundleURL(model));
121:
122: // fragments must not be started or have a start level
123: if (model instanceof IFragmentModel)
124: continue;
125:
126: String data = fModels.get(model).toString();
127: int index = data.indexOf(':');
128: String level = index > 0 ? data.substring(0, index)
129: : "default"; //$NON-NLS-1$
130: String auto = index > 0 && index < data.length() - 1 ? data
131: .substring(index + 1)
132: : "default"; //$NON-NLS-1$
133: if ("default".equals(auto)) //$NON-NLS-1$
134: auto = Boolean.toString(defaultAuto);
135: if (!level.equals("default") || "true".equals(auto)) //$NON-NLS-1$ //$NON-NLS-2$
136: buffer.append("@"); //$NON-NLS-1$
137:
138: if (!level.equals("default")) { //$NON-NLS-1$
139: buffer.append(level);
140: if ("true".equals(auto)) //$NON-NLS-1$
141: buffer.append(":"); //$NON-NLS-1$
142: }
143: if ("true".equals(auto)) { //$NON-NLS-1$
144: buffer.append("start"); //$NON-NLS-1$
145: }
146: }
147: }
148: return buffer.toString();
149: }
150:
151: /*
152: * (non-Javadoc)
153: * @see org.eclipse.pde.ui.launcher.AbstractPDELaunchConfiguration#preLaunchCheck(org.eclipse.debug.core.ILaunchConfiguration, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
154: */
155: protected void preLaunchCheck(ILaunchConfiguration configuration,
156: ILaunch launch, IProgressMonitor monitor)
157: throws CoreException {
158: fModels = BundleLauncherHelper.getMergedMap(configuration);
159: fAllBundles = new HashMap(fModels.size());
160: Iterator iter = fModels.keySet().iterator();
161: while (iter.hasNext()) {
162: IPluginModelBase model = (IPluginModelBase) iter.next();
163: fAllBundles.put(model.getPluginBase().getId(), model);
164: }
165:
166: if (!fAllBundles.containsKey("org.eclipse.osgi")) { //$NON-NLS-1$
167: // implicitly add it
168: IPluginModelBase model = PluginRegistry
169: .findModel("org.eclipse.osgi"); //$NON-NLS-1$
170: if (model != null) {
171: fModels.put(model, "default:default"); //$NON-NLS-1$
172: fAllBundles.put("org.eclipse.osgi", model); //$NON-NLS-1$
173: } else {
174: String message = PDEUIMessages.EquinoxLaunchConfiguration_oldTarget;
175: throw new CoreException(LauncherUtils
176: .createErrorStatus(message));
177: }
178: }
179: super .preLaunchCheck(configuration, launch, monitor);
180: }
181:
182: /*
183: * (non-Javadoc)
184: * @see org.eclipse.pde.ui.launcher.AbstractPDELaunchConfiguration#validatePluginDependencies(org.eclipse.debug.core.ILaunchConfiguration, org.eclipse.core.runtime.IProgressMonitor)
185: */
186: protected void validatePluginDependencies(
187: ILaunchConfiguration configuration, IProgressMonitor monitor)
188: throws CoreException {
189: OSGiValidationOperation op = new OSGiValidationOperation(
190: configuration);
191: LaunchPluginValidator.runValidationOperation(op, monitor);
192: }
193:
194: /**
195: * Clears the configuration area if the area exists and that option is selected.
196: *
197: * @param configuration
198: * the launch configuration
199: * @param monitor
200: * the progress monitor
201: * @throws CoreException
202: * if unable to retrieve launch attribute values
203: * @since 3.3
204: */
205: protected void clear(ILaunchConfiguration configuration,
206: IProgressMonitor monitor) throws CoreException {
207: // clear config area, if necessary
208: if (configuration.getAttribute(
209: IPDELauncherConstants.CONFIG_CLEAR_AREA, false))
210: CoreUtility.deleteContent(getConfigDir(configuration));
211: }
212:
213: }
|