1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.ws.client;
\r
21 import static compbio.ws.client.Constraints.hostkey;
\r
22 import static compbio.ws.client.Constraints.pseparator;
\r
23 import static compbio.ws.client.Constraints.servicekey;
\r
25 import java.io.ByteArrayInputStream;
\r
26 import java.io.Closeable;
\r
27 import java.io.IOException;
\r
28 import java.io.PrintWriter;
\r
29 import java.net.ConnectException;
\r
30 import java.util.Arrays;
\r
31 import java.util.List;
\r
33 import javax.xml.ws.WebServiceException;
\r
35 import org.apache.log4j.Logger;
\r
37 import compbio.data.msa.JABAService;
\r
38 import compbio.data.msa.Metadata;
\r
39 import compbio.data.msa.MsaWS;
\r
40 import compbio.data.msa.SequenceAnnotation;
\r
41 import compbio.data.sequence.Alignment;
\r
42 import compbio.data.sequence.FastaSequence;
\r
43 import compbio.data.sequence.ScoreManager;
\r
44 import compbio.data.sequence.SequenceUtil;
\r
45 import compbio.metadata.JobStatus;
\r
46 import compbio.metadata.Limit;
\r
47 import compbio.metadata.LimitsManager;
\r
48 import compbio.metadata.Option;
\r
49 import compbio.metadata.Preset;
\r
50 import compbio.metadata.PresetManager;
\r
51 import compbio.metadata.RunnerConfig;
\r
52 import compbio.metadata.UnsupportedRuntimeException;
\r
53 import compbio.util.FileUtil;
\r
54 import compbio.util.Util;
\r
57 * Class for testing web services
\r
61 * @version 1.0 February 2010
\r
63 public class WSTester {
\r
65 private static Logger log = Logger.getLogger(WSTester.class);
\r
67 * Sequences to be used as input for all WS
\r
69 public static final String fastaInput = ">Foo\n"
\r
70 + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV"
\r
72 + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n";
\r
74 public static final String fastaAlignment = ">Foo\n"
\r
75 + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n"
\r
77 + "ASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n"
\r
80 static final List<FastaSequence> seqs = loadSeqs();
\r
82 private static final String FAILED = "FAILED";
\r
83 private static final String OK = "OK";
\r
85 private static final String UNSUPPORTED = "UNSUPPORTED";
\r
88 * Converting input to a form accepted by WS
\r
90 * @return List of FastaSequence records
\r
92 private static List<FastaSequence> loadSeqs() {
\r
94 return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput
\r
96 } catch (IOException ignored) {
\r
97 // Should not happen as a source is not a external stream
\r
98 ignored.printStackTrace();
\r
104 * Converting input to a form accepted by WS
\r
106 * @return List of FastaSequence records
\r
108 private static List<FastaSequence> loadAlignment() {
\r
110 return SequenceUtil.readFasta(new ByteArrayInputStream(
\r
111 fastaAlignment.getBytes()));
\r
112 } catch (IOException ignored) {
\r
113 // Should not happen as a source is not a external stream
\r
114 ignored.printStackTrace();
\r
119 private final PrintWriter writer;
\r
120 private final String hostname;
\r
123 * Construct an instance of JABAWS tester
\r
126 * - fully qualified host and context name of JABAWS e.g.
\r
127 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
129 * a PrintWriter instance to writer test log to.
\r
131 public WSTester(String hostname, PrintWriter writer) {
\r
132 if (Util.isEmpty(hostname)) {
\r
133 throw new NullPointerException("Hostname must be provided!");
\r
135 this.hostname = hostname;
\r
136 this.writer = writer;
\r
142 static void printUsage() {
\r
143 System.out.println("Usage: <Class or Jar file name> " + hostkey
\r
144 + pseparator + "host_and_context " + "<" + servicekey
\r
145 + pseparator + "serviceName>");
\r
146 System.out.println();
\r
150 + "<host_and_context> - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws");
\r
152 .println(servicekey
\r
154 + "<ServiceName> - optional if unspecified all services are tested otherwise one of "
\r
155 + Arrays.toString(Services.values()));
\r
156 System.out.println();
\r
161 * Calls alignment with preset
\r
166 * list of the Preset
\r
167 * @throws UnsupportedRuntimeException
\r
169 private <T> boolean presetAlign(MsaWS<T> msaws, List<Preset<T>> presets)
\r
170 throws UnsupportedRuntimeException {
\r
171 boolean succeed = false;
\r
172 for (Preset<T> preset : presets) {
\r
173 writer.print("Aligning with preset '" + preset.getName() + "'... ");
\r
174 Alignment al = null;
\r
176 String taskId = msaws.presetAlign(seqs, preset);
\r
177 al = msaws.getResult(taskId);
\r
179 writer.println(OK);
\r
182 } catch (Exception e) {
\r
183 if (e instanceof UnsupportedRuntimeException) {
\r
184 // If executable is not supported than none of the presets
\r
187 throw (UnsupportedRuntimeException) e;
\r
189 reportException(e);
\r
197 private <T> boolean testMsaWS(MsaWS<T> msaws) throws Exception {
\r
198 assert msaws != null;
\r
200 boolean succeed = testDefaultAlignment(msaws);
\r
201 // If exception above is thrown than the tests below is not run
\r
203 PresetManager<T> pmanager = msaws.getPresets();
\r
204 if (pmanager != null && pmanager.getPresets().size() > 0) {
\r
205 writer.println("Testing alignment with presets:");
\r
206 List<Preset<T>> plist = pmanager.getPresets();
\r
207 succeed = !succeed ? presetAlign(msaws, plist) : succeed;
\r
209 testMetadata(msaws);
\r
213 * Call most of web services functions and check the output
\r
218 * @throws UnsupportedRuntimeException
\r
219 * is thrown if the connection to a web service was made, but
\r
220 * the web service is not functional. e.g. when native
\r
221 * executable does not exists for a server platform
\r
223 @SuppressWarnings("unchecked")
\r
224 private <T> boolean checkService(JABAService wservice, Services service) {
\r
226 if (wservice == null) {
\r
227 throw new NullPointerException(
\r
228 "JABAService instance must be provided!");
\r
231 if (wservice instanceof MsaWS) {
\r
232 return testMsaWS((MsaWS<T>) wservice);
\r
233 } else if (wservice instanceof SequenceAnnotation) {
\r
234 return testSequenceAnnotationWS(
\r
235 (SequenceAnnotation<T>) wservice, service);
\r
237 throw new UnsupportedOperationException("The service: "
\r
238 + wservice.getClass() + " is not supported! ");
\r
240 } catch (Exception e) {
\r
241 reportException(e);
\r
246 private <T> boolean testSequenceAnnotationWS(
\r
247 SequenceAnnotation<T> wservice, Services service) throws Exception {
\r
248 writer.print("Calling analyse.........");
\r
250 List<FastaSequence> input = loadSeqs();
\r
251 if (service == Services.AAConWS) {
\r
252 input = loadAlignment();
\r
254 boolean success = testDefaultAnalyse(input, wservice, null, null);
\r
256 PresetManager<T> presetman = wservice.getPresets();
\r
257 if (presetman != null) {
\r
258 List<Preset<T>> presets = presetman.getPresets();
\r
259 if (presets != null && !presets.isEmpty()) {
\r
260 Preset<T> preset = presets.get(0);
\r
261 writer.print("Calling analyse with Preset.........");
\r
262 success = testDefaultAnalyse(input, wservice, preset, null);
\r
265 testMetadata(wservice);
\r
269 private <T> boolean testDefaultAnalyse(List<FastaSequence> fastalist,
\r
270 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
271 List<Option<T>> customOptions) throws Exception {
\r
273 ScoreManager scores = null;
\r
275 String jobId = null;
\r
276 if (customOptions != null) {
\r
277 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
278 } else if (preset != null) {
\r
279 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
281 jobId = wsproxy.analize(fastalist);
\r
283 Thread.sleep(1000);
\r
284 scores = wsproxy.getAnnotation(jobId);
\r
285 if (scores != null) {
\r
286 writer.println(OK);
\r
289 return scores != null;
\r
292 private void reportException(Exception e) {
\r
293 writer.println(FAILED);
\r
294 writer.println("Exception while waiting for results. "
\r
295 + "Exception details are below:");
\r
296 writer.println(e.getLocalizedMessage());
\r
297 e.printStackTrace(writer);
\r
300 private <T> void testMetadata(Metadata<T> msaws)
\r
301 throws UnsupportedRuntimeException {
\r
303 writer.print("Querying presets...");
\r
304 PresetManager<T> pmanager = msaws.getPresets();
\r
305 if (pmanager != null && pmanager.getPresets().size() > 0) {
\r
306 writer.println(OK);
\r
308 writer.println(UNSUPPORTED);
\r
311 writer.print("Querying Parameters...");
\r
312 RunnerConfig<T> options = msaws.getRunnerOptions();
\r
313 if (options != null && options.getArguments().size() > 0) {
\r
314 writer.println(OK);
\r
316 writer.println(UNSUPPORTED);
\r
319 writer.print("Querying Limits...");
\r
320 LimitsManager<T> limits = msaws.getLimits();
\r
321 if (limits != null && limits.getLimits().size() > 0) {
\r
322 writer.println(OK);
\r
323 // writer.println("Limits details: \n" + limits.toString());
\r
325 writer.println(UNSUPPORTED);
\r
328 writer.print("Querying Local Engine Limits...");
\r
329 Limit<T> localLimit = msaws
\r
330 .getLimit(PresetManager.LOCAL_ENGINE_LIMIT_PRESET);
\r
331 if (localLimit != null) {
\r
332 writer.println(OK);
\r
334 writer.println(UNSUPPORTED);
\r
339 * Align using default settings
\r
343 * @throws UnsupportedRuntimeException
\r
345 private <T> boolean testDefaultAlignment(MsaWS<T> msaws) throws Exception {
\r
346 writer.print("Testing alignment with default parameters:");
\r
347 Alignment al = null;
\r
348 boolean succeed = false;
\r
350 String taskId = msaws.align(seqs);
\r
351 writer.print("\nQuerying job status...");
\r
352 JobStatus status = msaws.getJobStatus(taskId);
\r
353 while (status != JobStatus.FINISHED) {
\r
354 Thread.sleep(1000);
\r
355 status = msaws.getJobStatus(taskId);
\r
357 writer.println(OK);
\r
358 writer.print("Retrieving results...");
\r
359 al = msaws.getResult(taskId);
\r
362 writer.println(OK);
\r
367 * Test JWS2 web services
\r
372 * -h=<Your web application server host name, port and JWS2
\r
375 * -s=<ServiceName> which is optional. If service name is not
\r
376 * provided then all known JWS2 web services are tested
\r
377 * @throws IOException
\r
379 public static <T> void main(String[] args) throws IOException {
\r
381 if (args == null || args.length < 1) {
\r
382 WSTester.printUsage();
\r
385 String host = CmdHelper.getHost(args);
\r
386 String serviceName = CmdHelper.getServiceName(args);
\r
387 if (!Jws2Client.validURL(host)) {
\r
389 .println("<host_and_context> parameter is not provided or is incorrect!");
\r
392 WSTester tester = new WSTester(host, new PrintWriter(System.out, true));
\r
394 if (serviceName != null) {
\r
395 Services service = Services.getService(serviceName);
\r
396 if (service == null) {
\r
397 tester.writer.println("Service '" + serviceName
\r
398 + "' is not supported. Valid values are: "
\r
399 + Arrays.toString(Services.values()));
\r
400 tester.writer.println();
\r
404 tester.checkService(service);
\r
409 .println("<ServiceName> is not provided checking all known services...");
\r
411 for (Services serv : Services.values()) {
\r
412 tester.writer.println();
\r
413 tester.checkService(serv);
\r
419 * Test JABA web service
\r
422 * the service to test
\r
423 * @return true if the service works as expected, false otherwise
\r
424 * @throws WebServiceException
\r
425 * @throws ConnectException
\r
427 public boolean checkService(Services service) throws ConnectException,
\r
428 WebServiceException {
\r
429 JABAService ws = Jws2Client.connect(hostname, service);
\r
431 writer.println("Cannot estabilish the connection to host "
\r
432 + hostname + " with service " + service.toString());
\r
435 boolean succeed = false;
\r
437 writer.println("Checking service " + service.toString());
\r
438 succeed = checkService(ws, service);
\r
440 FileUtil.closeSilently(((Closeable) ws));
\r
442 reportResults(service, succeed);
\r
446 private void reportResults(Services serv, boolean succeed) {
\r
448 writer.println("Check is completed. The Service " + serv
\r
449 + " IS WORKING\n");
\r
451 writer.println("Check is aborted. The Service " + serv
\r
452 + " HAS SOME PROBLEMS\n");
\r