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