/* Copyright (c) 2009 Peter Troshin * * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache * License for more details. * * A copy of the license is in apache_license.txt. It is also available here: * @see: http://www.apache.org/licenses/LICENSE-2.0.txt * * Any republication or derived work distributed in source code form * must include this copyright and license notice. */ package compbio.ws.client; import static compbio.ws.client.Constraints.hostkey; import static compbio.ws.client.Constraints.pseparator; import static compbio.ws.client.Constraints.servicekey; import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.List; import javax.xml.ws.WebServiceException; import org.apache.log4j.Logger; import compbio.data.msa.JABAService; import compbio.data.msa.Metadata; import compbio.data.msa.MsaWS; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.Alignment; import compbio.data.sequence.FastaSequence; import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; import compbio.metadata.JobStatus; import compbio.metadata.JobSubmissionException; import compbio.metadata.Limit; import compbio.metadata.LimitsManager; import compbio.metadata.Option; import compbio.metadata.Preset; import compbio.metadata.PresetManager; import compbio.metadata.ResultNotAvailableException; import compbio.metadata.RunnerConfig; import compbio.metadata.UnsupportedRuntimeException; import compbio.metadata.WrongParameterException; /** * Class for testing web services * * @author pvtroshin * * @version 1.0 February 2010 */ public class WSTester { private static Logger log = Logger.getLogger(WSTester.class); /** * Sequences to be used as input for all WS */ static final String fastaInput = ">Foo\n" + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV" + "\n>Bar\n" + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA"; static final String fastaAlignment = ">Foo\r\n" + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\r\n" + ">Bar\r\n" + "ASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\r\n" + ""; static final List seqs = loadSeqs(); private static final String FAILED = "FAILED"; private static final String OK = "OK"; private static final String UNSUPPORTED = "UNSUPPORTED"; /** * Converting input to a form accepted by WS * * @return List of FastaSequence records */ static List loadSeqs() { try { return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput .getBytes())); } catch (IOException ignored) { // Should not happen as a source is not a external stream ignored.printStackTrace(); } return null; } /** * Converting input to a form accepted by WS * * @return List of FastaSequence records */ static List loadAlignment() { try { return SequenceUtil.readFasta(new ByteArrayInputStream( fastaAlignment.getBytes())); } catch (IOException ignored) { // Should not happen as a source is not a external stream ignored.printStackTrace(); } return null; } private final PrintWriter writer; public WSTester(PrintWriter writer) { this.writer = writer; } /** * Prints usage */ void printUsage() { writer.println("Usage: " + hostkey + pseparator + "host_and_context " + "<" + servicekey + pseparator + "serviceName>"); writer.println(); writer.println(hostkey + pseparator + " - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws"); writer.println(servicekey + pseparator + " - optional if unspecified all services are tested otherwise one of " + Arrays.toString(Services.values())); writer.println(); } /** * Calls alignment with preset * * @param * @param msaws * @param presets * list of the Preset * @throws UnsupportedRuntimeException */ boolean presetAlign(MsaWS msaws, List> presets) throws UnsupportedRuntimeException { boolean succeed = false; for (Preset preset : presets) { writer.print("Aligning with preset '" + preset.getName() + "'... "); Alignment al = null; try { String taskId = msaws.presetAlign(seqs, preset); al = msaws.getResult(taskId); if (al != null) { writer.println(OK); } succeed = true; } catch (UnsupportedRuntimeException e) { writer.println(FAILED); // If executable is not supported than none of the presets are // going to work throw new UnsupportedRuntimeException(e); } catch (JobSubmissionException e) { // TODO custom message writer.println(FAILED); writer.println(); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); continue; } catch (WrongParameterException e) { // TODO custom message writer.println(FAILED); writer.println(); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); continue; } catch (ResultNotAvailableException e) { // TODO custom message writer.println(FAILED); writer.println(); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); continue; } } return succeed; } private boolean testMsaWS(MsaWS msaws) throws UnsupportedRuntimeException { boolean succeed = testDefaultAlignment(msaws); // If exception above is thrown than the tests below is not run PresetManager pmanager = msaws.getPresets(); if (pmanager != null && pmanager.getPresets().size() > 0) { writer.println("Testing alignment with presets:"); List> plist = pmanager.getPresets(); succeed = !succeed ? presetAlign(msaws, plist) : succeed; } testMetadata(msaws); return succeed; } /** * Call most of web services functions and check the output * * @param * web service type * @param msaws * @throws UnsupportedRuntimeException * is thrown if the connection to a web service was made, but * the web service is not functional. e.g. when native * executable does not exists for a server platform */ @SuppressWarnings("unchecked") public boolean checkService(JABAService wservice) { try { if (wservice == null) { throw new NullPointerException( "JABAService instance must be provided!"); } if (wservice instanceof MsaWS) { return testMsaWS((MsaWS) wservice); } else if (wservice instanceof SequenceAnnotation) { return testSequenceAnnotationWS((SequenceAnnotation) wservice); } else { throw new UnsupportedOperationException("The service: " + wservice.getClass() + " is not supported! "); } } catch (UnsupportedRuntimeException e) { writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); return false; } } private boolean testSequenceAnnotationWS(SequenceAnnotation wservice) throws UnsupportedRuntimeException { boolean success = testDefaultAnalyse(loadAlignment(), wservice, null, null); PresetManager presetman = wservice.getPresets(); if (presetman != null) { List> presets = presetman.getPresets(); if (presets != null && !presets.isEmpty()) { Preset preset = presets.get(0); success = testDefaultAnalyse(loadAlignment(), wservice, preset, null); } } testMetadata(wservice); return success; } boolean testDefaultAnalyse(List fastalist, SequenceAnnotation wsproxy, Preset preset, List> customOptions) throws UnsupportedRuntimeException { ScoreManager scores = null; try { String jobId = null; if (customOptions != null) { jobId = wsproxy.customAnalize(fastalist, customOptions); } else if (preset != null) { jobId = wsproxy.presetAnalize(fastalist, preset); } else { jobId = wsproxy.analize(fastalist); } writer.println("\n\ncalling analise........."); Thread.sleep(1000); scores = wsproxy.getAnnotation(jobId); } catch (JobSubmissionException e) { writer.println("Exception while submitting job to a web server. " + "Exception details are below:"); e.printStackTrace(writer); } catch (ResultNotAvailableException e) { writer.println("Exception while waiting for results. " + "Exception details are below:"); e.printStackTrace(writer); } catch (InterruptedException e) { // ignore and propagate an interruption Thread.currentThread().interrupt(); writer.println("Exception while waiting for results. " + "Exception details are below:"); e.printStackTrace(writer); } catch (WrongParameterException e) { writer.println("Exception while parsing the web method input parameters. " + "Exception details are below:"); e.printStackTrace(writer); } return scores != null; } private void testMetadata(Metadata msaws) throws UnsupportedRuntimeException { writer.print("Querying presets..."); PresetManager pmanager = msaws.getPresets(); if (pmanager != null && pmanager.getPresets().size() > 0) { writer.println(OK); } else { writer.println(UNSUPPORTED); } writer.print("Querying Parameters..."); RunnerConfig options = msaws.getRunnerOptions(); if (options != null && options.getArguments().size() > 0) { writer.println(OK); } else { writer.println(UNSUPPORTED); } writer.print("Querying Limits..."); LimitsManager limits = msaws.getLimits(); if (limits != null && limits.getLimits().size() > 0) { writer.println(OK); } else { writer.println(UNSUPPORTED); } writer.print("Querying Local Engine Limits..."); Limit localLimit = msaws .getLimit(PresetManager.LOCAL_ENGINE_LIMIT_PRESET); if (localLimit != null) { writer.println(OK); } else { writer.println(UNSUPPORTED); } } /** * Align using default settings * * @param * @param msaws * @throws UnsupportedRuntimeException */ public boolean testDefaultAlignment(MsaWS msaws) throws UnsupportedRuntimeException { writer.print("Testing alignment with default parameters:"); Alignment al = null; boolean succeed = false; try { String taskId = msaws.align(seqs); writer.print("\nQuerying job status..."); JobStatus status = msaws.getJobStatus(taskId); while (status != JobStatus.FINISHED) { Thread.sleep(1000); status = msaws.getJobStatus(taskId); } writer.println(OK); writer.print("Retrieving results..."); al = msaws.getResult(taskId); succeed = true; } catch (ResultNotAvailableException e) { writer.println(FAILED); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); } catch (JobSubmissionException e) { writer.println(FAILED); writer.println(); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); } catch (InterruptedException e) { writer.println(FAILED); writer.println(); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); } if (al != null) { writer.println(OK); } return succeed; } /** * Connect to a WS using the host and the service name * * @param * @param host * @param service * @return */ JABAService connect(String host, Services service) { JABAService jabaservice = null; try { writer.print("Connecting to service " + service + " on " + host + " ... "); jabaservice = Jws2Client.connect(host, service); writer.println(OK); } catch (WebServiceException e) { writer.println(FAILED); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); } return jabaservice; } /** * Test JWS2 web services * * @param * web service type * @param args * -h= * * -s= which is optional. If service name is not * provided then all known JWS2 web services are tested * @throws IOException */ public static void main(String[] args) throws IOException { WSTester tester = new WSTester(new PrintWriter(System.out, true)); if (args == null || args.length < 1) { tester.printUsage(); System.exit(0); } String host = CmdHelper.getHost(args); String serviceName = CmdHelper.getServiceName(args); if (!Jws2Client.validURL(host)) { tester.writer .println(" parameter is not provided or is incorrect!"); System.exit(1); } boolean succeed = false; MsaWS msaws = null; if (serviceName != null) { Services service = Services.getService(serviceName); if (service == null) { tester.writer.println("Service '" + serviceName + "' is not supported. Valid values are: " + Arrays.toString(Services.values())); tester.writer.println(); tester.printUsage(); System.exit(1); } msaws = (MsaWS) tester.connect(host, service); if (msaws == null) { System.exit(1); } try { succeed = tester.checkService(msaws); } finally { ((Closeable) msaws).close(); } // TODO test results printing! tester.reportResults(service, succeed); System.exit(0); } tester.writer .println(" is not provided checking all known services..."); for (Services serv : Services.values()) { tester.writer.println(); msaws = (MsaWS) tester.connect(host, serv); if (msaws == null) { continue; } try { succeed = tester.checkService(msaws); } finally { ((Closeable) msaws).close(); } tester.reportResults(serv, succeed); } } private void reportResults(Services serv, boolean succeed) { if (succeed) { writer.println("Check is completed. The Service " + serv + " IS WORKING"); } else { writer.println("Check is completed. The Service " + serv + " HAS SOME PROBLEMS"); } } }