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