Skip to main content
summaryrefslogtreecommitdiffstats
blob: ff489695d0d73594ab7a4ab59717733dd0d38412 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**********************************************************************
 * This file is part of "Object Teams Development Tooling"-Software
 * 
 * Copyright 2004, 2006 Fraunhofer Gesellschaft, Munich, Germany,
 * for its Fraunhofer Institute for Computer Architecture and Software
 * Technology (FIRST), Berlin, Germany and Technical University Berlin,
 * Germany.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * $Id$
 * 
 * Please visit http://www.eclipse.org/objectteams for updates and contact.
 * 
 * Contributors:
 * Fraunhofer FIRST - Initial API and implementation
 * Technical University Berlin - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otdt.debug;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.VMRunnerConfiguration;
import org.eclipse.objectteams.otdt.core.ext.OTDTPlugin;
import org.eclipse.objectteams.otdt.core.ext.OTREContainer;
import org.eclipse.objectteams.otdt.debug.internal.TempFileManager;


/**
 * @author gis
 * @version $Id: OTLaunchConfigurationDelegate.java 11843 2006-05-09 10:36:01Z carp $
 */
@SuppressWarnings("nls")
public class OTVMRunnerAdaptor
{
	private static final String OT_DEBUG_VMARG = "-Dot.debug";
	private static final String OT_DEBUG_CALLIN_STEPPING_VMARG = "-Dot.debug.callin.stepping";
	private static final String OT_TEAMCONFIG_VMARG = "-Dot.teamconfig";
	private static List<String> JPLIS_VMARGS;
	static {
		JPLIS_VMARGS = new ArrayList<String>();
		JPLIS_VMARGS.add("-Dot.otdt");
		JPLIS_VMARGS.add("-javaagent:" + "\""+OTREContainer.OTRE_AGENT_JAR_PATH.toOSString()+'"'); // support blanks in path
	}

	final static private boolean _useJPLIS = true;
	private ILaunchConfiguration _launchConfig;
	private String _mode;
	private ILaunch _launch;

	public void setAdaptationArgs(ILaunchConfiguration configuration, String mode, ILaunch launch) throws CoreException
	{
		_launchConfig = configuration;
		_mode = mode;
		_launch = launch;
	}
	
	public boolean usesJPLIS() {
		return _useJPLIS;
	}
	
	public VMRunnerConfiguration adapt(VMRunnerConfiguration vmRunnerConfig) throws CoreException
	{
		adaptVMArgs(vmRunnerConfig);
		
		return vmRunnerConfig;
	}

	protected void cloneRunner(VMRunnerConfiguration vmRunnerConfig, VMRunnerConfiguration newConfig)
	{
		// Only main type and class path have to be set in the constructor already
    	newConfig.setBootClassPath(vmRunnerConfig.getBootClassPath());
    	newConfig.setEnvironment(vmRunnerConfig.getEnvironment());
    	newConfig.setProgramArguments(vmRunnerConfig.getProgramArguments());
    	newConfig.setResumeOnStartup(vmRunnerConfig.isResumeOnStartup());
    	newConfig.setVMArguments(vmRunnerConfig.getVMArguments());
    	newConfig.setVMSpecificAttributesMap(vmRunnerConfig.getVMSpecificAttributesMap());
    	newConfig.setWorkingDirectory(vmRunnerConfig.getWorkingDirectory());
	}

	protected void adaptVMArgs(VMRunnerConfiguration newConfig) throws CoreException
    {
	    List<String> otVMArgs = getOTVMArgs();
	    String teamConfigArg = generateTeamConfigArgument(_launch, _launchConfig);
	    if (teamConfigArg != null)
	    	otVMArgs.add(teamConfigArg);
	    if (ILaunchManager.DEBUG_MODE.equals(_mode)) 
	    {
	    	otVMArgs.add(OT_DEBUG_VMARG);
	    	
	    	String callinSteppingVMArg = getCallinSteppingVMArg();
	    	if (callinSteppingVMArg != null)
	    		otVMArgs.add(callinSteppingVMArg);
	    }

	    String[] vmArgs = newConfig.getVMArguments();
	    if (vmArgs.length > 0)
	    {
	    	for (int i = 0; i < vmArgs.length; i++)
				otVMArgs.add(vmArgs[i]);
	    }
	    
	    String[] newArgs = new String[otVMArgs.size()];
	    
	    // new launches need to enclose file paths in quotes (cmdline is one string parsed by ArgumentParser),
	    // which are removed here (cmdline is array of strings):
	    for (int i = 0; i<newArgs.length; i++) {
	    	newArgs[i] = otVMArgs.get(i).replaceAll("\"", "");
	    }
	    newConfig.setVMArguments(newArgs);
    }
	
