RNAalifoldClient updated to be more like AAConClient and now (attempts) to support...
[jalview.git] / src / jalview / ws / jws2 / JabaWsServerQuery.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /**
19  * 
20  */
21 package jalview.ws.jws2;
22
23 import jalview.bin.Cache;
24 import jalview.ws.jws2.jabaws2.Jws2Instance;
25
26 import java.util.HashSet;
27 import java.util.Set;
28
29 import compbio.data.msa.Category;
30 import compbio.data.msa.JABAService;
31 import compbio.ws.client.Jws2Client;
32 import compbio.ws.client.Services;
33
34 /**
35  * @author JimP
36  * 
37  */
38 public class JabaWsServerQuery implements Runnable
39 {
40
41   Jws2Discoverer jws2Discoverer = null;
42
43   String jwsservers = null;
44
45   boolean quit = false, running = false;
46
47   /**
48    * @return the running
49    */
50   public boolean isRunning()
51   {
52     return running;
53   }
54
55   /**
56    * @param quit
57    *          the quit to set
58    */
59   public void setQuit(boolean quit)
60   {
61     this.quit = quit;
62   }
63
64   public JabaWsServerQuery(Jws2Discoverer jws2Discoverer, String jwsservers)
65   {
66     this.jws2Discoverer = jws2Discoverer;
67     this.jwsservers = jwsservers;
68   }
69
70   Services[] JABAWS1SERVERS = new Services[]
71   { Services.ClustalWS, Services.MuscleWS, Services.MafftWS,
72       Services.ProbconsWS, Services.TcoffeeWS };
73
74   Services[] JABAWS2SERVERS = new Services[]
75   { Services.ClustalWS, Services.MuscleWS, Services.MafftWS,
76       Services.ProbconsWS, Services.TcoffeeWS, Services.AAConWS,
77       Services.DisemblWS, Services.GlobPlotWS, Services.IUPredWS,
78       Services.JronnWS, Services.RNAalifoldWS };
79
80   /*
81    * (non-Javadoc)
82    * 
83    * @see java.lang.Runnable#run()
84    */
85   @Override
86   public void run()
87   {
88     running = true;
89     try
90     {
91       if (Jws2Client.validURL(jwsservers))
92       {
93         compbio.data.msa.RegistryWS registry = null;
94         Set svccategories = null;
95         boolean noservices = true;
96         // look for services
97         boolean jabasws2 = false;
98         // If we are dealing with a JABAWS2 service, then just go and ask the
99         // JABAWS 2 service registry
100         Set<Services> srv_set = new HashSet<Services>();
101
102         Set<Category> categories = Category.getCategories();
103         String svc_cat;
104
105         try
106         {
107           // JBPNote: why is RegistryWS in compbio.data.msa ?
108           registry = Jws2Client.connectToRegistry(jwsservers);
109           if (registry != null)
110           {
111             // System.err.println("Test Services Output\n"
112             // + registry.testAllServices());
113             // TODO: enumerate services and test those that haven't been tested
114             // in the last n-days/hours/etc.
115
116             jabasws2 = true;
117             srv_set = registry.getSupportedServices();
118             
119             // dan test
120             System.out.println("registry.getSupportedServices: " + srv_set.toString());
121             
122             svccategories = registry.getServiceCategories();
123             
124             // dan test
125 //            System.out.println("registry.getServiceCategories: " + svccategories.toString());
126
127           }
128         } catch (Exception ex)
129         {
130           System.err.println("Exception whilst trying to get at registry:");
131           ex.printStackTrace();
132           // if that failed, then we are probably working with a JABAWS1 server.
133           // in that case, look for each service endpoint
134           System.err.println("JWS2 Discoverer: " + jwsservers
135                   + " is a JABAWS1 server. Using hardwired list.");
136           for (Services srv : JABAWS1SERVERS)
137           {
138             srv_set.add(srv);
139           }
140         }
141         for (Category cat : categories)
142         {
143           for (Services srv : cat.getServices())
144           {
145             if (quit)
146             {
147               running = false;
148               return;
149             }
150             if (!srv_set.contains(srv))
151             {
152               continue;
153             }
154             JABAService service = null;
155             try
156             {
157               service = Jws2Client.connect(jwsservers, srv);
158             } catch (Exception e)
159             {
160               System.err.println("Jws2 Discoverer: Problem on "
161                       + jwsservers + " with service " + srv + ":\n"
162                       + e.getMessage());
163               if (!(e instanceof javax.xml.ws.WebServiceException))
164               {
165                 e.printStackTrace();
166               }
167               // For moment, report service as a problem.
168               jws2Discoverer.addInvalidServiceUrl(jwsservers);
169             }
170             ;
171             if (service != null)
172             {
173               noservices = false;
174               Jws2Instance svc = null;
175               if (registry != null)
176               {
177
178                 String description = registry.getServiceDescription(srv);
179
180                 svc = new Jws2Instance(jwsservers, srv.toString(),
181                         cat.name, description, service);
182               }
183               if (svc == null)
184               {
185                 svc = new Jws2Instance(jwsservers, srv.toString(),
186                         cat.name, "JABAWS 1 Alignment Service", service);
187               }
188               jws2Discoverer.addService(jwsservers, svc);
189             }
190
191           }
192         }
193
194         if (noservices)
195         {
196           jws2Discoverer.addUrlwithnoservices(jwsservers);
197         }
198       }
199       else
200       {
201         jws2Discoverer.addInvalidServiceUrl(jwsservers);
202         Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
203       }
204     } catch (Exception e)
205     {
206       e.printStackTrace();
207       Cache.log.warn("Exception when discovering Jws2 services.", e);
208       jws2Discoverer.addInvalidServiceUrl(jwsservers);
209     } catch (Error e)
210     {
211       Cache.log.error("Exception when discovering Jws2 services.", e);
212       jws2Discoverer.addInvalidServiceUrl(jwsservers);
213     }
214     running = false;
215   }
216
217 }