JAL-1043 patch to show warning dialog when webservice exception is thrown
[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.gui.Desktop;
28 import jalview.ws.jws2.jabaws2.Jws2Instance;
29 import jalview.ws.jws2.jabaws2.Jws2InstanceFactory;
30
31 import java.util.HashSet;
32 import java.util.Set;
33
34 import javax.swing.JOptionPane;
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 jwsservers = 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 jwsservers)
72   {
73     this.jws2Discoverer = jws2Discoverer;
74     this.jwsservers = jwsservers;
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       if (Jws2Client.validURL(jwsservers))
99       {
100         compbio.data.msa.RegistryWS registry = null;
101         Set svccategories = null;
102         boolean noservices = true;
103         // look for services
104         boolean jabasws2 = false;
105         // If we are dealing with a JABAWS2 service, then just go and ask the
106         // JABAWS 2 service registry
107         Set<Services> srv_set = new HashSet<Services>();
108
109         Set<Category> categories = Category.getCategories();
110         String svc_cat;
111
112         try
113         {
114           // JBPNote: why is RegistryWS in compbio.data.msa ?
115           registry = Jws2Client.connectToRegistry(jwsservers);
116           if (registry != null)
117           {
118             // System.err.println("Test Services Output\n"
119             // + registry.testAllServices());
120             // TODO: enumerate services and test those that haven't been tested
121             // in the last n-days/hours/etc.
122
123             jabasws2 = true;
124             srv_set = registry.getSupportedServices();
125
126             // dan test
127             System.out.println("registry.getSupportedServices: "
128                     + srv_set.toString());
129
130             svccategories = registry.getServiceCategories();
131
132             // dan test
133             // System.out.println("registry.getServiceCategories: " +
134             // svccategories.toString());
135
136           }
137         } catch (javax.xml.ws.WebServiceException ex)
138         {
139           JOptionPane
140                   .showInternalMessageDialog(
141                           Desktop.desktop,
142                           "Exception whilst trying to access the webservice registry:",
143                           "Webservice glitch", JOptionPane.WARNING_MESSAGE);
144           System.err.println("Exception whilst trying to get at registry:");
145           ex.printStackTrace();
146           // if that failed, then we are probably working with a JABAWS1 server.
147           // in that case, look for each service endpoint
148           System.err.println("JWS2 Discoverer: " + jwsservers
149                   + " is a JABAWS1 server. Using hardwired list.");
150           for (Services srv : JABAWS1SERVERS)
151           {
152             srv_set.add(srv);
153           }
154         }
155  catch (Exception ex)
156         {
157           ex.printStackTrace();
158         }
159         for (Category cat : categories)
160         {
161           for (Services srv : cat.getServices())
162           {
163             if (quit)
164             {
165               running = false;
166               return;
167             }
168             if (!srv_set.contains(srv))
169             {
170               continue;
171             }
172             JABAService service = null;
173             try
174             {
175               service = Jws2Client.connect(jwsservers, srv);
176             } catch (Exception e)
177             {
178               System.err.println("Jws2 Discoverer: Problem on "
179                       + jwsservers + " with service " + srv + ":\n"
180                       + e.getMessage());
181               if (!(e instanceof javax.xml.ws.WebServiceException))
182               {
183                 e.printStackTrace();
184               }
185               // For moment, report service as a problem.
186               jws2Discoverer.addInvalidServiceUrl(jwsservers);
187             }
188             ;
189             if (service != null)
190             {
191               noservices = false;
192               Jws2Instance svc = null;
193               if (registry != null)
194               {
195
196                 String description = registry.getServiceDescription(srv);
197
198                 svc = Jws2InstanceFactory.newJws2Instance(jwsservers,
199                         srv.toString(), cat.name, description, service);
200               }
201               if (svc == null)
202               {
203                 svc = Jws2InstanceFactory.newJws2Instance(jwsservers,
204                         srv.toString(), cat.name,
205                         "JABAWS 1 Alignment Service", service);
206               }
207               jws2Discoverer.addService(jwsservers, svc);
208             }
209
210           }
211         }
212
213         if (noservices)
214         {
215           jws2Discoverer.addUrlwithnoservices(jwsservers);
216         }
217       }
218       else
219       {
220         jws2Discoverer.addInvalidServiceUrl(jwsservers);
221         Cache.log.info("Ignoring invalid Jws2 service url " + jwsservers);
222       }
223     } catch (Exception e)
224     {
225       e.printStackTrace();
226       Cache.log.warn("Exception when discovering Jws2 services.", e);
227       jws2Discoverer.addInvalidServiceUrl(jwsservers);
228     } catch (Error e)
229     {
230       Cache.log.error("Exception when discovering Jws2 services.", e);
231       jws2Discoverer.addInvalidServiceUrl(jwsservers);
232     }
233     running = false;
234   }
235
236 }