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.internal.ui.launcher;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014: import java.util.Properties;
015: import java.util.StringTokenizer;
016:
017: import org.eclipse.core.runtime.Preferences;
018: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
019: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
020: import org.eclipse.pde.internal.core.ICoreConstants;
021: import org.eclipse.pde.internal.core.PDECore;
022: import org.eclipse.pde.internal.core.TargetPlatformHelper;
023: import org.eclipse.pde.internal.ui.IPDEUIConstants;
024: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
025: import org.eclipse.pde.ui.launcher.OSGiLaunchConfigurationInitializer;
026:
027: public class EquinoxInitializer extends
028: OSGiLaunchConfigurationInitializer {
029:
030: private Map fStartLevels;
031:
032: public void initialize(ILaunchConfigurationWorkingCopy configuration) {
033: super .initialize(configuration);
034: initializeProgramArguments(configuration);
035: initializeVMArguments(configuration);
036: initializeTracing(configuration);
037: }
038:
039: private void initializeProgramArguments(
040: ILaunchConfigurationWorkingCopy configuration) {
041: StringBuffer buffer = new StringBuffer(LaunchArgumentsHelper
042: .getInitialProgramArguments());
043: if (buffer.length() > 0) {
044: if (buffer.indexOf("-console") == -1) { //$NON-NLS-1$
045: buffer.append(" -console"); //$NON-NLS-1$
046: }
047: } else {
048: buffer.append("-console"); //$NON-NLS-1$
049: }
050: configuration.setAttribute(
051: IPDEUIConstants.APPEND_ARGS_EXPLICITLY, true);
052: configuration
053: .setAttribute(
054: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
055: buffer.toString()); //$NON-NLS-1$
056: }
057:
058: private void initializeVMArguments(
059: ILaunchConfigurationWorkingCopy configuration) {
060: configuration.setAttribute(
061: IPDEUIConstants.LAUNCHER_PDE_VERSION, "3.3"); //$NON-NLS-1$
062: Preferences preferences = PDECore.getDefault()
063: .getPluginPreferences();
064: StringBuffer vmArgs = new StringBuffer(preferences
065: .getString(ICoreConstants.VM_ARGS));
066: if (vmArgs.indexOf("-Declipse.ignoreApp") == -1) { //$NON-NLS-1$
067: if (vmArgs.length() > 0)
068: vmArgs.append(" "); //$NON-NLS-1$
069: vmArgs.append("-Declipse.ignoreApp=true"); //$NON-NLS-1$
070: }
071: if (vmArgs.indexOf("-Dosgi.noShutdown") == -1) { //$NON-NLS-1$
072: vmArgs.append(" -Dosgi.noShutdown=true"); //$NON-NLS-1$
073: }
074: configuration.setAttribute(
075: IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
076: vmArgs.toString());
077: }
078:
079: private void initializeTracing(
080: ILaunchConfigurationWorkingCopy configuration) {
081: configuration.setAttribute(
082: IPDELauncherConstants.TRACING_CHECKED,
083: IPDELauncherConstants.TRACING_NONE);
084: }
085:
086: protected void initializeBundleState(
087: ILaunchConfigurationWorkingCopy configuration) {
088: initializeBundleState();
089: super .initializeBundleState(configuration);
090: }
091:
092: protected String getAutoStart(String bundleID) {
093: if (fStartLevels.containsKey(bundleID)) {
094: String value = fStartLevels.get(bundleID).toString();
095: return value.substring(value.indexOf(":") + 1); //$NON-NLS-1$
096: }
097: return super .getAutoStart(bundleID);
098: }
099:
100: protected String getStartLevel(String bundleID) {
101: if (fStartLevels.containsKey(bundleID)) {
102: String value = fStartLevels.get(bundleID).toString();
103: return value.substring(0, value.indexOf(":")); //$NON-NLS-1$
104: }
105: return super .getStartLevel(bundleID);
106: }
107:
108: private void initializeBundleState() {
109: if (fStartLevels == null)
110: fStartLevels = new HashMap();
111: Properties props = TargetPlatformHelper
112: .getConfigIniProperties();
113: if (props != null) {
114: String value = (String) props.get("osgi.bundles"); //$NON-NLS-1$
115: if (value != null) {
116: StringTokenizer tokenizer = new StringTokenizer(value,
117: ","); //$NON-NLS-1$
118: while (tokenizer.hasMoreTokens()) {
119: String tokenValue = tokenizer.nextToken();
120: int index = tokenValue.indexOf("@"); //$NON-NLS-1$
121: if (index > 0) {
122: String bundle = tokenValue.substring(0, index)
123: .trim();
124: fStartLevels.put(bundle,
125: getStartValue(tokenValue
126: .substring(index)));
127: }
128: }
129: }
130: }
131: }
132:
133: private String getStartValue(String value) {
134: StringBuffer buffer = new StringBuffer(value);
135: StringBuffer result = new StringBuffer(":"); //$NON-NLS-1$
136:
137: int index = value.indexOf("start"); //$NON-NLS-1$
138: result.append(Boolean.toString(index != -1));
139:
140: if (index != -1)
141: buffer.delete(index, index + 5);
142:
143: int colon = value.indexOf(':');
144: if (colon != -1)
145: buffer.deleteCharAt(colon);
146:
147: // delete the first char '@'
148: buffer.deleteCharAt(0);
149:
150: try {
151: result
152: .insert(0, Integer.parseInt(buffer.toString()
153: .trim()));
154: } catch (NumberFormatException e) {
155: result.insert(0, DEFAULT);
156: }
157: return result.toString();
158: }
159:
160: }
|