JAL-1517 fix copyright for 2.8.2
[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: " + srv_set.toString());
125             
126             svccategories = registry.getServiceCategories();
127             
128             // dan test
129 //            System.out.println("registry.getServiceCategories: " + svccategories.toString());
130
131           }
132         } catch (Exception ex)
133         {
134           System.err.println("Exception whilst trying to get at registry:");
135           ex.printStackTrace();
136           // if that failed, then we are probably working with a JABAWS1 server.
137           // in that case, look for each service endpoint
138           System.err.println("JWS2 Discoverer: " + jwsservers
139                   + " is a JABAWS1 server. Using hardwired list.");
140           for (Services srv : JABAWS1SERVERS)
141           {
142             srv_set.add(srv);
143           }
144         }
145         for (Category cat : categories)
146         {
147           for (Services srv : cat.getServices())
148           {
149             if (quit)
150             {
151               running = false;
152               return;
153             }
154             if (!srv_set.contains(srv))
155             {
156               continue;
157             }
158             JABAService service = null;
159             try
160             {
161               service = Jws2Client.connect(jwsservers, srv);
162             } catch (Exception e)
163             {
164               System.err.println("Jws2 Discoverer: Problem on "
165                       + jwsservers + " with service " + srv + ":\n"
166                       + e.getMessage());
167               if (!(e instanceof javax.xml.ws.WebServiceException))
168               {
169                 e.printStackTrace();
170               }
171               // For moment, report service as a problem.
172               jws2Discoverer.addInvalidServiceUrl(jwsservers);
173             }
174             ;
175             if (service != null)
176             {
177               noservices = false;
178               Jws2Instance svc = null;
179               if (registry != null)
180               {
181
182                 String description = registry.getServiceDescription(srv);
183
184                 svc = Jws2InstanceFactory.newJws2Instance(jwsservers, srv.toString(),
185                         cat.name, description, service);
186               }
187               if (svc == null)
188               {
189                 svc = Jws2InstanceFactory.newJws2Instance(jwsservers, srv.toString(),
190                         cat.name, "JABAWS 1 Alignment Service", service);
191               }
192               jws2Discoverer.addService(jwsservers, svc);
193             }
194
195           }
196         }
197
198         if (noservices)
199         {
200           jws2Discoverer.addUrlwithnoservices(jwsservers);
201         }
202       }
203       else
204       {
205         jws2Discoverer.addInvalidServiceUrl(jwsservers);
206         Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
207       }
208     } catch (Exception e)
209     {
210       e.printStackTrace();
211       Cache.log.warn("Exception when discovering Jws2 services.", e);
212       jws2Discoverer.addInvalidServiceUrl(jwsservers);
213     } catch (Error e)
214     {
215       Cache.log.error("Exception when discovering Jws2 services.", e);
216       jws2Discoverer.addInvalidServiceUrl(jwsservers);
217     }
218     running = false;
219   }
220
221 }