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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.sca.core.launch;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.Socket;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
/**
* A launch shortcut for SCA .composite files.
*
* @version $Rev$ $Date$
*/
public class TuscanyLaunchShortcut implements ILaunchShortcut {
private final static String TUSCANY_SCA_DOMAIN_PROJECT = "tuscany-sca-domain";
public void launch(final ISelection selection, final String mode) {
try {
// Make sure we have a .composite file selected
if (!(selection instanceof IStructuredSelection)) {
return;
}
Object[] selections = ((IStructuredSelection)selection).toArray();
if (selections.length == 0) {
return;
}
final IFile file = (IFile)selections[0];
if (!file.getFileExtension().equals("composite")) {
return;
}
// Run with a progress monitor
//PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, new IRunnableWithProgress() {
PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException {
try {
progressMonitor.beginTask("Starting SCA Composite", 100);
// Get our launch configuration type
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType launchConfigurationType =launchManager.getLaunchConfigurationType(
"org.apache.tuscany.sca.core.launch.configurationtype");
progressMonitor.worked(10);
if (progressMonitor.isCanceled()) {
return;
}
// If the SCA domain controller is not running yet, launch it
if (!isDomainManagerRunning()) {
launchDomainManager(mode, file, launchManager, launchConfigurationType, progressMonitor);
if (progressMonitor.isCanceled()) {
return;
}
if (!waitForDomainManager(progressMonitor)) {
throw new RuntimeException("SCA Domain Manager could not be started.");
}
}
if (progressMonitor.isCanceled()) {
return;
}
progressMonitor.worked(50);
// Launch an SCA node
launchNode(mode, file, launchManager, launchConfigurationType, progressMonitor);
progressMonitor.done();
} catch (Exception e) {
throw new InvocationTargetException(e);
} finally {
progressMonitor.done();
}
}
});
} catch (Exception e) {
Platform.getLog(
Platform.getBundle("org.apache.tuscany.sca.core")).log(
new Status(IStatus.ERROR, "org.apache.tuscany.sca.core", "Could not launch SCA composite", e));
}
}
public void launch(IEditorPart editor, String mode) {
//TODO later...
}
/**
* Launch an SCA node.
*
* @param mode
* @param file
* @param launchManager
* @param launchConfigurationType
* @throws CoreException
* @throws JavaModelException
*/
private void launchNode(String mode,
IFile file,
ILaunchManager launchManager,
ILaunchConfigurationType launchConfigurationType,
IProgressMonitor progressMonitor) throws CoreException, JavaModelException, IOException {
progressMonitor.subTask("Starting SCA node");
if (progressMonitor.isCanceled()) {
return;
}
// Get the Java project
IJavaProject javaProject = JavaCore.create(file.getProject());
// Get the contribution location and URI
String contributionLocation = contributionLocation(javaProject);
String contributionURI = contributionURI(javaProject);
// Determine the composite file URI
String compositeURI = compositeURI(javaProject, file);
// Configure the node
String nodeName = configureNode(contributionURI, contributionLocation, compositeURI, progressMonitor);
// Create a launch configuration
ILaunchConfigurationWorkingCopy configuration = launchConfigurationType.newInstance(null,
launchManager.generateUniqueLaunchConfigurationNameFrom(file.getFullPath().removeFileExtension().lastSegment()));
// Set the project and type to launch
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "org.apache.tuscany.sca.node.launcher.NodeLauncher");
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, file.getProject().getName());
// Pass the URL of the node install image to the launcher
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
"http://localhost:9990/node-image/" + nodeName);
// Save the configuration
configuration.doSave();
// Launch
configuration.launch(mode, null);
}
/**
* Launch the SCA domain manager.
*
* @param mode
* @param launchManager
* @param launchConfigurationType
* @throws CoreException
* @throws JavaModelException
*/
private void launchDomainManager(String mode,
IFile file,
ILaunchManager launchManager,
ILaunchConfigurationType launchConfigurationType,
IProgressMonitor progressMonitor) throws CoreException, JavaModelException {
progressMonitor.subTask("Starting SCA domain manager");
if (progressMonitor.isCanceled()) {
return;
}
// Get the SCA domain project
IProject domainProject = domainProject(progressMonitor);
// Create a launch configuration
ILaunchConfigurationWorkingCopy configuration = launchConfigurationType.newInstance(null,
launchManager.generateUniqueLaunchConfigurationNameFrom("Apache Tuscany SCA Domain Manager"));
// Set the project and type to launch
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, file.getProject().getName());
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "org.apache.tuscany.sca.node.launcher.DomainManagerLauncher");
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, domainProject.getLocation().toString());
// Save the configuration
configuration.doSave();
// Launch
configuration.launch(mode, null);
}
private static final String PING_HEADER =
"GET /ping HTTP/1.0\n" + "Host: localhost\n"
+ "Content-Type: text/xml\n"
+ "Connection: close\n"
+ "Content-Length: ";
private static final String PING_CONTENT = "";
private static final String PING =
PING_HEADER + PING_CONTENT.getBytes().length + "\n\n" + PING_CONTENT;
/**
* Returns true if the SCA domain controller is running.
*
* @return
*/
private static boolean isDomainManagerRunning() {
try {
Socket client = new Socket("localhost", 9990);
OutputStream os = client.getOutputStream();
os.write(PING.getBytes());
os.flush();
String response = read(client);
if (response.indexOf("<span id=\"ping\">") != -1) {
return true;
} else {
return false;
}
} catch (IOException e) {
return false;
}
}
/**
* Wait for domain to be running.
*
* @return
*/
private static boolean waitForDomainManager(IProgressMonitor progressMonitor) throws InterruptedException {
progressMonitor.subTask("Contacting SCA domain manager");
for (int i = 0; i < 40; i++) {
if (progressMonitor.isCanceled()) {
return false;
}
if (isDomainManagerRunning()) {
return true;
}
Thread.sleep(500);
}
return false;
}
/**
* Read a String from a socket.
*
* @param socket
* @return
* @throws IOException
*/
private static String read(Socket socket) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuffer sb = new StringBuffer();
String str;
while ((str = reader.readLine()) != null) {
sb.append(str);
}
return sb.toString();
} finally {
if (reader != null) {
reader.close();
}
}
}
/**
* Returns the location of the SCA contribution representing a Java project.
* @param javaProject
* @return
* @throws MalformedURLException
* @throws JavaModelException
*/
private static String contributionLocation(IJavaProject javaProject) throws MalformedURLException, JavaModelException {
IPath location = javaProject.getOutputLocation();
IResource resource;
if (location.segmentCount() == 1) {
resource = javaProject.getProject();
} else {
resource = javaProject.getProject().getWorkspace().getRoot().getFolder(location);
}
location = resource.getLocation();
String url = location.toFile().toURI().toURL().toString();
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
return url;
}
/**
* Returns the URI of the SCA contribution representing a Java project.
* @param javaProject
* @return
*/
private static String contributionURI(IJavaProject javaProject) {
return javaProject.getProject().getName();
}
/**
* Returns the SCA artifact URI of a composite file inside a Java project.
*
* @param javaProject
* @param file
* @return
* @throws JavaModelException
*/
private static String compositeURI(IJavaProject javaProject, IFile file) throws JavaModelException {
// Find the Java source container containing the specified file
IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
int sourceFolderSegments = 0;
for (IClasspathEntry entry : classpathEntries) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
sourceFolderSegments = entry.getPath().matchingFirstSegments(file.getFullPath());
if (sourceFolderSegments > 0)
break;
}
}
// Determine the composite URI
String compositeURI = file.getFullPath().removeFirstSegments(sourceFolderSegments).toString();
return compositeURI;
}
private static final String START_HEADER1 =
"GET /quickstart?";
private static final String START_HEADER2 =
" HTTP/1.0\n" + "Host: localhost\n"
+ "Content-Type: text/xml\n"
+ "Connection: close\n"
+ "Content-Length: ";
private static final String START_CONTENT = "";
/**
* Send a request to the SCA domain manager to configure an SCA node for
* the specified composite.
*
* @param contributionURI
* @param contributionLocation
* @param compositeURI
* @return
* @throws IOException
*/
private static String configureNode(String contributionURI, String contributionLocation, String compositeURI,
IProgressMonitor progressMonitor) throws IOException, CoreException {
progressMonitor.subTask("Configuring node");
// Send the request to configure the node
Socket client = new Socket("localhost", 9990);
OutputStream os = client.getOutputStream();
String request = START_HEADER1 +
"contribution=" + contributionURI + "&location=" + contributionLocation + "&composite=" + compositeURI +
START_HEADER2 + START_CONTENT.getBytes().length + "\n\n" + START_CONTENT;
os.write(request.getBytes());
os.flush();
String response = read(client);
// Refresh the domain project
domainProject(progressMonitor).refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
int i = response.indexOf("<span id=\"node\">");
if (i != -1) {
// Extract the node name
response = response.substring(i + 16);
i = response.indexOf("</span>");
String nodeName = response.substring(0, i);
return nodeName;
} else {
throw new RuntimeException("Node could not be configured: " + response);
}
}
/**
* Returns the SCA domain project.
*
* @return
* @throws CoreException
*/
private static IProject domainProject(IProgressMonitor progressMonitor) throws CoreException {
IProject domainProject = ResourcesPlugin.getWorkspace().getRoot().getProject(TUSCANY_SCA_DOMAIN_PROJECT);
if (progressMonitor.isCanceled()) {
return domainProject;
}
if (!domainProject.exists()) {
progressMonitor.subTask("Creating SCA domain resources");
domainProject.create(new SubProgressMonitor(progressMonitor, 5));
domainProject.open(new SubProgressMonitor(progressMonitor, 5));
String html = "<html>\n" +
"<head>\n" +
"<meta http-equiv=\"refresh\" content=\"0;url=http://localhost:9990/ui/home\">\n" +
"</head>\n" +
"<body>\n" +
"<a href=\"http://localhost:9990/ui/home\">SCA Domain</a>\n" +
"</body>\n" +
"</html>";
IFile file = domainProject.getFile(new Path("domain.html"));
file.create(new ByteArrayInputStream(html.getBytes()), true, new SubProgressMonitor(progressMonitor, 5));
}
return domainProject;
}
}
|