JAL-1432 updated copyright notices
[jalview.git] / src / jalview / ws / jws2 / JabaWsServerQuery.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 /**
20  * 
21  */
22 package jalview.ws.jws2;
23
24 import jalview.bin.Cache;
25 import jalview.ws.jws2.jabaws2.Jws2Instance;
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 };
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             svccategories = registry.getServiceCategories();
120
121           }
122         } catch (Exception ex)
123         {
124           System.err.println("Exception whilst trying to get at registry:");
125           ex.printStackTrace();
126           // if that failed, then we are probably working with a JABAWS1 server.
127           // in that case, look for each service endpoint
128           System.err.println("JWS2 Discoverer: " + jwsservers
129                   + " is a JABAWS1 server. Using hardwired list.");
130           for (Services srv : JABAWS1SERVERS)
131           {
132             srv_set.add(srv);
133           }
134         }
135         for (Category cat : categories)
136         {
137           for (Services srv : cat.getServices())
138           {
139             if (quit)
140             {
141               running = false;
142               return;
143             }
144             if (!srv_set.contains(srv))
145             {
146               continue;
147             }
148             JABAService service = null;
149             try
150             {
151               service = Jws2Client.connect(jwsservers, srv);
152             } catch (Exception e)
153             {
154               System.err.println("Jws2 Discoverer: Problem on "
155                       + jwsservers + " with service " + srv + ":\n"
156                       + e.getMessage());
157               if (!(e instanceof javax.xml.ws.WebServiceException))
158               {
159                 e.printStackTrace();
160               }
161               // For moment, report service as a problem.
162               jws2Discoverer.addInvalidServiceUrl(jwsservers);
163             }
164             ;
165             if (service != null)
166             {
167               noservices = false;
168               Jws2Instance svc = null;
169               if (registry != null)
170               {
171
172                 String description = registry.getServiceDescription(srv);
173
174                 svc = new Jws2Instance(jwsservers, srv.toString(),
175                         cat.name, description, service);
176               }
177               if (svc == null)
178               {
179                 svc = new Jws2Instance(jwsservers, srv.toString(),
180                         cat.name, "JABAWS 1 Alignment Service", service);
181               }
182               jws2Discoverer.addService(jwsservers, svc);
183             }
184
185           }
186         }
187
188         if (noservices)
189         {
190           jws2Discoverer.addUrlwithnoservices(jwsservers);
191         }
192       }
193       else
194       {
195         jws2Discoverer.addInvalidServiceUrl(jwsservers);
196         Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
197       }
198     } catch (Exception e)
199     {
200       e.printStackTrace();
201       Cache.log.warn("Exception when discovering Jws2 services.", e);
202       jws2Discoverer.addInvalidServiceUrl(jwsservers);
203     } catch (Error e)
204     {
205       Cache.log.error("Exception when discovering Jws2 services.", e);
206       jws2Discoverer.addInvalidServiceUrl(jwsservers);
207     }
208     running = false;
209   }
210
211 }