-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava_api_examples.htm
515 lines (456 loc) · 18.6 KB
/
java_api_examples.htm
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>Java Client SDK Examples</title>
<link rel="stylesheet" href="/help/docs/ppilot.css" type="text/css">
</head>
<body>
<h1>Java Client SDK Examples</h1>
<p>Java is a popular programming language for running distributed applications via
web services. This worked example illustrates how to run a
protocol from a Java program. The specific example shows you how to run a protocol
synchronously—control returns to the example program only when the
protocol has run to completion. An asynchronous example is
included at the end. </p>
<div class="tip">
<p><strong>Tips:</strong>
</p>
<ul>
<li>
The source code for these examples is available in the Java
Client SDK package "<install>/apps/scitegic/clientsdkjava/web/examples".
<li>
To work with these examples, you need to be proficient in Java programming and
understand the basic concepts of Web services.
<li>
In future releases, we may make enhancements to the SDK between
versions that could break applications that call it. When
you upgrade, review the release notes for details.
</li>
</ul>
</div>
<h2>Overview</h2>
<p>The worked example covers the following steps.</p>
<ul>
<li>
Importing Java libraries
<li>
Connecting to the Pipeline Pilot server
<li>
Checking the server configuration
<li>
Creating a protocol job
<li>
Adding parameters
<li>
Running the job
<li>
Processing the results
<li>
Deleting the job data from the server
<li>
Compiling and running the example</li>
</ul>
<h2>Importing Java Libraries</h2>
<p>All SciTegic classes needed are in the com.scitegic.proxy package. You can import the
entire package or just the classes you need.
In addition, the following example uses the standard Java File class:</p>
<pre class="syntax">
import com.scitegic.proxy.*;
import java.io.File;
</pre>
<h2>Connecting to the Server</h2>
<p>A Pipeline Pilot server URL, username, and password are necessary to connect to
the server. The format of the server URL is "http://<yourhost.yourdomain.com>:9944".
For a secure connection, use HTTPS and port number 9943. Change the
URL to the appropriate value for your local configuration. The following
example shows getting these values from the command line:</p>
<pre class="syntax">
public static void main(String[] args) {
// Pick up the server URL, username, and password from the command line
if (args.length < 3) {
System.out.println("Usage: java com.scitegic.proxy.examples.SimpleSynchronousRun <server_url> <username> <password>");
return;
}
String server = args[0];
String user = args[1];
String password = args[2];
</pre>
<p>The <code>PipelinePilotServer</code> constructor takes the server, username, and
password as arguments. Create the <code>PipelinePilotServer</code> and <code>Job</code> objects outside
the try block to allow clean up from an <code>Exception</code>:</p>
<pre class="syntax">
PipelinePilotServer pp = null;
Job protocol = null;
try {
pp = new PipelinePilotServer(server, user, password);
</pre>
<h2>Checking the Server Configuration</h2>
<p>To get the server configuration use getServerConfig():</p>
<pre class="syntax">
// Print server configuration settings.
PipelinePilotServerConfig conf = pp.getServerConfig();
System.out.println("Printing server configuration settings");
System.out.println(" Local root = " + conf.getLocalRoot());
System.out.println(" Local temp root = " + conf.getLocalTempRoot());
System.out.println(" File Web Service Endpoint = " + conf.getSciTegicFileEndPoint());
System.out.println(" Server version = " + conf.getServerVersion());
System.out.println(" Authentication on = " + conf.isAuthenticationOn());
System.out.println();
System.out.println("Printing SciTegic Root of server");
String root = pp.getRemoteFileManager().getSciTegicRoot();
System.out.println(" SciTegic Root = " + root);
System.out.println();
</pre>
<h2>Checking the Protocol Database</h2>
<p>Now that the server connection exists, components and protocols are accessible. To get to the
component database, use getComponentDatabase(). Here, a list of example protocols is
printed:</p>
<pre class="syntax">
ComponentDatabase compdb = pp.getComponentDatabase();
// Print contents of 'Protocols/Examples'
System.out.println("Printing folder names under " + EXAMPLE_PROTOCOLS);
String[] list = compdb.getFolderNamesInFolder(EXAMPLE_PROTOCOLS);
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
</pre>
<p>Here, a tree of protocols is fetched and printed (see the full source code below for the
helper method that prints the tree):</p>
<pre class="syntax">
// Print out folder hierarchy under 'Protocols/Web Services/Web Port Examples'.
System.out.println("Printing folder tree under " + WEB_PORT_EXAMPLE_PROTOCOLS
+ " (omitting Utilities directories)");
XmldbItem rootFolder = compdb.getXmldbContentsRecursive(WEB_PORT_EXAMPLE_PROTOCOLS);
printFolderTreeRecursive(rootFolder, 0);
</pre>
<h2>Creating a Protocol Job</h2>
<p>Creating the protocol job is straightforward. In the following example,
the protocol name was set in a class variable:</p>
<pre class="syntax">
static final String PROTOCOL = "Protocols/Web Services/Web Port Examples/Generic/XY Scatter Plot Utility";
protocol = pp.createJob(PROTOCOL);
</pre>
<h2>Adding Parameters</h2>
<p>The XY Scatter Plot Utility takes a Source, an X Property, a Y Property,
a Tooltip and a File Type. To ensure that all
parameter are set and the protocol is ready to run, use <code>validate()</code>:</p>
<pre class="syntax">
boolean uploadFromClient = true;
if (uploadFromClient) {
// Use setInputFileOnClient() to upload a local file to the server.
File localFile = new File("./data/imports-85.txt");
protocol.setInputFileOnClient("Source", localFile);
} else {
// Alternatively, read a file on the server.
protocol.setInputValue("Source", "data/Tables/imports-85.txt");
}
protocol.setInputValue("X Property", "Highwaympg");
protocol.setInputValue("Y Property", "Horsepower");
protocol.setInputValue("Tooltip",
"'Make = ' . (make) . ', $(X Property) = ' . ($(X Property)) . ', "
+ "$(Y Property) = ' . ($(Y Property))");
protocol.setInputValue("File Type", "PDF");
protocol.validate();
</pre>
<h2>Running the Job</h2>
<p>A single method call starts the job and polls for completion. Control returns
to the calling program when the job is complete: </p>
<pre class="syntax">
JobResult prr = protocol.runAndPoll();
</pre>
<h2>Processing the Results</h2>
<p>Once the protocol is finished, the results are immediately available. Use <code>getResultFiles()</code>:</p>
<pre class="syntax">
String[] results = prr.getResultFiles();
if (results.length > 0) {
// print all result files
System.out.println("Getting job results:");
for (int i = 0; i < results.length; i++) {
System.out.println(results[i]);
}
System.out.println();
// download, locally, the first result file
File localResultFile = new File("chart.pdf");
System.out.print("Writing result file to ");
System.out.println(localResultFile.getAbsolutePath());
System.out.println();
pp.getRemoteFileManager().downloadFile(results[0], localResultFile);
}
</pre>
<h2>Deleting the Job</h2>
<p>To avoid cluttering up the server with every job that runs, each set of
job data needs to be deleted after any useful results are extracted
or reviewed:</p>
<pre class="syntax">
// Remove the job from the server
System.out.println("Deleting job " + protocol.getJobId());
pp.releaseJob(protocol.getJobId());
</pre>
<h2>The Complete Example</h2>
<p>Here is the complete example:</p>
<pre class="syntax">
package com.scitegic.proxy.examples;
import java.io.File;
import com.scitegic.proxy.ComponentDatabase;
import com.scitegic.proxy.Job;
import com.scitegic.proxy.JobResult;
import com.scitegic.proxy.JobStatus;
import com.scitegic.proxy.PipelinePilotServer;
import com.scitegic.proxy.PipelinePilotServerConfig;
import com.scitegic.proxy.XmldbItem;
/**
* This sample Java client illustrates how to use the 'Java Client SDK for Server' to launch Protocol Jobs, Synchronously, on a Server
*/
public class SimpleSynchronousRun {
static final String EXAMPLE_PROTOCOLS = "Protocols/Examples";
static final String WEB_PORT_EXAMPLE_PROTOCOLS = "Protocols/Web Services/Web Port Examples";
static final String PROTOCOL = WEB_PORT_EXAMPLE_PROTOCOLS + "/Generic/XY Scatter Plot Utility";
static final String WHITESPACE = " ";
public SimpleSynchronousRun() {
}
private static void printFolderTreeRecursive(XmldbItem folder, int indent) {
if (folder.getName().toLowerCase().equals("utilities")) {
return;
}
System.out.println(WHITESPACE.substring(0, indent) + folder.getName());
indent += 2;
XmldbItem[] children = folder.getChildren();
for (int i = 0, m = children.length; i < m; i++) {
XmldbItem child = children[i];
if (child.isFolder()) {
printFolderTreeRecursive(child, indent);
} else {
System.out.println(WHITESPACE.substring(0, indent) + child.getName());
}
}
}
public static void main(String[] args) {
// Pick up the server URL, username, and password from the command line
if (args.length < 3) {
System.out.println("Usage: java com.scitegic.proxy.examples.SimpleSynchronousRun "
+ "<server_url> <username> <password>");
return;
}
String server = args[0];
String user = args[1];
String password = args[2];
PipelinePilotServer pp = null;
Job protocol = null;
try {
pp = new PipelinePilotServer(server, user, password);
// Print server configuration settings.
PipelinePilotServerConfig conf = pp.getServerConfig();
System.out.println("Printing server configuration settings");
System.out.println(" Local root = " + conf.getLocalRoot());
System.out.println(" Local temp root = " + conf.getLocalTempRoot());
System.out.println(" File Web Service Endpoint = " + conf.getSciTegicFileEndPoint());
System.out.println(" Server version = " + conf.getServerVersion());
System.out.println(" Authentication on = " + conf.isAuthenticationOn());
System.out.println();
System.out.println("Printing SciTegic Root of server");
String root = pp.getRemoteFileManager().getSciTegicRoot();
System.out.println(" SciTegic Root = " + root);
System.out.println();
ComponentDatabase compdb = pp.getComponentDatabase();
// Print contents of 'Protocols/Examples'
System.out.println("Printing folder names under " + EXAMPLE_PROTOCOLS);
String[] list = compdb.getFolderNamesInFolder(EXAMPLE_PROTOCOLS);
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
System.out.println();
// Print out folder hierarchy under 'Protocols/Web Services/Web Port Examples'.
System.out.println("Printing folder tree under " + WEB_PORT_EXAMPLE_PROTOCOLS
+ " (omitting Utilities directories)");
XmldbItem rootFolder = compdb.getXmldbContentsRecursive(WEB_PORT_EXAMPLE_PROTOCOLS);
printFolderTreeRecursive(rootFolder, 0);
System.out.println();
// Create job and add parameters for protocol run
protocol = pp.createJob(PROTOCOL);
boolean uploadFromClient = true;
if (uploadFromClient) {
// Use setInputFileOnClient() to upload a local file to the server.
File localFile = new File("./data/imports-85.txt");
protocol.setInputFileOnClient("Source", localFile);
} else {
// Alternatively, read a file on the server.
protocol.setInputValue("Source", "data/Tables/imports-85.txt");
}
protocol.setInputValue("X Property", "Highwaympg");
protocol.setInputValue("Y Property", "Horsepower");
protocol.setInputValue("Tooltip",
"'Make = ' . (make) . ', $(X Property) = ' . ($(X Property)) . ', "
+ "$(Y Property) = ' . ($(Y Property))");
protocol.setInputValue("File Type", "PDF");
protocol.validate();
// Run job and check results
System.out.println("Starting Protocol Job Synchronously");
System.out.println();
JobResult prr = protocol.runAndPoll();
JobStatus status = protocol.getStatus();
if (JobStatus.Finished.equals(status)) {
String[] results = prr.getResultFiles();
if (results.length > 0) {
// print all result files
System.out.println("Getting job results:");
for (int i = 0; i < results.length; i++) {
System.out.println(results[i]);
}
System.out.println();
// download, locally, the first result file
File localResultFile = new File("chart.pdf");
System.out.print("Writing result file to ");
System.out.println(localResultFile.getAbsolutePath());
System.out.println();
pp.getRemoteFileManager().downloadFile(results[0], localResultFile);
}
System.out.println("Job files will be deleted after you press <Enter>");
System.out.print(">>>>");
System.in.read();
} else {
System.out.println("Getting job errors results");
String[] error = protocol.getErrorMessages();
for (int i = 0; i < error.length; i++) {
System.out.println(error[i]);
}
}
// Remove the job from the server
System.out.println("Deleting job " + protocol.getJobId());
pp.releaseJob(protocol.getJobId());
} catch (Exception e) {
if (pp != null && protocol != null) {
System.out.println("Deleting job " + protocol.getJobId());
try {
pp.releaseJob(protocol.getJobId());
} catch (Exception ex) {
}
}
e.printStackTrace();
}
}
}
</pre>
<h2>Compiling and Running the Example</h2>
<p>Copy SimpleSynchronousRun.java to a local src directory.
Since the Java class is in the com.scitegic.proxy.examples package,
the full path for the local copy of the Java file should be "src/com/scitegic/proxy/examples/SimpleSynchronousRun.java".
Use the "run.bat" batch file to compile the example program. It
creates a class directory, sets the CLASSPATH, and
runs javac to compile the .java file and generate the
.class file. </p>
<p>The CLASSPATH needs to include the class
directory and the following JAR files: </p>
<p>jalpp.jar<br>
activation.jar<br>
jaxp-api.jar<br>
jax-qname.jar<br>
JaxRPC_PP_Stubs.jar<br>
jaxrpc-api.jar<br>
jaxrpc-impl.jar<br>
jaxrpc-spi.jar<br>
mail.jar<br>
relaxngDatatype.jar<br>
saaj-api.jar<br>
saaj-impl.jar<br>
xerces.jar<br>
xsdlib.jar</p>
<pre class="syntax">
mkdir class
set CLASSPATH=class;lib/jalpp.jar;lib/activation.jar;lib/jaxp-api.jar;lib/jax-qname.jar;lib/JaxRPC_PP_Stubs.jar;lib/jaxrpc-api.jar;lib/jaxrpc-impl.jar;lib/jaxrpc-spi.jar;lib/mail.jar;lib/relaxngDatatype.jar;lib/saaj-api.jar;lib/saaj-impl.jar;lib/xerces.jar;lib/xsdlib.jar
javac -d class -classpath %CLASSPATH% src/com/scitegic/proxy/examples/*.java
</pre>
<p>Now use the "run.bat" batch file to run the example program. The
batch file sets the CLASSPATH, and runs java to
execute the .class file. The CLASSPATH is the same as
the one used in the compile step. Remember to enter the Pipeline Pilot server URL, the username, and the password on the command line.</p>
<pre class="syntax">
set CLASSPATH=class;lib/jalpp.jar;lib/activation.jar;lib/jaxp-api.jar;lib/jax-qname.jar;lib/JaxRPC_PP_Stubs.jar;lib/jaxrpc-api.jar;lib/jaxrpc-impl.jar;lib/jaxrpc-spi.jar;lib/mail.jar;lib/relaxngDatatype.jar;lib/saaj-api.jar;lib/saaj-impl.jar;lib/xerces.jar;lib/xsdlib.jar
java -classpath %CLASSPATH% com.scitegic.proxy.examples.SimpleSynchronousRun %1 %2 %3
</pre>
<p>With input values of "http://localhost:9944", "scitegicuser",
and "scitegic", the example program produces the following output:</p>
<pre class="syntax">
Printing server configuration settings
Local root = C:/SesInstall/public
Local temp root = C:/SesInstall/web/jobs
File Web Service Endpoint = http://localhost:9944/scitegic/file
Server version = 8.1.0.0
Authentication on = false
Printing SciTegic Root of server
SciTegic Root = C:/SesInstall/public
Printing folder names under Protocols/Examples
ADMET
Advanced Data Modeling
Chemistry
Data Modeling
Decision Trees
Gene Expression
Generic
Imaging
Integration
Next Gen Sequencing
Plate Data Analytics
R Statistics
Reporting
Sequence Analysis
Printing folder tree under Protocols/Web Services/Web Port Examples (omitting Utilities directories)
Protocols/Web Services/Web Port Examples
Chemistry
01 Simple Search
02 Property Profiling
03 ADMET Profiling
04 Clustering Molecules
05 Query by Form
06 SAR Table
07 Activity Modeling
Generic
Baseball Statistics
PilotPoker
Show Environment
XY Scatter Plot Utility
Sequence Analysis
DAS Data Sources
DAS Yeast Annotations
01 Protein Summary (local)
02 Protein Summary (internet)
03 Protein Summary and Text Search
List DAS Data Sources Protocols
Protein BLAST Interface
Sequence Analysis and Profile Search
Starting Protocol Job Synchronously
Getting job results:
http://localhost:9944/jobs/user/{jlpDAD4F-849E-4543-A41D-1D9B1268E70D}/Chart.pdf
Writing result file to C:\SesInstall\apps\scitegic\clientsdkjava\web\examples\chart.pdf
Job files will be deleted after you press <Enter>
>>>>
Deleting job {jlpDAD4F-849E-4543-A41D-1D9B1268E70D}
</pre>
<h2>An Asynchronous Example</h2>
<p>To run a protocol asynchronously, instead of synchronously, replace the <code>runAndPoll()</code> call
with the combination of <code>run()</code>, <code>getStatus()</code>, and <code>isExitStatus()</code>. The calling
program is now free to execute other Java code while waiting for the protocol
to complete, but it is the calling program's responsibility to manually poll
for the protocol's completion.</p>
<p>Replace the following code in the synchronous example:</p>
<pre class="syntax">
JobResult prr = protocol.runAndPoll();
JobStatus status = protocol.getStatus();
</pre>
<p>with this:</p>
<pre class="syntax">
protocol.run();
System.out.println("Polling Job to see if it is finished...");
JobStatus status = protocol.getStatus();
System.out.println("Protocol Status is: " + status.toString());
while (!status.isExitStatus()) {
Thread.sleep(2000);
status = protocol.getStatus();
System.out.println("Protocol Status is: " + status.toString());
}
</pre>
<p>The rest of the code remains unchanged. Compile and run as before.</p>
<p> </p>
</body>
</html>