From 804613aebb1aba7ef76a4766c0b16698c35e6795 Mon Sep 17 00:00:00 2001 From: pvtroshin Date: Tue, 7 Jun 2011 16:47:05 +0000 Subject: [PATCH] further work on registry & WSTester nearly there git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@4229 e3abac25-378b-4346-85de-24260fe3988d --- testsrc/compbio/metadata/AllTestSuit.java | 1 + testsrc/compbio/ws/client/WSTesterTester.java | 24 ++-- testsrc/compbio/ws/server/RegistryWSTester.java | 53 +++++++- webservices/compbio/data/msa/RegistryWS.java | 48 +++++++ webservices/compbio/ws/client/WSTester.java | 158 ++++++++++------------- webservices/compbio/ws/server/RegistryWS.java | 79 +++++++----- 6 files changed, 218 insertions(+), 145 deletions(-) diff --git a/testsrc/compbio/metadata/AllTestSuit.java b/testsrc/compbio/metadata/AllTestSuit.java index c359d28..4ae260e 100644 --- a/testsrc/compbio/metadata/AllTestSuit.java +++ b/testsrc/compbio/metadata/AllTestSuit.java @@ -52,6 +52,7 @@ public class AllTestSuit { public final static String test_group_cluster = "cluster"; public final static String test_group_runner = "runner"; public final static String test_group_non_windows = "non_windows"; + public final static String test_group_windows_only = "windows_only"; public final static String test_group_engine = "engine"; public final static String test_group_long = "performance"; diff --git a/testsrc/compbio/ws/client/WSTesterTester.java b/testsrc/compbio/ws/client/WSTesterTester.java index 43204e5..5fe0a35 100644 --- a/testsrc/compbio/ws/client/WSTesterTester.java +++ b/testsrc/compbio/ws/client/WSTesterTester.java @@ -6,7 +6,6 @@ import java.io.PrintWriter; import org.testng.annotations.Test; -import compbio.data.msa.JABAService; import compbio.metadata.AllTestSuit; public class WSTesterTester { @@ -15,18 +14,13 @@ public class WSTesterTester { @Test(groups = {AllTestSuit.test_group_runner}) public void testAllWindowsWS() { - WSTester tester = new WSTester(new PrintWriter(System.out, true)); - JABAService ws = Jws2Client.connect(SERVER, Services.AAConWS); - assertTrue(tester.checkService(ws)); + WSTester tester = new WSTester(SERVER, + new PrintWriter(System.out, true)); - ws = Jws2Client.connect(SERVER, Services.JronnWS); - assertTrue(tester.checkService(ws)); - - ws = Jws2Client.connect(SERVER, Services.ClustalWS); - assertTrue(tester.checkService(ws)); - - ws = Jws2Client.connect(SERVER, Services.MuscleWS); - assertTrue(tester.checkService(ws)); + assertTrue(tester.checkService(Services.AAConWS)); + assertTrue(tester.checkService(Services.JronnWS)); + assertTrue(tester.checkService(Services.ClustalWS)); + assertTrue(tester.checkService(Services.MuscleWS)); // Will throw UnsupportedRuntimeException on windows // ws = Jws2Client.connect(SERVER, Services.MafftWS); @@ -36,11 +30,9 @@ public class WSTesterTester { @Test(groups = {AllTestSuit.test_group_non_windows}) public void testAllWS() { - WSTester tester = new WSTester(new PrintWriter(System.out)); - + WSTester tester = new WSTester(SERVER, new PrintWriter(System.out)); for (Services service : Services.values()) { - JABAService ws = Jws2Client.connect(SERVER, service); - assertTrue(tester.checkService(ws)); + assertTrue(tester.checkService(service)); } } diff --git a/testsrc/compbio/ws/server/RegistryWSTester.java b/testsrc/compbio/ws/server/RegistryWSTester.java index 4093626..91ee328 100644 --- a/testsrc/compbio/ws/server/RegistryWSTester.java +++ b/testsrc/compbio/ws/server/RegistryWSTester.java @@ -1,25 +1,68 @@ package compbio.ws.server; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.util.EnumSet; + import org.testng.annotations.Test; +import compbio.metadata.AllTestSuit; import compbio.ws.client.Jws2Client; import compbio.ws.client.Services; import compbio.ws.client.WSTesterTester; public class RegistryWSTester { - @Test + @Test(groups = {AllTestSuit.test_group_windows_only}) public void testGetSupportedServices() { compbio.data.msa.RegistryWS reg = Jws2Client .connectToRegistry(WSTesterTester.SERVER); - System.out.println(reg.getSupportedServices()); + assertEquals(EnumSet.of(Services.AAConWS, Services.JronnWS, + Services.MuscleWS, Services.ClustalWS), + reg.getSupportedServices()); } - - @Test + @Test() public void testTestService() { compbio.data.msa.RegistryWS reg = Jws2Client .connectToRegistry(WSTesterTester.SERVER); - System.out.println(reg.testService(Services.AAConWS)); + assertNotNull(reg.testService(Services.AAConWS)); + } + + @Test(dependsOnMethods = {"testTestService"}) + public void testIsOperating() { + compbio.data.msa.RegistryWS reg = Jws2Client + .connectToRegistry(WSTesterTester.SERVER); + assertTrue(reg.isOperating(Services.AAConWS)); + } + + @Test(dependsOnMethods = {"testTestService"}) + public void testGetLastTestedOn() { + compbio.data.msa.RegistryWS reg = Jws2Client + .connectToRegistry(WSTesterTester.SERVER); + assertNotNull(reg.getLastTestedOn(Services.AAConWS)); } + @Test(dependsOnMethods = {"testTestService"}) + public void testGetLastTested() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + compbio.data.msa.RegistryWS reg = Jws2Client + .connectToRegistry(WSTesterTester.SERVER); + System.out.println(reg.getLastTested(Services.AAConWS)); + assertTrue(reg.getLastTested(Services.AAConWS) > 0); + } + + @Test() + public void testTestAllServices() { + compbio.data.msa.RegistryWS reg = Jws2Client + .connectToRegistry(WSTesterTester.SERVER); + System.out.println(reg.testAllServices()); + } } diff --git a/webservices/compbio/data/msa/RegistryWS.java b/webservices/compbio/data/msa/RegistryWS.java index 3dd89b5..9f55ff9 100644 --- a/webservices/compbio/data/msa/RegistryWS.java +++ b/webservices/compbio/data/msa/RegistryWS.java @@ -7,12 +7,60 @@ import javax.jws.WebService; import compbio.ws.client.Services; +/** + * JABAWS services registry + * + * @author pvtroshin + * @version 1.0 June 2011 + */ @WebService(targetNamespace = "http://msa.data.compbio/01/12/2010/") public interface RegistryWS extends JABAService { + + /** + * List of services that are functioning on the server. This function + * returns the results of testing performed some time ago by + * {@link #testAllServices} or {@link #testService(Services)} methods. The + * time of last check can be obtained from + * {@link #getLastTestedOn(Services)} method + * + * @return the Set of Services which are functioning on the server + * @see #testAllServices() + */ Set getSupportedServices(); + /** + * Number of seconds since the last test. Returns 0 if the service was not + * tested or tested less then a one second ago. + * + * @param service + * @return + */ + int getLastTested(Services service); + /** + * The date and time the service has been verified to work last time + * + * @param service + * @return the Date and time on which the service was last tested + */ Date getLastTestedOn(Services service); + /** + * Test all JABAWS services on the server + * + * @return the test log + */ String testAllServices(); + /** + * Test a particular service + * + * @param service + * @return the testing log + */ String testService(Services service); + /** + * Check whether a particular web service is working on this server + * + * @param service + * @return true if the service was functioning in time of last testing. + */ boolean isOperating(Services service); } diff --git a/webservices/compbio/ws/client/WSTester.java b/webservices/compbio/ws/client/WSTester.java index 05bd0b8..a0e635b 100644 --- a/webservices/compbio/ws/client/WSTester.java +++ b/webservices/compbio/ws/client/WSTester.java @@ -29,8 +29,6 @@ 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; @@ -52,6 +50,8 @@ import compbio.metadata.ResultNotAvailableException; import compbio.metadata.RunnerConfig; import compbio.metadata.UnsupportedRuntimeException; import compbio.metadata.WrongParameterException; +import compbio.util.FileUtil; +import compbio.util.Util; /** * Class for testing web services @@ -89,7 +89,7 @@ public class WSTester { * * @return List of FastaSequence records */ - static List loadSeqs() { + private static List loadSeqs() { try { return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput .getBytes())); @@ -105,7 +105,7 @@ public class WSTester { * * @return List of FastaSequence records */ - static List loadAlignment() { + private static List loadAlignment() { try { return SequenceUtil.readFasta(new ByteArrayInputStream( fastaAlignment.getBytes())); @@ -117,27 +117,34 @@ public class WSTester { } private final PrintWriter writer; + private final String hostname; - public WSTester(PrintWriter writer) { + public WSTester(String hostname, PrintWriter writer) { + if (Util.isEmpty(hostname)) { + throw new NullPointerException("Hostname must be provided!"); + } + this.hostname = hostname; this.writer = writer; } /** * Prints usage */ - void printUsage() { - writer.println("Usage: " + hostkey + static void printUsage() { + System.out.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(); + System.out.println(); + System.out + .println(hostkey + + pseparator + + " - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws"); + System.out + .println(servicekey + + pseparator + + " - optional if unspecified all services are tested otherwise one of " + + Arrays.toString(Services.values())); + System.out.println(); } @@ -150,7 +157,7 @@ public class WSTester { * list of the Preset * @throws UnsupportedRuntimeException */ - boolean presetAlign(MsaWS msaws, List> presets) + private boolean presetAlign(MsaWS msaws, List> presets) throws UnsupportedRuntimeException { boolean succeed = false; for (Preset preset : presets) { @@ -196,9 +203,9 @@ public class WSTester { private boolean testMsaWS(MsaWS msaws) throws UnsupportedRuntimeException { + assert msaws != null; boolean succeed = testDefaultAlignment(msaws); - // If exception above is thrown than the tests below is not run PresetManager pmanager = msaws.getPresets(); @@ -222,12 +229,13 @@ public class WSTester { * executable does not exists for a server platform */ @SuppressWarnings("unchecked") - public boolean checkService(JABAService wservice) { + private 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) { @@ -245,13 +253,16 @@ public class WSTester { private boolean testSequenceAnnotationWS(SequenceAnnotation wservice) throws UnsupportedRuntimeException { + writer.println("Calling analyse........."); 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); + writer.println("Calling analyse with Preset........."); success = testDefaultAnalyse(loadAlignment(), wservice, preset, null); } @@ -260,7 +271,7 @@ public class WSTester { return success; } - boolean testDefaultAnalyse(List fastalist, + private boolean testDefaultAnalyse(List fastalist, SequenceAnnotation wsproxy, Preset preset, List> customOptions) throws UnsupportedRuntimeException { @@ -274,7 +285,6 @@ public class WSTester { } else { jobId = wsproxy.analize(fastalist); } - writer.println("\n\ncalling analise........."); Thread.sleep(1000); scores = wsproxy.getAnnotation(jobId); @@ -344,7 +354,7 @@ public class WSTester { * @param msaws * @throws UnsupportedRuntimeException */ - public boolean testDefaultAlignment(MsaWS msaws) + private boolean testDefaultAlignment(MsaWS msaws) throws UnsupportedRuntimeException { writer.print("Testing alignment with default parameters:"); Alignment al = null; @@ -361,20 +371,14 @@ public class WSTester { 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); + } catch (Exception e) { + if (e instanceof UnsupportedRuntimeException) { + throw (UnsupportedRuntimeException) e; + } else { + writer.println(FAILED); + writer.println(e.getLocalizedMessage()); + e.printStackTrace(writer); + } } if (al != null) { writer.println(OK); @@ -383,29 +387,6 @@ public class WSTester { } /** - * 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 @@ -419,20 +400,20 @@ public class WSTester { * @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(); + WSTester.printUsage(); System.exit(0); } String host = CmdHelper.getHost(args); String serviceName = CmdHelper.getServiceName(args); if (!Jws2Client.validURL(host)) { - tester.writer + System.err .println(" parameter is not provided or is incorrect!"); System.exit(1); } - boolean succeed = false; - MsaWS msaws = null; + WSTester tester = new WSTester(host, new PrintWriter(System.out, true)); + if (serviceName != null) { Services service = Services.getService(serviceName); if (service == null) { @@ -440,21 +421,10 @@ public class WSTester { + "' 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) { + printUsage(); System.exit(1); } - try { - succeed = tester.checkService(msaws); - } finally { - ((Closeable) msaws).close(); - } - // TODO test results printing! - tester.reportResults(service, succeed); + tester.checkService(service); System.exit(0); } @@ -463,26 +433,36 @@ public class WSTester { 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); + tester.checkService(serv); } } + + public boolean checkService(Services service) { + JABAService ws = Jws2Client.connect(hostname, service); + if (ws == null) { + writer.println("Cannot estabilish the connection to host " + + hostname + " with service " + service.toString()); + return false; + } + boolean succeed = false; + try { + writer.println("Checking service " + service.toString()); + succeed = checkService(ws); + } finally { + FileUtil.closeSilently(((Closeable) ws)); + } + reportResults(service, succeed); + return succeed; + } + private void reportResults(Services serv, boolean succeed) { if (succeed) { writer.println("Check is completed. The Service " + serv - + " IS WORKING"); + + " IS WORKING\n"); } else { writer.println("Check is completed. The Service " + serv - + " HAS SOME PROBLEMS"); + + " HAS SOME PROBLEMS\n"); } } } diff --git a/webservices/compbio/ws/server/RegistryWS.java b/webservices/compbio/ws/server/RegistryWS.java index 869807b..dda265e 100644 --- a/webservices/compbio/ws/server/RegistryWS.java +++ b/webservices/compbio/ws/server/RegistryWS.java @@ -17,12 +17,11 @@ import javax.xml.ws.handler.MessageContext; import org.apache.log4j.Logger; import compbio.data.msa.JABAService; -import compbio.ws.client.Jws2Client; import compbio.ws.client.Services; import compbio.ws.client.WSTester; /** - * TODO + * JABAWS services registry * * @author pvtroshin * @@ -34,20 +33,10 @@ public class RegistryWS implements compbio.data.msa.RegistryWS, JABAService { @Resource WebServiceContext wsContext; - private static Logger statLog = Logger.getLogger("RegistryWS-stats"); private static Logger log = Logger.getLogger(RegistryWS.class); private final static Map operating = new ConcurrentHashMap(); - /** - * 1) Check that the actual executable is configured - * - * 2) Check whether the service is enabled? - * - * 3) Attempt to execute? - * - * @return - */ @Override public Set getSupportedServices() { init(); @@ -55,28 +44,55 @@ public class RegistryWS implements compbio.data.msa.RegistryWS, JABAService { } private void init() { - // This can be run concurrently + // This should not be run concurrently if (operating.isEmpty()) { - testAllServices(); + synchronized (operating) { + if (operating.isEmpty()) { + testAllServices(); + } + } } } + + @Override + public int getLastTested(Services service) { + Date testedOn = getLastTestedOn(service); + if (testedOn != null) { + return (int) ((System.currentTimeMillis() - testedOn.getTime()) / 1000); + } + return 0; + } + + /** + * Can potentially return null if the service has not been tested yet. + */ @Override public Date getLastTestedOn(Services service) { - return operating.get(service); + if (operating.containsKey(service)) { + return operating.get(service); + } + return null; } + /** + * TODO improve reporting. stop testing service on unsupported runtime env + * exception + */ @Override public String testAllServices() { Writer testlog = new StringWriter(); PrintWriter writer = new PrintWriter(testlog, true); - WSTester tester = new WSTester(writer); - - for (Services service : Services.values()) { - JABAService ws = Jws2Client.connect(getServicePath(), service); - if (tester.checkService(ws)) { - operating.put(service, new Date()); + WSTester tester = new WSTester(getServicePath(), writer); + // This is done deliberately to prevent malicious user from overloading + // the server + synchronized (operating) { + for (Services service : Services.values()) { + if (tester.checkService(service)) { + operating.put(service, new Date()); + } } } + writer.close(); return testlog.toString(); } @@ -96,27 +112,20 @@ public class RegistryWS implements compbio.data.msa.RegistryWS, JABAService { String server = getServicePath(); Writer testlog = new StringWriter(); PrintWriter writer = new PrintWriter(testlog, true); - WSTester tester = new WSTester(writer); - writer.println("Attempting to connect to the service " + service - + " on the server " + server + "..."); - JABAService ws = Jws2Client.connect(server, service); - writer.println("Connected successfully!"); + WSTester tester = new WSTester(server, writer); try { - boolean succeed = tester.checkService(ws); - if (succeed) { - // TODO extract messages (see WSTester main) - writer.println("Check is completed. The Service " - + service.toString() + " IS WORKING"); - operating.put(service, new Date()); - } else { - writer.println("Check is completed. The Service " - + service.toString() + " HAS SOME PROBLEMS"); + synchronized (operating) { + boolean succeed = tester.checkService(service); + if (succeed) { + operating.put(service, new Date()); + } } } finally { writer.close(); } return testlog.toString(); } + @Override public boolean isOperating(Services service) { init(); -- 1.7.10.2