JAL-1365 use AlignAnalysisUIText class to provide metadata for a service's UI elements
[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 import jalview.ws.jws2.jabaws2.Jws2InstanceFactory;
26
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import compbio.data.msa.Category;
31 import compbio.data.msa.JABAService;
32 import compbio.ws.client.Jws2Client;
33 import compbio.ws.client.Services;
34
35 /**
36  * @author JimP
37  * 
38  */
39 public class JabaWsServerQuery implements Runnable
40 {
41
42   Jws2Discoverer jws2Discoverer = null;
43
44   String jwsservers = null;
45
46   boolean quit = false, running = false;
47
48   /**
49    * @return the running
50    */
51   public boolean isRunning()
52   {
53     return running;
54   }
55
56   /**
57    * @param quit
58    *          the quit to set
59    */
60   public void setQuit(boolean quit)
61   {
62     this.quit = quit;
63   }
64
65   public JabaWsServerQuery(Jws2Discoverer jws2Discoverer, String jwsservers)
66   {
67     this.jws2Discoverer = jws2Discoverer;
68     this.jwsservers = jwsservers;
69   }
70
71   Services[] JABAWS1SERVERS = new Services[]
72   { Services.ClustalWS, Services.MuscleWS, Services.MafftWS,
73       Services.ProbconsWS, Services.TcoffeeWS };
74
75   Services[] JABAWS2SERVERS = new Services[]
76   { Services.ClustalWS, Services.MuscleWS, Services.MafftWS,
77       Services.ProbconsWS, Services.TcoffeeWS, Services.AAConWS,
78       Services.DisemblWS, Services.GlobPlotWS, Services.IUPredWS,
79       Services.JronnWS, Services.RNAalifoldWS };
80
81   /*
82    * (non-Javadoc)
83    * 
84    * @see java.lang.Runnable#run()
85    */
86   @Override
87   public void run()
88   {
89     running = true;
90     try
91     {
92       if (Jws2Client.validURL(jwsservers))
93       {
94         compbio.data.msa.RegistryWS registry = null;
95         Set svccategories = null;
96         boolean noservices = true;
97         // look for services
98         boolean jabasws2 = false;
99         // If we are dealing with a JABAWS2 service, then just go and ask the
100         // JABAWS 2 service registry
101         Set<Services> srv_set = new HashSet<Services>();
102
103         Set<Category> categories = Category.getCategories();
104         String svc_cat;
105
106         try
107         {
108           // JBPNote: why is RegistryWS in compbio.data.msa ?
109           registry = Jws2Client.connectToRegistry(jwsservers);
110           if (registry != null)
111           {
112             // System.err.println("Test Services Output\n"
113             // + registry.testAllServices());
114             // TODO: enumerate services and test those that haven't been tested
115             // in the last n-days/hours/etc.
116
117             jabasws2 = true;
118             srv_set = registry.getSupportedServices();
119             
120             // dan test
121             System.out.println("registry.getSupportedServices: " + srv_set.toString());
122             
123             svccategories = registry.getServiceCategories();
124             
125             // dan test
126 //            System.out.println("registry.getServiceCategories: " + svccategories.toString());
127
128           }
129         } catch (Exception ex)
130         {
131           System.err.println("Exception whilst trying to get at registry:");
132           ex.printStackTrace();
133           // if that failed, then we are probably working with a JABAWS1 server.
134           // in that case, look for each service endpoint
135           System.err.println("JWS2 Discoverer: " + jwsservers
136                   + " is a JABAWS1 server. Using hardwired list.");
137           for (Services srv : JABAWS1SERVERS)
138           {
139             srv_set.add(srv);
140           }
141         }
142         for (Category cat : categories)
143         {
144           for (Services srv : cat.getServices())
145           {
146             if (quit)
147             {
148               running = false;
149               return;
150             }
151             if (!srv_set.contains(srv))
152             {
153               continue;
154             }
155             JABAService service = null;
156             try
157             {
158               service = Jws2Client.connect(jwsservers, srv);
159             } catch (Exception e)
160             {
161               System.err.println("Jws2 Discoverer: Problem on "
162                       + jwsservers + " with service " + srv + ":\n"
163                       + e.getMessage());
164               if (!(e instanceof javax.xml.ws.WebServiceException))
165               {
166                 e.printStackTrace();
167               }
168               // For moment, report service as a problem.
169               jws2Discoverer.addInvalidServiceUrl(jwsservers);
170             }
171             ;
172             if (service != null)
173             {
174               noservices = false;
175               Jws2Instance svc = null;
176               if (registry != null)
177               {
178
179                 String description = registry.getServiceDescription(srv);
180
181                 svc = Jws2InstanceFactory.newJws2Instance(jwsservers, srv.toString(),
182                         cat.name, description, service);
183               }
184               if (svc == null)
185               {
186                 svc = Jws2InstanceFactory.newJws2Instance(jwsservers, srv.toString(),
187                         cat.name, "JABAWS 1 Alignment Service", service);
188               }
189               jws2Discoverer.addService(jwsservers, svc);
190             }
191
192           }
193         }
194
195         if (noservices)
196         {
197           jws2Discoverer.addUrlwithnoservices(jwsservers);
198         }
199       }
200       else
201       {
202         jws2Discoverer.addInvalidServiceUrl(jwsservers);
203         Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
204       }
205     } catch (Exception e)
206     {
207       e.printStackTrace();
208       Cache.log.warn("Exception when discovering Jws2 services.", e);
209       jws2Discoverer.addInvalidServiceUrl(jwsservers);
210     } catch (Error e)
211     {
212       Cache.log.error("Exception when discovering Jws2 services.", e);
213       jws2Discoverer.addInvalidServiceUrl(jwsservers);
214     }
215     running = false;
216   }
217
218 }