Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a68112161aac2ce701815b873e16ea674d49ccbc (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *    
 * 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
 *
 * Contributors:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.moka.launch;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.HashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.moka.Activator;
import org.eclipse.papyrus.moka.MokaConstants;
import org.eclipse.papyrus.moka.debug.MokaDebugTarget;
import org.eclipse.papyrus.moka.debug.MokaProcess;
import org.eclipse.papyrus.moka.engine.IExecutionEngine;
import org.eclipse.papyrus.moka.engine.MokaExecutionEngineJob;
import org.eclipse.ui.IEditorPart;


/**
 * An implementation of ILaunchConfigurationDelegate
 *
 */
public class MokaLaunchDelegate extends LaunchConfigurationDelegate implements ILaunchConfigurationDelegate {

	/**
	 * The attribute name for the resource uri associated with a launch configuration
	 * The corresponding resource contains the EObject to be executed
	 */
	public static String URI_ATTRIBUTE_NAME = "URI_ATTRIBUTE" ;

	/**
	 * The attribute name for the uri fragment associated with a launch configuration
	 * This fragment is an id for the EObject to be executed 
	 */
	public static String FRAGMENT_ATTRIBUTE_NAME = "FRAGMENT_ATTRIBUTE" ;

	/**
	 * The attribute name for the arguments associated with a launch configuration
	 * This arguments are given to the execution engine for initialization, before actually starting execution.
	 */
	public static String ARGS_ATTRIBUTE_NAME = "ARGS_ATTRIBUTE" ;

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {

		// instantiates the actual execution engine
		IExecutionEngine engine = this.instantiateExecutionEngine() ;
		if (engine == null)
			this.abort("Could not instantiate execution engine", null) ;

		// Create a job for the execution of this engine
		MokaExecutionEngineJob engineJob = new MokaExecutionEngineJob("Execution Engine Job", engine) ;

		// retrieves values for the various attributes associated with the launch configuration
		String resourceURI = configuration.getAttribute(URI_ATTRIBUTE_NAME, "") ;
		String uriFragment = configuration.getAttribute(FRAGMENT_ATTRIBUTE_NAME, "") ;
		String arguments = configuration.getAttribute(ARGS_ATTRIBUTE_NAME, "") ;
		String[] args = arguments.split("\\s+");

		IEditorPart part = EditorUtils.getEditorPart(resourceURI) ;

		//IEditorPart part = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor() ;
		ServicesRegistry servicesRegistry =  (ServicesRegistry)part.getAdapter(ServicesRegistry.class);
		ResourceSet resourceSet = null ;
		try {
			resourceSet = servicesRegistry.getService(ModelSet.class) ;
		} catch (ServiceException e1) {
			resourceSet = new ResourceSetImpl() ;
			e1.printStackTrace();
		}

		// from the arguments, retrieves the EObject to be executed

		//ResourceSet resourceSet = new ResourceSetImpl() ;
		Resource resource = resourceSet.getResource(URI.createURI(resourceURI), true) ;
		final EObject eObjectToExecute = resource.getEObject(uriFragment) ;

		// Gets port addresses for sockets
		int requestPort = -1;
		int replyPort = -1;
		int eventPort = -1;
		requestPort = findFreePort();
		eventPort = findFreePort();
		replyPort = findFreePort();
		if (requestPort == -1 || replyPort == -1 || eventPort == -1) {
			this.abort("Unable to find free port", null);
		}

		// The resulting job is used for the creation of MokaRuntimeProcess, thereby simulating a real, physical process
		IProcess process = new MokaProcess(launch, engineJob, "Moka runtime process", new HashMap<String,String>()) ;

		// Initializes the engine as well as the debug target
		MokaDebugTarget target = new MokaDebugTarget(launch, process);
		try {
			engine.init(eObjectToExecute, args, target, requestPort, replyPort, eventPort) ;
			target.connect(requestPort, replyPort, eventPort) ;
			launch.addDebugTarget(target);
			engineJob.setDebugTarget(target) ;
			engineJob.schedule() ;
		} catch (UnknownHostException e) {
			org.eclipse.papyrus.infra.core.Activator.log.equals(e) ;
		} catch (IOException e) {
			org.eclipse.papyrus.infra.core.Activator.log.equals(e) ;
		}
			
	}

	/**
	 * A convenience method for instantiating the actual execution engine.
	 * The class to be instantiated is determined in the Moka preference page (see MokaConstants.DEFAULT_EXECUTION_ENGINE)
	 * Any engine contributing to MokaConstants.ENGINE_EXTENSION_POINT_ID can be selected in this preference page.
	 * 
	 * @return
	 */
	protected IExecutionEngine instantiateExecutionEngine() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IConfigurationElement[] config = registry.getConfigurationElementsFor(MokaConstants.MOKA_ENGINE_EXTENSION_POINT_ID);
		try {
			IConfigurationElement e = null;
			String selectedExecutionEngine = "" + Activator.getDefault().getPreferenceStore().getString(MokaConstants.MOKA_DEFAULT_EXECUTION_ENGINE_PREF);
			for(int i = 0; i < config.length; i++) {
				if(config[i].getNamespaceIdentifier().equals(selectedExecutionEngine))
					e = config[i];
			}
			final Object o = e.createExecutableExtension("class");
			return (IExecutionEngine)o ;
		} catch (CoreException ex) {
			org.eclipse.papyrus.infra.core.Activator.log.equals(ex) ;
		} catch (Exception ex) {
			org.eclipse.papyrus.infra.core.Activator.log.equals(ex) ;
		}
		// If null is returned, the calling method (launch) fires a CoreException
		return null ; 
	}

	/**
	 * Returns a free port number on localhost, or -1 if unable to find a free port.
	 * This code has been duplicated from the PDAExample. See http://www.eclipse.org/articles/Article-Debugger/how-to.html
	 * 
	 * @return a free port number on localhost, or -1 if unable to find a free port
	 */
	protected static int findFreePort() {
		ServerSocket socket= null;
		try {
			socket= new ServerSocket(0);
			return socket.getLocalPort();
		} catch (IOException e) { 
		} finally {
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
				}
			}
		}
		return -1;		
	}

	/**
	 * A convenience method for aborting launching.
	 * This code has been duplicated from the PDAExample. See http://www.eclipse.org/articles/Article-Debugger/how-to.html
	 * 
	 * @param message 
	 * @param e
	 * @throws CoreException
	 */
	protected void abort(String message, Throwable e) throws CoreException {
		throw new CoreException(new Status(IStatus.ERROR, MokaConstants.MOKA_DEBUG_MODEL_ID, 0, message, e));
	}

}

Back to the top