	/**
	 * API version: given a list of vm arguments add ot-specific vm arguments.
	 * 
	 * @param vmArguments list to be augmented.
	 * @throws CoreException
	 */
	public void adaptVMArguments(List vmArguments) throws CoreException 
	{
	    vmArguments.addAll(getOTVMArgs());
	    String teamConfigArg = generateTeamConfigArgument(_launch, _launchConfig);
	    if (teamConfigArg != null)
	    	vmArguments.add(teamConfigArg);
	    if (ILaunchManager.DEBUG_MODE.equals(_mode))
	    	vmArguments.add(OT_DEBUG_VMARG);
	}

	public String adaptVMArgumentString(String vmArguments) throws CoreException 
	{
		String sep = " ";
	    StringBuffer result = new StringBuffer(vmArguments);
	    
	    if (_useJPLIS)
			for (String arg : JPLIS_VMARGS)
				result.append(sep).append(arg);
	    
	    String callinSteppingVMArg = getCallinSteppingVMArg();
	    if (callinSteppingVMArg != null)
	    	result.append(sep).append(callinSteppingVMArg);
	    
	    String teamConfigArg = generateTeamConfigArgument(_launch, _launchConfig);
	    if (teamConfigArg != null)
	    	result.append(sep).append(teamConfigArg);
	    if (ILaunchManager.DEBUG_MODE.equals(_mode))
	    	result.append(sep).append(OT_DEBUG_VMARG);
	    
	    return result.toString();
	}

	private String getCallinSteppingVMArg() {
		String value = OTDebugPlugin.getDefault().getCallinSteppingConfig();
		if (value == null) return null;
		return OT_DEBUG_CALLIN_STEPPING_VMARG+'='+value;
	}

	protected List<String> getOTVMArgs()
	{
        return new ArrayList<String>(JPLIS_VMARGS);
	}
	
    /**
	 * Teams to activate are read from ILaunchConfiguration configuration.
	 * The list of team names are written to temporary file.
	 * The commandline par -Dot.teamconfig=filename is returned and can
	 * be added to the vmArgs variable. 
     * @param launch
	 * @param configuration	The launch configuration object.
	 * @return 
	 */
	private String generateTeamConfigArgument(ILaunch launch, ILaunchConfiguration configuration) throws CoreException
	{
		List<String> teamList = getTeamConfigList(configuration);
		if (teamList.isEmpty())
			return null;

		// write the teams into a temporary file:
        File teamFile = createTeamConfigFile(launch, teamList);
        
		return OT_TEAMCONFIG_VMARG+"=\"" + teamFile.getAbsolutePath()+'"';
	}

	private File createTeamConfigFile(ILaunch launch, List<String> teamList) throws CoreException
    {
        try {
            TempFileManager manager = OTDebugPlugin.getDefault().getTempFileManager();
            File tempFile = manager.createTempFile(launch, ".otteamconfig", ".conf");

		    PrintStream printStream = new PrintStream(new FileOutputStream(tempFile));
        	for (Iterator<String> iter = teamList.iterator(); iter.hasNext();) {
        		String element = iter.next();
				printStream.println(element);
			}
        	printStream.close();
        	return tempFile;
        } catch (Exception e) {
            IStatus status = OTDebugPlugin.createErrorStatus("Error writing static teams configuration", e);
        	throw new CoreException(status);
        }
    }

    /**
	 * Returns the list of teams which will be woven into the launched application 
	 * specified by the given launch configuration, as a list of strings. 
	 * The returned list is empty if no teams are are specified or if the teams
	 * are deactivated in the configuratio tab.
	 * 
	 * @param configuration
	 *            launch configuration
	 * @return the list of teams from the team configuration specified by the given launch
	 *         configuration, possibly an empty list
	 * @exception CoreException
	 *                if unable to retrieve the attribute
	 */
	private List<String> getTeamConfigList(ILaunchConfiguration configuration)
			throws CoreException 
	{
		boolean teamsActive = configuration.getAttribute(IOTLaunchConstants.ATTR_TEAMCONFIG_ACTIVE, true);
		if (!teamsActive)
			return new LinkedList<String>();
		
		List teamHandles = configuration.getAttribute(IOTLaunchConstants.ATTR_TEAMCONFIG_LIST, new LinkedList());	
		List<String> teamNames = new LinkedList<String>();
		List<String> badHandles = new LinkedList<String>();
		
		for (Iterator iter = teamHandles.iterator(); iter.hasNext();)
        {
            String teamHandle = (String) iter.next();
            IType teamType = (IType) JavaCore.create(teamHandle);
            if (teamType != null && teamType.exists())
                teamNames.add(teamType.getFullyQualifiedName());
            else
                badHandles.add(teamHandle);
        }
		
		if (!badHandles.isEmpty())
		{
		    IStatus status = OTDebugPlugin.createErrorStatus("Cannot determine types: " + badHandles.toString());
		    throw new CoreException(status);
		}
		
    	return teamNames;
    }
}

Back to the top