1 /* Copyright (c) 2011 Peter Troshin
\r
2 * Copyright (c) 2013 Alexander Sherstnev
\r
4 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
6 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
7 * Apache License version 2 as published by the Apache Software Foundation
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
11 * License for more details.
\r
13 * A copy of the license is in apache_license.txt. It is also available here:
\r
14 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
16 * Any republication or derived work distributed in source code form
\r
17 * must include this copyright and license notice.
\r
20 package compbio.ws.client;
\r
22 import static compbio.ws.client.Constraints.hostkey;
\r
23 import static compbio.ws.client.Constraints.pseparator;
\r
24 import static compbio.ws.client.Constraints.servicekey;
\r
26 import java.io.ByteArrayInputStream;
\r
27 import java.io.Closeable;
\r
28 import java.io.IOException;
\r
29 import java.io.PrintWriter;
\r
30 import java.net.ConnectException;
\r
31 import java.net.MalformedURLException;
\r
32 import java.net.URL;
\r
33 import java.util.Arrays;
\r
34 import java.util.List;
\r
35 import java.util.logging.Level;
\r
37 import javax.xml.ws.Service;
\r
38 import javax.xml.ws.WebServiceException;
\r
40 import compbio.data.msa.JABAService;
\r
41 import compbio.data.msa.Metadata;
\r
42 import compbio.data.msa.MsaWS;
\r
43 import compbio.data.msa.SequenceAnnotation;
\r
44 import compbio.data.sequence.Alignment;
\r
45 import compbio.data.sequence.FastaSequence;
\r
46 import compbio.data.sequence.ScoreManager;
\r
47 import compbio.data.sequence.SequenceUtil;
\r
48 import compbio.metadata.JobStatus;
\r
49 import compbio.metadata.Limit;
\r
50 import compbio.metadata.LimitsManager;
\r
51 import compbio.metadata.Option;
\r
52 import compbio.metadata.Preset;
\r
53 import compbio.metadata.PresetManager;
\r
54 import compbio.metadata.RunnerConfig;
\r
55 import compbio.metadata.UnsupportedRuntimeException;
\r
56 import compbio.util.FileUtil;
\r
57 import compbio.util.Util;
\r
60 * Class for testing web services
\r
64 * @version 1.5 February 2013
\r
66 public class WSTester {
\r
69 * Test sequences to be used as input for WS
\r
71 public static final String fastaInput2records =
\r
72 ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n"
\r
73 + ">Bar\nASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n";
\r
74 public static final String fastaInput1record =
\r
75 ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n";
\r
76 public static final String fastaAlignment =
\r
77 ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n"
\r
78 + ">Bar\nASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n";
\r
79 public static final String fastaRNAAlignment =
\r
80 ">Foo\nC-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGAUGGUUG----GCAAC\n"
\r
81 + ">Bar\nG-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGAU-UACGAUUAACCAC\n";
\r
86 private static final String FAILED = "FAILED";
\r
87 private static final String OK = "OK";
\r
88 private static final String UNSUPPORTED = "UNSUPPORTED";
\r
91 * Converting input to a form accepted by WS
\r
93 * @return List of FastaSequence records
\r
95 private static List<FastaSequence> loadSeqs(int nLines) {
\r
98 return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput1record.getBytes()));
\r
100 return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput2records.getBytes()));
\r
101 } catch (IOException ignored) {
\r
102 // Should not happen as a source is not a external stream
\r
103 ignored.printStackTrace();
\r
109 * Converting input to a form accepted by WS
\r
111 * @return List of FastaSequence records
\r
113 private static List<FastaSequence> loadAlignment() {
\r
115 return SequenceUtil.readFasta(new ByteArrayInputStream(
\r
116 fastaAlignment.getBytes()));
\r
117 } catch (IOException ignored) {
\r
118 // Should not happen as a source is not a external stream
\r
119 ignored.printStackTrace();
\r
124 * Converting input to a form accepted by WS
\r
126 * @return List of FastaSequence records with aligned RNA sequences
\r
128 private static List<FastaSequence> loadRNAAlignment() {
\r
130 return SequenceUtil.readFasta(new ByteArrayInputStream(fastaRNAAlignment.getBytes()));
\r
131 } catch (IOException ignored) {
\r
132 // Should not happen as a source is not a external stream
\r
133 ignored.printStackTrace();
\r
138 private final PrintWriter writer;
\r
139 private final String hostname;
\r
142 * Construct an instance of JABAWS tester
\r
145 * - fully qualified host and context name of JABAWS e.g.
\r
146 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
148 * a PrintWriter instance to writer test log to.
\r
150 public WSTester(String hostname, PrintWriter writer) {
\r
151 if (Util.isEmpty(hostname)) {
\r
152 throw new NullPointerException("Hostname must be provided!");
\r
154 this.hostname = hostname;
\r
155 this.writer = writer;
\r
161 static void printUsage() {
\r
162 System.out.println("Usage: <Class or Jar file name> " + hostkey
\r
163 + pseparator + "host_and_context " + "<" + servicekey
\r
164 + pseparator + "serviceName>");
\r
165 System.out.println();
\r
166 System.out.println(hostkey
\r
168 + "<host_and_context> - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws");
\r
169 System.out.println(servicekey
\r
171 + "<ServiceName> - optional if unspecified all services are tested otherwise one of "
\r
172 + Arrays.toString(Services.values()));
\r
173 System.out.println();
\r
178 * Calls alignment with preset
\r
183 * list of the Preset
\r
184 * @throws UnsupportedRuntimeException
\r
186 private <T> boolean presetAlign(MsaWS<T> msaws, List<Preset<T>> presets)
\r
187 throws UnsupportedRuntimeException {
\r
188 boolean succeed = false;
\r
189 for (Preset<T> preset : presets) {
\r
190 writer.print("Aligning with preset '" + preset.getName() + "'... ");
\r
191 Alignment al = null;
\r
193 String taskId = msaws.presetAlign(loadSeqs(2), preset);
\r
194 al = msaws.getResult(taskId);
\r
196 writer.println(OK);
\r
199 } catch (Exception e) {
\r
200 if (e instanceof UnsupportedRuntimeException) {
\r
201 // If executable is not supported than none of the presets
\r
204 throw (UnsupportedRuntimeException) e;
\r
206 reportException(e);
\r
214 private <T> boolean testMsaWS(MsaWS<T> msaws, Services service) throws Exception {
\r
215 assert msaws != null;
\r
217 boolean succeed = testDefaultAlignment(msaws, service);
\r
218 // If exception above is thrown than the tests below is not run
\r
220 PresetManager<T> pmanager = msaws.getPresets();
\r
221 if (pmanager != null && pmanager.getPresets().size() > 0) {
\r
222 writer.println("Testing alignment with presets:");
\r
223 List<Preset<T>> plist = pmanager.getPresets();
\r
224 succeed = !succeed ? presetAlign(msaws, plist) : succeed;
\r
226 testMetadata(msaws);
\r
230 * Call most of web services functions and check the output
\r
235 * @throws UnsupportedRuntimeException
\r
236 * is thrown if the connection to a web service was made, but
\r
237 * the web service is not functional. e.g. when native
\r
238 * executable does not exists for a server platform
\r
240 @SuppressWarnings("unchecked")
\r
241 private <T> boolean checkService(JABAService wservice, Services service) {
\r
243 if (wservice == null) {
\r
244 throw new NullPointerException("JABAService instance must be provided!");
\r
247 if (wservice instanceof MsaWS) {
\r
248 return testMsaWS((MsaWS<T>) wservice, service);
\r
249 } else if (wservice instanceof SequenceAnnotation) {
\r
250 return testSequenceAnnotationWS(
\r
251 (SequenceAnnotation<T>) wservice, service);
\r
253 throw new UnsupportedOperationException("The service: " + wservice.getClass() + " is not supported! ");
\r
255 } catch (Exception e) {
\r
256 reportException(e);
\r
261 private <T> boolean testSequenceAnnotationWS(
\r
262 SequenceAnnotation<T> wservice, Services service) throws Exception {
\r
263 writer.print("Calling annotation test.........");
\r
265 List<FastaSequence> input = loadSeqs(2);
\r
266 if (service == Services.AAConWS ) {
\r
267 input = loadAlignment();
\r
268 } else if (service == Services.RNAalifoldWS) {
\r
269 input = loadRNAAlignment();
\r
271 boolean success = testDefaultAnalyse(input, wservice, null, null);
\r
273 PresetManager<T> presetman = wservice.getPresets();
\r
274 if (presetman != null) {
\r
275 List<Preset<T>> presets = presetman.getPresets();
\r
276 if (presets != null && !presets.isEmpty()) {
\r
277 Preset<T> preset = presets.get(0);
\r
278 writer.print("Calling analyse with Preset.........");
\r
279 success = testDefaultAnalyse(input, wservice, preset, null);
\r
282 testMetadata(wservice);
\r
286 private <T> boolean testDefaultAnalyse(List<FastaSequence> fastalist,
\r
287 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
288 List<Option<T>> customOptions) throws Exception {
\r
290 ScoreManager scores = null;
\r
292 String jobId = null;
\r
293 if (customOptions != null) {
\r
294 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
295 } else if (preset != null) {
\r
296 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
298 jobId = wsproxy.analize(fastalist);
\r
300 Thread.sleep(1000);
\r
301 scores = wsproxy.getAnnotation(jobId);
\r
302 if (scores != null) {
\r
303 writer.println(OK);
\r
306 return scores != null;
\r
309 private void reportException(Exception e) {
\r
310 writer.println(FAILED);
\r
311 writer.println("Exception while waiting for results. Exception details are below:");
\r
312 writer.println(e.getLocalizedMessage());
\r
313 e.printStackTrace(writer);
\r
316 private <T> void testMetadata(Metadata<T> msaws)
\r
317 throws UnsupportedRuntimeException {
\r
319 writer.print("Querying presets...");
\r
320 PresetManager<T> pmanager = msaws.getPresets();
\r
321 if (pmanager != null && pmanager.getPresets().size() > 0) {
\r
322 writer.println(OK);
\r
324 writer.println(UNSUPPORTED);
\r
327 writer.print("Querying Parameters...");
\r
328 RunnerConfig<T> options = msaws.getRunnerOptions();
\r
329 if (options != null && options.getArguments().size() > 0) {
\r
330 writer.println(OK);
\r
332 writer.println(UNSUPPORTED);
\r
335 writer.print("Querying Limits...");
\r
336 LimitsManager<T> limits = msaws.getLimits();
\r
337 if (limits != null && limits.getLimits().size() > 0) {
\r
338 writer.println(OK);
\r
339 // writer.println("Limits details: \n" + limits.toString());
\r
341 writer.println(UNSUPPORTED);
\r
344 writer.print("Querying Local Engine Limits...");
\r
345 Limit<T> localLimit = msaws
\r
346 .getLimit(PresetManager.LOCAL_ENGINE_LIMIT_PRESET);
\r
347 if (localLimit != null) {
\r
348 writer.println(OK);
\r
350 writer.println(UNSUPPORTED);
\r
355 * Align using default settings
\r
359 * @throws UnsupportedRuntimeException
\r
361 private <T> boolean testDefaultAlignment(MsaWS<T> msaws, Services service) throws Exception {
\r
362 writer.print("Testing alignment with default parameters:");
\r
363 Alignment al = null;
\r
364 boolean succeed = false;
\r
367 if (service == Services.JpredWS) {
\r
368 taskId = msaws.align(loadAlignment());
\r
370 taskId = msaws.align(loadSeqs(2));
\r
372 writer.print("\nQuerying job status...");
\r
373 JobStatus status = msaws.getJobStatus(taskId);
\r
374 while (status != JobStatus.FINISHED) {
\r
375 Thread.sleep(1000);
\r
376 status = msaws.getJobStatus(taskId);
\r
378 writer.println(OK);
\r
379 writer.print("Retrieving results...");
\r
380 al = msaws.getResult(taskId);
\r
383 writer.println(OK);
\r
388 * Test JWS2 web services
\r
393 * -h=<Your web application server host name, port and JWS2
\r
396 * -s=<ServiceName> which is optional. If service name is not
\r
397 * provided then all known JWS2 web services are tested
\r
398 * @throws IOException
\r
400 public static <T> void main(String[] args) throws IOException {
\r
402 if (args == null || args.length < 1) {
\r
403 WSTester.printUsage();
\r
406 String host = CmdHelper.getHost(args);
\r
407 String serviceName = CmdHelper.getServiceName(args);
\r
408 if (!Jws2Client.validURL(host)) {
\r
409 System.err.println("<host_and_context> parameter is not provided or is incorrect!");
\r
412 WSTester tester = new WSTester(host, new PrintWriter(System.out, true));
\r
414 if (serviceName != null) {
\r
415 Services service = Services.getService(serviceName);
\r
416 if (service == null) {
\r
417 tester.writer.println("Service '" + serviceName + "' is not supported. Valid values are: "
\r
418 + Arrays.toString(Services.values()));
\r
419 tester.writer.println();
\r
423 tester.checkService(service);
\r
427 tester.writer.println("<ServiceName> is not provided checking all known services...");
\r
429 for (Services serv : Services.values()) {
\r
430 tester.writer.println();
\r
431 tester.checkService(serv);
\r
437 * Connects to a web service by the host and the service name web service type
\r
440 * the fully qualified name of JABAWS server including JABAWS
\r
442 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
444 * the name of the JABAWS service to connect to
\r
445 * @return JABAService<T>
\r
446 * @throws WebServiceException
\r
447 * @throws ConnectException
\r
448 * if fails to connect to the service on the host
\r
450 private JABAService connect(String host, Services service)
\r
451 throws WebServiceException, ConnectException {
\r
453 System.out.println ("Attempting to connect with " + service.toString() + "...");
\r
455 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
456 System.out.println ("URL: " + url.toString());
\r
457 } catch (MalformedURLException e) {
\r
458 e.printStackTrace();
\r
460 Service serv = null;
\r
462 serv = service.getService(url, service.getServiceNamespace());
\r
463 } catch (WebServiceException wse) {
\r
464 wse.printStackTrace();
\r
466 if (serv == null) {
\r
467 throw new ConnectException("Could not connect to " + url + ". Is the server down?");
\r
469 JABAService srv = service.getInterface(serv);
\r
470 System.out.println ("Connected successfully!");
\r
475 * Test JABA web service
\r
478 * the service to test
\r
479 * @return true if the service works as expected, false otherwise
\r
480 * @throws WebServiceException
\r
481 * @throws ConnectException
\r
483 public boolean checkService(Services service) throws ConnectException,
\r
484 WebServiceException {
\r
485 JABAService ws = connect(hostname, service);
\r
487 String line = "Cannot estabilish the connection to host " + hostname + " with service ";
\r
488 writer.println(line + service.toString());
\r
491 boolean succeed = false;
\r
493 writer.println("Checking service " + service.toString());
\r
494 succeed = checkService(ws, service);
\r
496 FileUtil.closeSilently(((Closeable) ws));
\r
498 reportResults(service, succeed);
\r
502 private void reportResults(Services serv, boolean succeed) {
\r
504 writer.println("Check is completed. The Service " + serv + " IS WORKING\n");
\r
506 writer.println("Check is aborted. The Service " + serv + " HAS SOME PROBLEMS\n");
\r