Expose a few methods of Client and WSTester as API for Jim, new distributables.
[jabaws.git] / webservices / compbio / ws / client / Jws2Client.java
index 48d107d..d93e43f 100644 (file)
@@ -28,10 +28,13 @@ import java.io.FileInputStream;
 import java.io.IOException;\r
 import java.io.PrintWriter;\r
 import java.io.Writer;\r
+import java.net.ConnectException;\r
 import java.net.MalformedURLException;\r
 import java.net.URL;\r
 import java.util.Arrays;\r
+import java.util.Collections;\r
 import java.util.List;\r
+import java.util.Set;\r
 import java.util.logging.Level;\r
 import java.util.logging.Logger;\r
 \r
@@ -42,6 +45,7 @@ import javax.xml.ws.WebServiceException;
 import compbio.data.msa.JABAService;\r
 import compbio.data.msa.Metadata;\r
 import compbio.data.msa.MsaWS;\r
+import compbio.data.msa.RegistryWS;\r
 import compbio.data.msa.SequenceAnnotation;\r
 import compbio.data.sequence.Alignment;\r
 import compbio.data.sequence.FastaSequence;\r
@@ -53,6 +57,7 @@ import compbio.metadata.Option;
 import compbio.metadata.Preset;\r
 import compbio.metadata.ResultNotAvailableException;\r
 import compbio.metadata.WrongParameterException;\r
+import compbio.util.FileUtil;\r
 \r
 /**\r
  * A command line client for JAva Bioinformatics Analysis Web Services\r
@@ -69,12 +74,6 @@ public class Jws2Client {
        private static final Logger log = Logger.getLogger(Jws2Client.class\r
                        .getCanonicalName());\r
 \r
-       // JABAWS version 1.0 service name\r
-       static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/01/2010/";\r
-\r
-       // JABAWS version 2.0 service name\r
-       static final String V2_QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/";\r
-\r
        /**\r
         * Attempt to construct the URL object from the string\r
         * \r
@@ -115,6 +114,13 @@ public class Jws2Client {
                        System.err.println("Host name is not valid!");\r
                        printUsage(1);\r
                }\r
+               // Just list available services and quit\r
+               boolean listServices = CmdHelper.listServices(cmd);\r
+               if (listServices) {\r
+                       listServices(hostname);\r
+                       System.exit(0);\r
+               }\r
+\r
                String serviceName = CmdHelper.getServiceName(cmd);\r
                if (serviceName == null) {\r
                        System.err.println("Service name is no provided!");\r
@@ -123,10 +129,17 @@ public class Jws2Client {
                Services service = Services.getService(serviceName);\r
                if (service == null) {\r
                        System.err.println("Service " + serviceName\r
-                                       + " is no supported! Valid values are: "\r
+                                       + " is no recognized! Valid values are: "\r
                                        + Arrays.toString(Services.values()));\r
                        printUsage(1);\r
                }\r
+               // Test service and quit\r
+               boolean testService = CmdHelper.testService(cmd);\r
+               if (testService) {\r
+                       testService(hostname, service, new PrintWriter(System.out, true));\r
+                       System.exit(0);\r
+               }\r
+\r
                File inputFile = IOHelper.getFile(cmd, inputkey, true);\r
                File outFile = IOHelper.getFile(cmd, outputkey, false);\r
                File parametersFile = IOHelper.getFile(cmd, paramFile, true);\r
@@ -182,6 +195,50 @@ public class Jws2Client {
                ((Closeable) msaws).close();\r
                log.fine("Disconnected successfully!");\r
        }\r
+\r
+       /**\r
+        * Asks registry to test the service on the host hostname\r
+        * \r
+        * @param hostname\r
+        * @param service\r
+        * @param writer\r
+        * @throws ConnectException\r
+        * @throws WebServiceException\r
+        */\r
+       public static void testService(String hostname, Services service,\r
+                       PrintWriter writer) throws ConnectException, WebServiceException {\r
+               RegistryWS registry = connectToRegistry(hostname);\r
+               if (registry != null) {\r
+                       String message = registry.testService(service);\r
+                       writer.println("Service " + service + " testing results: ");\r
+                       writer.println(message);\r
+                       FileUtil.closeSilently(((Closeable) registry));\r
+               }\r
+               writer.flush();\r
+       }\r
+\r
+       public static Set<Services> getServices(String hostname)\r
+                       throws WebServiceException, ConnectException {\r
+               RegistryWS registry = connectToRegistry(hostname);\r
+               Set<Services> services = Collections.EMPTY_SET;\r
+               if (registry != null) {\r
+                       services = registry.getSupportedServices();\r
+                       FileUtil.closeSilently(((Closeable) registry));\r
+               }\r
+               return services;\r
+       }\r
+\r
+       private static void listServices(String hostname)\r
+                       throws WebServiceException, IOException {\r
+               Set<Services> services = Jws2Client.getServices(hostname);\r
+               if (!services.isEmpty()) {\r
+                       System.out.println("Supported services are: "\r
+                                       + Services.toString(services));\r
+               } else {\r
+                       System.out.println("Failed to connect to the registry! ");\r
+               }\r
+       }\r
+\r
        /**\r
         * Calculate conservation for sequences loaded from the file\r
         * \r
@@ -280,17 +337,22 @@ public class Jws2Client {
                return analize(fastalist, wsproxy, preset, customOptions);\r
        }\r
        /**\r
-        * Connects to a web service by the host and the service name\r
+        * Connects to a web service by the host and the service name web service\r
+        * type\r
         * \r
-        * @param T\r
-        *            web service type\r
         * @param host\r
+        *            the fully qualified name of JABAWS server including JABAWS\r
+        *            context name e.g\r
+        *            http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
         * @param service\r
-        * @return MsaWS<T>\r
+        *            the name of the JABAWS service to connect to\r
+        * @return JABAService<T>\r
         * @throws WebServiceException\r
+        * @throws ConnectException\r
+        *             if fails to connect to the service on the host\r
         */\r
        public static JABAService connect(String host, Services service)\r
