-/* Copyright (c) 2009 Peter Troshin\r
+/* Copyright (c) 2011 Peter Troshin\r
* \r
- * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 \r
+ * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 \r
* \r
* This library is free software; you can redistribute it and/or modify it under the terms of the\r
* Apache License version 2 as published by the Apache Software Foundation\r
import java.io.Closeable;\r
import java.io.IOException;\r
import java.io.PrintWriter;\r
+import java.net.ConnectException;\r
import java.util.Arrays;\r
import java.util.List;\r
\r
import compbio.data.sequence.ScoreManager;\r
import compbio.data.sequence.SequenceUtil;\r
import compbio.metadata.JobStatus;\r
-import compbio.metadata.JobSubmissionException;\r
import compbio.metadata.Limit;\r
import compbio.metadata.LimitsManager;\r
import compbio.metadata.Option;\r
import compbio.metadata.Preset;\r
import compbio.metadata.PresetManager;\r
-import compbio.metadata.ResultNotAvailableException;\r
import compbio.metadata.RunnerConfig;\r
import compbio.metadata.UnsupportedRuntimeException;\r
-import compbio.metadata.WrongParameterException;\r
+import compbio.util.FileUtil;\r
+import compbio.util.Util;\r
\r
/**\r
* Class for testing web services\r
* \r
* @return List of FastaSequence records\r
*/\r
- static List<FastaSequence> loadSeqs() {\r
+ private static List<FastaSequence> loadSeqs() {\r
try {\r
return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput\r
.getBytes()));\r
* \r
* @return List of FastaSequence records\r
*/\r
- static List<FastaSequence> loadAlignment() {\r
+ private static List<FastaSequence> loadAlignment() {\r
try {\r
return SequenceUtil.readFasta(new ByteArrayInputStream(\r
fastaAlignment.getBytes()));\r
}\r
\r
private final PrintWriter writer;\r
+ private final String hostname;\r
\r
- public WSTester(PrintWriter writer) {\r
+ /**\r
+ * Construct an instance of JABAWS tester\r
+ * \r
+ * @param hostname\r
+ * - fully qualified host and context name of JABAWS e.g.\r
+ * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
+ * @param writer\r
+ * a PrintWriter instance to writer test log to.\r
+ */\r
+ public WSTester(String hostname, PrintWriter writer) {\r
+ if (Util.isEmpty(hostname)) {\r
+ throw new NullPointerException("Hostname must be provided!");\r
+ }\r
+ this.hostname = hostname;\r
this.writer = writer;\r
}\r
\r
/**\r
* Prints usage\r
*/\r
- void printUsage() {\r
- writer.println("Usage: <Class or Jar file name> " + hostkey\r
+ static void printUsage() {\r
+ System.out.println("Usage: <Class or Jar file name> " + hostkey\r
+ pseparator + "host_and_context " + "<" + servicekey\r
+ pseparator + "serviceName>");\r
- writer.println();\r
- writer.println(hostkey\r
- + pseparator\r
- + "<host_and_context> - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws");\r
- writer.println(servicekey\r
- + pseparator\r
- + "<ServiceName> - optional if unspecified all services are tested otherwise one of "\r
- + Arrays.toString(Services.values()));\r
- writer.println();\r
+ System.out.println();\r
+ System.out\r
+ .println(hostkey\r
+ + pseparator\r
+ + "<host_and_context> - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws");\r
+ System.out\r
+ .println(servicekey\r
+ + pseparator\r
+ + "<ServiceName> - optional if unspecified all services are tested otherwise one of "\r
+ + Arrays.toString(Services.values()));\r
+ System.out.println();\r
\r
}\r
\r
* list of the Preset\r
* @throws UnsupportedRuntimeException\r
*/\r
- <T> boolean presetAlign(MsaWS<T> msaws, List<Preset<T>> presets)\r
+ private <T> boolean presetAlign(MsaWS<T> msaws, List<Preset<T>> presets)\r
throws UnsupportedRuntimeException {\r
boolean succeed = false;\r
for (Preset<T> preset : presets) {\r
writer.println(OK);\r
}\r
succeed = true;\r
- } catch (UnsupportedRuntimeException e) {\r
- writer.println(FAILED);\r
- // If executable is not supported than none of the presets are\r
- // going to work\r
- throw new UnsupportedRuntimeException(e);\r
- } catch (JobSubmissionException e) {\r
- // TODO custom message\r
- writer.println(FAILED);\r
- writer.println();\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
- continue;\r
- } catch (WrongParameterException e) {\r
- // TODO custom message\r
- writer.println(FAILED);\r
- writer.println();\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
- continue;\r
- } catch (ResultNotAvailableException e) {\r
- // TODO custom message\r
- writer.println(FAILED);\r
- writer.println();\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
+ } catch (Exception e) {\r
+ if (e instanceof UnsupportedRuntimeException) {\r
+ // If executable is not supported than none of the presets\r
+ // are\r
+ // going to work\r
+ throw (UnsupportedRuntimeException) e;\r
+ } else {\r
+ reportException(e);\r
+ }\r
continue;\r
}\r
}\r
return succeed;\r
}\r
\r
- private <T> boolean testMsaWS(MsaWS<T> msaws)\r
- throws UnsupportedRuntimeException {\r
+ private <T> boolean testMsaWS(MsaWS<T> msaws) throws Exception {\r
+ assert msaws != null;\r
\r
boolean succeed = testDefaultAlignment(msaws);\r
-\r
// If exception above is thrown than the tests below is not run\r
\r
PresetManager<T> pmanager = msaws.getPresets();\r
* executable does not exists for a server platform\r
*/\r
@SuppressWarnings("unchecked")\r
- public <T> boolean checkService(JABAService wservice) {\r
+ private <T> boolean checkService(JABAService wservice, Services service) {\r
try {\r
if (wservice == null) {\r
throw new NullPointerException(\r
"JABAService instance must be provided!");\r
}\r
+\r
if (wservice instanceof MsaWS) {\r
return testMsaWS((MsaWS<T>) wservice);\r
} else if (wservice instanceof SequenceAnnotation) {\r
- return testSequenceAnnotationWS((SequenceAnnotation<T>) wservice);\r
+ return testSequenceAnnotationWS(\r
+ (SequenceAnnotation<T>) wservice, service);\r
} else {\r
throw new UnsupportedOperationException("The service: "\r
+ wservice.getClass() + " is not supported! ");\r
}\r
- } catch (UnsupportedRuntimeException e) {\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
+ } catch (Exception e) {\r
+ reportException(e);\r
return false;\r
}\r
}\r
\r
- private <T> boolean testSequenceAnnotationWS(SequenceAnnotation<T> wservice)\r
- throws UnsupportedRuntimeException {\r
- boolean success = testDefaultAnalyse(loadAlignment(), wservice, null,\r
- null);\r
+ private <T> boolean testSequenceAnnotationWS(\r
+ SequenceAnnotation<T> wservice, Services service) throws Exception {\r
+ writer.print("Calling analyse.........");\r
+\r
+ List<FastaSequence> input = loadSeqs();\r
+ if (service == Services.AAConWS) {\r
+ input = loadAlignment();\r
+ }\r
+ boolean success = testDefaultAnalyse(input, wservice, null, null);\r
+\r
PresetManager<T> presetman = wservice.getPresets();\r
if (presetman != null) {\r
List<Preset<T>> presets = presetman.getPresets();\r
if (presets != null && !presets.isEmpty()) {\r
Preset<T> preset = presets.get(0);\r
- success = testDefaultAnalyse(loadAlignment(), wservice, preset,\r
- null);\r
+ writer.print("Calling analyse with Preset.........");\r
+ success = testDefaultAnalyse(input, wservice, preset, null);\r
}\r
}\r
testMetadata(wservice);\r
return success;\r
}\r
\r
- <T> boolean testDefaultAnalyse(List<FastaSequence> fastalist,\r
+ private <T> boolean testDefaultAnalyse(List<FastaSequence> fastalist,\r
SequenceAnnotation<T> wsproxy, Preset<T> preset,\r
- List<Option<T>> customOptions) throws UnsupportedRuntimeException {\r
+ List<Option<T>> customOptions) throws Exception {\r
\r
ScoreManager scores = null;\r
- try {\r
- String jobId = null;\r
- if (customOptions != null) {\r
- jobId = wsproxy.customAnalize(fastalist, customOptions);\r
- } else if (preset != null) {\r
- jobId = wsproxy.presetAnalize(fastalist, preset);\r
- } else {\r
- jobId = wsproxy.analize(fastalist);\r
- }\r
- writer.println("\n\ncalling analise.........");\r
- Thread.sleep(1000);\r
- scores = wsproxy.getAnnotation(jobId);\r
-\r
- } catch (JobSubmissionException e) {\r
- writer.println("Exception while submitting job to a web server. "\r
- + "Exception details are below:");\r
- e.printStackTrace(writer);\r
- } catch (ResultNotAvailableException e) {\r
- writer.println("Exception while waiting for results. "\r
- + "Exception details are below:");\r
- e.printStackTrace(writer);\r
- } catch (InterruptedException e) {\r
- // ignore and propagate an interruption\r
- Thread.currentThread().interrupt();\r
- writer.println("Exception while waiting for results. "\r
- + "Exception details are below:");\r
- e.printStackTrace(writer);\r
- } catch (WrongParameterException e) {\r
- writer.println("Exception while parsing the web method input parameters. "\r
- + "Exception details are below:");\r
- e.printStackTrace(writer);\r
+\r
+ String jobId = null;\r
+ if (customOptions != null) {\r
+ jobId = wsproxy.customAnalize(fastalist, customOptions);\r
+ } else if (preset != null) {\r
+ jobId = wsproxy.presetAnalize(fastalist, preset);\r
+ } else {\r
+ jobId = wsproxy.analize(fastalist);\r
+ }\r
+ Thread.sleep(1000);\r
+ scores = wsproxy.getAnnotation(jobId);\r
+ if (scores != null) {\r
+ writer.println(OK);\r
}\r
+\r
return scores != null;\r
}\r
\r
+ private void reportException(Exception e) {\r
+ writer.println(FAILED);\r
+ writer.println("Exception while waiting for results. "\r
+ + "Exception details are below:");\r
+ writer.println(e.getLocalizedMessage());\r
+ e.printStackTrace(writer);\r
+ }\r
+\r
private <T> void testMetadata(Metadata<T> msaws)\r
throws UnsupportedRuntimeException {\r
\r
LimitsManager<T> limits = msaws.getLimits();\r
if (limits != null && limits.getLimits().size() > 0) {\r
writer.println(OK);\r
+ // writer.println("Limits details: \n" + limits.toString());\r
} else {\r
writer.println(UNSUPPORTED);\r
}\r
* @param msaws\r
* @throws UnsupportedRuntimeException\r
*/\r
- public <T> boolean testDefaultAlignment(MsaWS<T> msaws)\r
- throws UnsupportedRuntimeException {\r
+ private <T> boolean testDefaultAlignment(MsaWS<T> msaws) throws Exception {\r
writer.print("Testing alignment with default parameters:");\r
Alignment al = null;\r
boolean succeed = false;\r
- try {\r
- String taskId = msaws.align(seqs);\r
- writer.print("\nQuerying job status...");\r
- JobStatus status = msaws.getJobStatus(taskId);\r
- while (status != JobStatus.FINISHED) {\r
- Thread.sleep(1000);\r
- status = msaws.getJobStatus(taskId);\r
- }\r
- writer.println(OK);\r
- writer.print("Retrieving results...");\r
- al = msaws.getResult(taskId);\r
- succeed = true;\r
- } catch (ResultNotAvailableException e) {\r
- writer.println(FAILED);\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
- } catch (JobSubmissionException e) {\r
- writer.println(FAILED);\r
- writer.println();\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
- } catch (InterruptedException e) {\r
- writer.println(FAILED);\r
- writer.println();\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
+\r
+ String taskId = msaws.align(seqs);\r
+ writer.print("\nQuerying job status...");\r
+ JobStatus status = msaws.getJobStatus(taskId);\r
+ while (status != JobStatus.FINISHED) {\r
+ Thread.sleep(1000);\r
+ status = msaws.getJobStatus(taskId);\r
}\r
+ writer.println(OK);\r
+ writer.print("Retrieving results...");\r
+ al = msaws.getResult(taskId);\r
+ succeed = true;\r
if (al != null) {\r
writer.println(OK);\r
}\r
return succeed;\r
}\r
-\r
- /**\r
- * Connect to a WS using the host and the service name\r
- * \r
- * @param <T>\r
- * @param host\r
- * @param service\r
- * @return\r
- */\r
- <T> JABAService connect(String host, Services service) {\r
- JABAService jabaservice = null;\r
- try {\r
- writer.print("Connecting to service " + service + " on " + host\r
- + " ... ");\r
- jabaservice = Jws2Client.connect(host, service);\r
- writer.println(OK);\r
- } catch (WebServiceException e) {\r
- writer.println(FAILED);\r
- writer.println(e.getLocalizedMessage());\r
- e.printStackTrace(writer);\r
- }\r
- return jabaservice;\r
- }\r
-\r
/**\r
* Test JWS2 web services\r
* \r
* @throws IOException\r
*/\r
public static <T> void main(String[] args) throws IOException {\r
- WSTester tester = new WSTester(new PrintWriter(System.out, true));\r
+\r
if (args == null || args.length < 1) {\r
- tester.printUsage();\r
+ WSTester.printUsage();\r
System.exit(0);\r
}\r
String host = CmdHelper.getHost(args);\r
String serviceName = CmdHelper.getServiceName(args);\r
if (!Jws2Client.validURL(host)) {\r
- tester.writer\r
+ System.err\r
.println("<host_and_context> parameter is not provided or is incorrect!");\r
System.exit(1);\r
}\r
- boolean succeed = false;\r
- MsaWS<T> msaws = null;\r
+ WSTester tester = new WSTester(host, new PrintWriter(System.out, true));\r
+\r
if (serviceName != null) {\r
Services service = Services.getService(serviceName);\r
if (service == null) {\r
+ "' is not supported. Valid values are: "\r
+ Arrays.toString(Services.values()));\r
tester.writer.println();\r
- tester.printUsage();\r
- System.exit(1);\r
- }\r
-\r
- msaws = (MsaWS<T>) tester.connect(host, service);\r
- if (msaws == null) {\r
+ printUsage();\r
System.exit(1);\r
}\r
- try {\r
- succeed = tester.checkService(msaws);\r
- } finally {\r
- ((Closeable) msaws).close();\r
- }\r
- // TODO test results printing!\r
- tester.reportResults(service, succeed);\r
+ tester.checkService(service);\r
System.exit(0);\r
}\r
\r
\r
for (Services serv : Services.values()) {\r
tester.writer.println();\r
- msaws = (MsaWS<T>) tester.connect(host, serv);\r
- if (msaws == null) {\r
- continue;\r
- }\r
- try {\r
- succeed = tester.checkService(msaws);\r
- } finally {\r
- ((Closeable) msaws).close();\r
- }\r
- tester.reportResults(serv, succeed);\r
+ tester.checkService(serv);\r
}\r
\r
}\r
+\r
+ /**\r
+ * Test JABA web service\r
+ * \r
+ * @param service\r
+ * the service to test\r
+ * @return true if the service works as expected, false otherwise\r
+ * @throws WebServiceException\r
+ * @throws ConnectException\r
+ */\r
+ public boolean checkService(Services service) throws ConnectException,\r
+ WebServiceException {\r
+ JABAService ws = Jws2Client.connect(hostname, service);\r
+ if (ws == null) {\r
+ writer.println("Cannot estabilish the connection to host "\r
+ + hostname + " with service " + service.toString());\r
+ return false;\r
+ }\r
+ boolean succeed = false;\r
+ try {\r
+ writer.println("Checking service " + service.toString());\r
+ succeed = checkService(ws, service);\r
+ } finally {\r
+ FileUtil.closeSilently(((Closeable) ws));\r
+ }\r
+ reportResults(service, succeed);\r
+ return succeed;\r
+ }\r
+\r
private void reportResults(Services serv, boolean succeed) {\r
if (succeed) {\r
writer.println("Check is completed. The Service " + serv\r
- + " IS WORKING");\r
+ + " IS WORKING\n");\r
} else {\r
- writer.println("Check is completed. The Service " + serv\r
- + " HAS SOME PROBLEMS");\r
+ writer.println("Check is aborted. The Service " + serv\r
+ + " HAS SOME PROBLEMS\n");\r
}\r
}\r
}\r