Expose a few methods of Client and WSTester as API for Jim, new distributables.
[jabaws.git] / webservices / compbio / ws / client / Jws2Client.java
index e775727..d93e43f 100644 (file)
@@ -32,6 +32,7 @@ import java.net.ConnectException;
 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
@@ -56,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
@@ -134,7 +136,7 @@ public class Jws2Client {
                // Test service and quit\r
                boolean testService = CmdHelper.testService(cmd);\r
                if (testService) {\r
-                       testService(hostname, service);\r
+                       testService(hostname, service, new PrintWriter(System.out, true));\r
                        System.exit(0);\r
                }\r
 \r
@@ -194,26 +196,44 @@ public class Jws2Client {
                log.fine("Disconnected successfully!");\r
        }\r
 \r
-       // TODO make public?\r
-       private void testService(String hostname, Services service)\r
-                       throws IOException {\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
-                       System.out.println("Service " + service + " testing results: ");\r
-                       System.out.println(message);\r
-                       ((Closeable) registry).close();\r
+                       writer.println("Service " + service + " testing results: ");\r
+                       writer.println(message);\r
+                       FileUtil.closeSilently(((Closeable) registry));\r
                }\r
+               writer.flush();\r
        }\r
 \r
-       // TODO make public?\r
-       private void listServices(String hostname) throws IOException {\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
-                       Set<Services> services = registry.getSupportedServices();\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
-                       ((Closeable) registry).close();\r
                } else {\r
                        System.out.println("Failed to connect to the registry! ");\r
                }\r
@@ -383,9 +403,10 @@ public class Jws2Client {
         * @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
@@ -400,7 +421,7 @@ public class Jws2Client {
                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