-                       throws WebServiceException {\r
+                       throws WebServiceException, ConnectException {\r
                URL url = null;\r
                log.log(Level.FINE, "Attempting to connect...");\r
                try {\r
@@ -301,17 +363,17 @@ public class Jws2Client {
                }\r
                Service serv = null;\r
                try {\r
-                       serv = service.getService(url, QUALIFIED_SERVICE_NAME);\r
+                       serv = service.getService(url, JABAService.SERVICE_NAMESPACE);\r
                } catch (WebServiceException wse) {\r
                        System.out.println("Connecting to JABAWS version 2 service");\r
                        if (isV2service(wse)) {\r
-                               serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME);\r
+                               serv = service\r
+                                               .getService(url, JABAService.V2_SERVICE_NAMESPACE);\r
                        }\r
                }\r
                if (serv == null) {\r
-                       System.err.println("Could not connect to " + url\r
+                       throw new ConnectException("Could not connect to " + url\r
                                        + " the server is down?");\r
-                       // FIXME\r
                }\r
                JABAService serviceIF = service.getInterface(serv);\r
                log.log(Level.INFO, "Connected successfully!");\r
@@ -323,15 +385,28 @@ public class Jws2Client {
                String message = wse.getMessage();\r
                int idx = message.indexOf("not a valid service");\r
                if (idx > 0) {\r
-                       if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) {\r
+                       if (message.substring(idx).contains(\r
+                                       JABAService.V2_SERVICE_NAMESPACE)) {\r
                                return true;\r
                        }\r
                }\r
                return false;\r
        }\r
 \r
+       /**\r
+        * Get a connection of JABAWS registry\r
+        * \r
+        * @param host\r
+        *            the fully qualified name of JABAWS server including JABAWS\r
+        *            context name e.g\r
+        *            http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
+        * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web\r
+        *         service\r
+        * @throws WebServiceException\r
+        * @throws ConnectException\r
+        */\r
        public static compbio.data.msa.RegistryWS connectToRegistry(String host)\r
-                       throws WebServiceException {\r
+                       throws WebServiceException, ConnectException {\r
                URL url = null;\r
                String service = "RegistryWS";\r
                log.log(Level.FINE, "Attempting to connect...");\r
@@ -342,11 +417,11 @@ public class Jws2Client {
                        e.printStackTrace();\r
                        // ignore as the host name is already verified\r
                }\r
-               QName qname = new QName(V2_QUALIFIED_SERVICE_NAME, service);\r
+               QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);\r
                Service serv = Service.create(url, qname);\r
 \r
                if (serv == null) {\r
-                       System.err.println("Could not connect to " + url\r
+                       throw new ConnectException("Could not connect to " + url\r
                                        + " the server is down?");\r
                }\r
 \r