Refactoring: rename duplicated Util classes
[jabaws.git] / webservices / compbio / ws / client / WSTester.java
index edf186c..928288c 100644 (file)
@@ -28,9 +28,13 @@ import java.io.Closeable;
 import java.io.IOException;\r
 import java.io.PrintWriter;\r
 import java.net.ConnectException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
 import java.util.Arrays;\r
 import java.util.List;\r
+import java.util.logging.Level;\r
 \r
+import javax.xml.ws.Service;\r
 import javax.xml.ws.WebServiceException;\r
 \r
 import compbio.data.msa.JABAService;\r
@@ -256,7 +260,7 @@ public class WSTester {
 \r
        private <T> boolean testSequenceAnnotationWS(\r
                        SequenceAnnotation<T> wservice, Services service) throws Exception {\r
-               writer.print("Calling analyse.........");\r
+               writer.print("Calling annotation test.........");\r
 \r
                List<FastaSequence> input = loadSeqs(2);\r
                if (service == Services.AAConWS ) {\r
@@ -304,8 +308,7 @@ public class WSTester {
 \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("Exception while waiting for results. Exception details are below:");\r
                writer.println(e.getLocalizedMessage());\r
                e.printStackTrace(writer);\r
        }\r
@@ -429,6 +432,44 @@ public class WSTester {
                }\r
 \r
        }\r
+       \r
+       /**\r
+        * Connects to a web service by the host and the service name web service type\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
+        * @param service\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
+       private JABAService connect(String host, Services service)\r
+                       throws WebServiceException, ConnectException {\r
+               URL url = null;\r
+               System.out.println ("Attempting to connect with " + service.toString() + "...");\r
+               try {\r
+                       url = new URL(host + "/" + service.toString() + "?wsdl");\r
+                       System.out.println ("URL: " + url.toString());\r
+               } catch (MalformedURLException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               Service serv = null;\r
+               try {\r
+                       serv = service.getService(url, service.getServiceNamespace());\r
+               } catch (WebServiceException wse) {\r
+                       wse.printStackTrace();\r
+               }\r
+               if (serv == null) {\r
+                       throw new ConnectException("Could not connect to " + url + ". Is the server down?");\r
+               }\r
+               JABAService srv = service.getInterface(serv);\r
+               System.out.println ("Connected successfully!");\r
+               return srv;\r
+       }\r
 \r
        /**\r
         * Test JABA web service\r
@@ -441,7 +482,7 @@ public class WSTester {
         */\r
        public boolean checkService(Services service) throws ConnectException,\r
                        WebServiceException {\r
-               JABAService ws = Jws2Client.connect(hostname, service);\r
+               JABAService ws = connect(hostname, service);\r
                if (ws == null) {\r
                        String line = "Cannot estabilish the connection to host " + hostname + " with service ";\r
                        writer.println(line + service.toString());\r