Adding registry web service and changes to WStester and JWS2Client code. Bugs in...
[jabaws.git] / webservices / compbio / ws / client / Jws2Client.java
index 1673e18..ceb9501 100644 (file)
@@ -38,20 +38,20 @@ import java.net.MalformedURLException;
 import java.net.URL;\r
 import java.util.Arrays;\r
 import java.util.List;\r
-import java.util.Set;\r
 import java.util.logging.Level;\r
 import java.util.logging.Logger;\r
 \r
+import javax.xml.namespace.QName;\r
 import javax.xml.ws.Service;\r
 import javax.xml.ws.WebServiceException;\r
 \r
-import compbio.data.msa.Annotation;\r
 import compbio.data.msa.JABAService;\r
 import compbio.data.msa.Metadata;\r
 import compbio.data.msa.MsaWS;\r
+import compbio.data.msa.SequenceAnnotation;\r
 import compbio.data.sequence.Alignment;\r
 import compbio.data.sequence.FastaSequence;\r
-import compbio.data.sequence.Score;\r
+import compbio.data.sequence.ScoreManager;\r
 import compbio.data.sequence.SequenceUtil;\r
 import compbio.data.sequence.UnknownFileFormatException;\r
 import compbio.metadata.JobSubmissionException;\r
@@ -69,8 +69,8 @@ import compbio.metadata.WrongParameterException;
 public class Jws2Client {\r
 \r
        /*\r
-        * Use java.util.Logger instead of log4j logger to reduce the size of the\r
-        * client package\r
+        * TODO Use java.util.Logger instead of log4j logger to reduce the size of\r
+        * the client package\r
         */\r
        private static final Logger log = Logger.getLogger(Jws2Client.class\r
                        .getCanonicalName());\r
@@ -159,17 +159,17 @@ public class Jws2Client {
                                // std.out will not be\r
                                outStream = System.out;\r
                        }\r
-                       if (service == Services.AAConWS) {\r
-                               Set<Score> result = analize(inputFile, ((Annotation<T>) msaws),\r
-                                               preset, customOptions);\r
+                       if (service.getServiceType() == SequenceAnnotation.class) {\r
+                               ScoreManager result = analize(inputFile,\r
+                                               ((SequenceAnnotation<T>) msaws), preset, customOptions);\r
+\r
                                IOHelper.writeOut(outStream, result);\r
                        } else {\r
                                alignment = align(inputFile, (MsaWS<T>) msaws, preset,\r
                                                customOptions);\r
                                IOHelper.writeOut(outStream, alignment);\r
                        }\r
-\r
-                       // stream is closed in the method no need to close it here\r
+                       outStream.close();\r
                }\r
 \r
                boolean listParameters = CmdHelper.listParameters(cmd);\r
@@ -202,14 +202,12 @@ public class Jws2Client {
         * @return Set<Score> the conservation scores\r
         * @throws UnknownFileFormatException\r
         */\r
-       <T> Set<Score> analize(File file, Annotation<T> wsproxy, Preset<T> preset,\r
+       static <T> ScoreManager analize(List<FastaSequence> fastalist,\r
+                       SequenceAnnotation<T> wsproxy, Preset<T> preset,\r
                        List<Option<T>> customOptions) {\r
 \r
-               List<FastaSequence> fastalist = null;\r
-               Set<Score> scores = null;\r
+               ScoreManager scores = null;\r
                try {\r
-                       fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());\r
-\r
                        String jobId = null;\r
                        if (customOptions != null && preset != null) {\r
                                System.out\r
@@ -222,15 +220,10 @@ public class Jws2Client {
                        } else {\r
                                jobId = wsproxy.analize(fastalist);\r
                        }\r
+                       System.out.println("\n\ncalling analise.........");\r
                        Thread.sleep(1000);\r
                        scores = wsproxy.getAnnotation(jobId);\r
 \r
-               } catch (IOException e) {\r
-                       System.err\r
-                                       .println("Exception while reading the input file. "\r
-                                                       + "Check that the input file contains a list of fasta formatted sequences! "\r
-                                                       + "Exception details are below:");\r
-                       e.printStackTrace();\r
                } catch (JobSubmissionException e) {\r
                        System.err\r
                                        .println("Exception while submitting job to a web server. "\r
@@ -240,14 +233,49 @@ public class Jws2Client {
                        System.err.println("Exception while waiting for results. "\r
                                        + "Exception details are below:");\r
                        e.printStackTrace();\r
-               } catch (InterruptedException ignored) {\r
+               } catch (InterruptedException e) {\r
                        // ignore and propagate an interruption\r
                        Thread.currentThread().interrupt();\r
+                       System.err.println("Exception while waiting for results. "\r
+                                       + "Exception details are below:");\r
+                       e.printStackTrace();\r
                } catch (WrongParameterException e) {\r
                        System.err\r
                                        .println("Exception while parsing the web method input parameters. "\r
                                                        + "Exception details are below:");\r
                        e.printStackTrace();\r
+               }\r
+               return scores;\r
+\r
+       }\r
+\r
+       /**\r
+        * Calculate conservation for sequences loaded from the file\r
+        * \r
+        * @param wsproxy\r
+        *            a web service proxy\r
+        * @param file\r
+        *            the file to read the results from\r
+        * @param preset\r
+        *            Preset to use optional\r
+        * @param customOptions\r
+        *            the list of options\r
+        * @return Set<Score> the conservation scores\r
+        * @throws IOException\r
+        * @throws UnknownFileFormatException\r
+        */\r
+       static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,\r
+                       Preset<T> preset, List<Option<T>> customOptions) {\r
+               List<FastaSequence> fastalist = null;\r
+               try {\r
+                       fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());\r
+                       assert !fastalist.isEmpty() : "Input is empty!";\r
+               } catch (IOException e) {\r
+                       System.err\r
+                                       .println("Exception while reading the input file. "\r
+                                                       + "Check that the input file contains a list of fasta formatted sequences! "\r
+                                                       + "Exception details are below:");\r
+                       e.printStackTrace();\r
                } catch (UnknownFileFormatException e) {\r
                        System.err\r
                                        .println("Exception while attempting to read the input file "\r
@@ -255,9 +283,8 @@ public class Jws2Client {
                        System.out.println(e.getMessage());\r
                        e.printStackTrace();\r
                }\r
-               return scores;\r
+               return analize(fastalist, wsproxy, preset, customOptions);\r
        }\r
-\r
        /**\r
         * Connects to a web service by the host and the service name\r
         * \r
@@ -282,13 +309,18 @@ public class Jws2Client {
                try {\r
                        serv = service.getService(url, QUALIFIED_SERVICE_NAME);\r
                } catch (WebServiceException wse) {\r
-                       System.out.println("Conecting to JABAWS version 2 service");\r
+                       System.out.println("Connecting to JABAWS version 2 service");\r
                        if (isV2service(wse)) {\r
                                serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME);\r
                        }\r
                }\r
+               if (serv == null) {\r
+                       System.err.println("Could not connect to " + url\r
+                                       + " the server is down?");\r
+                       // FIXME\r
+               }\r
                JABAService serviceIF = service.getInterface(serv);\r
-               log.log(Level.FINE, "Connected successfully!");\r
+               log.log(Level.INFO, "Connected successfully!");\r
 \r
                return serviceIF;\r
        }\r
@@ -304,6 +336,36 @@ public class Jws2Client {
                return false;\r
        }\r
 \r
+       public static compbio.data.msa.RegistryWS connectToRegistry(String host)\r
+                       throws WebServiceException {\r
+               URL url = null;\r
+               String service = "RegistryWS";\r
+               log.log(Level.FINE, "Attempting to connect...");\r
+\r
+               try {\r
+                       url = new URL(host + "/" + service + "?wsdl");\r
+               } catch (MalformedURLException e) {\r
+                       e.printStackTrace();\r
+                       // ignore as the host name is already verified\r
+               }\r
+               QName qname = new QName(V2_QUALIFIED_SERVICE_NAME, service);\r
+               Service serv = Service.create(url, qname);\r
+\r
+               if (serv == null) {\r
+                       System.err.println("Could not connect to " + url\r
+                                       + " the server is down?");\r
+               }\r
+\r
+               QName portName = new QName(serv.getServiceName().getNamespaceURI(),\r
+                               service + "Port");\r
+               compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,\r
+                               compbio.data.msa.RegistryWS.class);\r
+\r
+               log.log(Level.INFO, "Connected to " + service + " successfully!");\r
+\r
+               return serviceIF;\r
+       }\r
+\r
        /**\r
         * Align sequences from the file using MsaWS\r
         * \r
@@ -340,6 +402,7 @@ public class Jws2Client {
                        } else {\r
                                jobId = msaws.align(fastalist);\r
                        }\r
+                       System.out.println("\n\ncalling align.........");\r
                        Thread.sleep(1000);\r
                        alignment = msaws.getResult(jobId);\r
 \r
@@ -426,7 +489,7 @@ public class Jws2Client {
 \r
        /**\r
         * Starts command line client, if no parameter are supported print help. Two\r
-        * parameters are required for successfull call the JWS2 host name and a\r
+        * parameters are required for successful call the JWS2 host name and a\r
         * service name.\r
         * \r
         * @param args\r