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