JAL-3066 JAL-3070 pull up non-jabaws specific service descriptor components for annot...
[jalview.git] / src / jalview / ws / jws2 / jabaws2 / Jws2Instance.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 package jalview.ws.jws2.jabaws2;
22
23 import jalview.gui.AlignFrame;
24 import jalview.gui.Desktop;
25 import jalview.util.MessageManager;
26 import jalview.ws.api.JalviewServiceEndpointProviderI;
27 import jalview.ws.api.ServiceWithParameters;
28 import jalview.ws.jws2.JabaParamStore;
29 import jalview.ws.jws2.MsaWSClient;
30 import jalview.ws.jws2.SequenceAnnotationWSClient;
31 import jalview.ws.params.ParamDatastoreI;
32 import jalview.ws.params.ParamManager;
33
34 import java.io.Closeable;
35 import java.net.URL;
36
37 import javax.swing.JMenu;
38
39 import compbio.data.msa.JABAService;
40 import compbio.data.msa.MsaWS;
41 import compbio.data.msa.SequenceAnnotation;
42 import compbio.metadata.PresetManager;
43 import compbio.metadata.RunnerConfig;
44
45 public class Jws2Instance extends ServiceWithParameters
46         implements JalviewServiceEndpointProviderI, AutoCloseable
47 {
48
49   public JABAService service;
50
51   /**
52    * 
53    * @param hosturl
54    *          Service endpoint
55    * @param serviceType
56    *          Category for this service's analysis
57    * @param action
58    *          text describing their action that service performs (eg 'aligning',
59    *          'analysing')
60    * @param description
61    *          Description from JABAWS registry
62    * @param service
63    *          JABAWS registry ID for service
64    */
65   public Jws2Instance(String hosturl, String serviceType, String action,
66           String description, JABAService service)
67   {
68     super(action, action, serviceType, description, hosturl);
69     this.service = service;
70     int p = description.indexOf("MORE INFORMATION:");
71     if (p > -1)
72     {
73       String docUrl = description.substring(description.indexOf("http", p))
74               .trim();
75       if (docUrl.indexOf('\n') > -1)
76       {
77         docUrl = docUrl.substring(0, docUrl.indexOf("\n")).trim();
78       }
79       if (docUrl.length() > 0)
80       {
81         try
82         {
83           URL url = new URL(docUrl);
84           if (url != null)
85           {
86             setDocumentationUrl(docUrl);
87           }
88         } catch (Exception x)
89         {
90
91         }
92       }
93
94     }
95   }
96
97   PresetManager presets = null;
98
99   public JabaParamStore paramStore = null;
100
101   /**
102    * non thread safe - gets the presets for this service (blocks whilst it calls
103    * the service to get the preset set)
104    * 
105    * @return service presets or null if exceptions were raised.
106    */
107   public PresetManager getPresets()
108   {
109     if (presets == null)
110     {
111       try
112       {
113         if (service instanceof MsaWS<?>)
114         {
115           presets = ((MsaWS) service).getPresets();
116
117         }
118         if (service instanceof SequenceAnnotation<?>)
119         {
120           presets = ((SequenceAnnotation) service).getPresets();
121         }
122       } catch (Exception ex)
123       {
124         System.err.println("Exception when retrieving presets for service "
125                 + getServiceType() + " at " + getHostURL());
126       }
127     }
128     return presets;
129   }
130
131   /**
132    * non-thread safe - blocks whilst accessing service to get complete set of
133    * available options and parameters
134    * 
135    * @return
136    */
137   public RunnerConfig getRunnerConfig()
138   {
139     if (service instanceof MsaWS<?>)
140     {
141       return ((MsaWS) service).getRunnerOptions();
142     }
143     if (service instanceof SequenceAnnotation<?>)
144     {
145       return ((SequenceAnnotation) service).getRunnerOptions();
146     }
147     throw new Error(MessageManager.formatMessage(
148             "error.implementation_error_runner_config_not_available",
149             new String[]
150             { getServiceType(), service.getClass().toString() }));
151   }
152
153   @Override
154   public void close()
155   {
156     if (service != null)
157     {
158       try
159       {
160         ((Closeable) service).close();
161       } catch (Throwable t)
162       {
163         // ignore
164       }
165     }
166     // super.finalize();
167   }
168
169   @Override
170   public ParamDatastoreI getParamStore()
171   {
172     if (paramStore == null)
173     {
174       try
175       {
176         paramStore = new JabaParamStore(this,
177                 (Desktop.instance != null ? Desktop.getUserParameterStore()
178                         : null));
179       } catch (Exception ex)
180       {
181         System.err.println("Unexpected exception creating JabaParamStore.");
182         ex.printStackTrace();
183       }
184
185     }
186     return paramStore;
187   }
188
189   private boolean hasParams = false, lookedForParams = false;
190
191   @Override
192   public boolean hasParameters()
193   {
194     if (!lookedForParams)
195     {
196       lookedForParams = true;
197       try
198       {
199         hasParams = (getRunnerConfig().getArguments().size() > 0);
200       } catch (Exception e)
201       {
202
203       }
204     }
205     return hasParams;
206   }
207
208   public void attachWSMenuEntry(JMenu atpoint, AlignFrame alignFrame)
209   {
210     if (service instanceof MsaWS<?>)
211     {
212       new MsaWSClient().attachWSMenuEntry(atpoint, this, alignFrame);
213     }
214     else if (service instanceof SequenceAnnotation<?>)
215     {
216       new SequenceAnnotationWSClient().attachWSMenuEntry(atpoint, this,
217               alignFrame);
218     }
219   }
220
221   /**
222    * initialise a parameter store for this service
223    * 
224    * @param userParameterStore
225    *          - the user ParamManager (e.g. Desktop.getUserParameterStore() )
226    */
227   @Override
228   public void initParamStore(ParamManager userParameterStore)
229   {
230     if (paramStore == null)
231     {
232       paramStore = new JabaParamStore(this, userParameterStore);
233     }
234   }
235
236   /**
237    * an object that implements one or more interfaces in jalview.ws.api
238    * 
239    * @return
240    */
241   @Override
242   public Object getEndpoint()
243   {
244     if (service instanceof MsaWS<?>)
245     {
246       if (aaui != null)
247       {
248         throw new Error(
249                 "JABAWS MsaWS based instant calculation not implemented.");
250
251       }
252       else
253       {
254         return new JabawsMsaInstance(this);
255       }
256     }
257     else
258     {
259       if (service instanceof compbio.data.msa.SequenceAnnotation)
260       {
261         if (aaui != null)
262         {
263           try
264           {
265             // probably a factory would be nicer but..
266             return aaui.getClient().getConstructor(getClass())
267                     .newInstance(this);
268           } catch (Throwable t)
269           {
270             throw new Error("Implementation Error in web service framework",
271                     t);
272           }
273         }
274         return new AADisorderClient(this);
275       }
276       return null;
277     }
278   }
279 }