55799f05bc5299de30b6f22a75acb746cf75a2b0
[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   public String getUri()
190   {
191     // TODO verify that service parameter sets in projects are consistent with
192     // Jalview 2.10.4
193     // this is only valid for Jaba 1.0 - this formula might have to change!
194     return getHostURL()
195             + (getHostURL().lastIndexOf("/") == (getHostURL().length() - 1)
196                     ? ""
197                     : "/")
198             + getName();
199   }
200
201   private boolean hasParams = false, lookedForParams = false;
202
203   @Override
204   public boolean hasParameters()
205   {
206     if (!lookedForParams)
207     {
208       lookedForParams = true;
209       try
210       {
211         hasParams = (getRunnerConfig().getArguments().size() > 0);
212       } catch (Exception e)
213       {
214
215       }
216     }
217     return hasParams;
218   }
219
220   public void attachWSMenuEntry(JMenu atpoint, AlignFrame alignFrame)
221   {
222     if (service instanceof MsaWS<?>)
223     {
224       new MsaWSClient().attachWSMenuEntry(atpoint, this, alignFrame);
225     }
226     else if (service instanceof SequenceAnnotation<?>)
227     {
228       new SequenceAnnotationWSClient().attachWSMenuEntry(atpoint, this,
229               alignFrame);
230     }
231   }
232
233   public String getNameURI()
234   {
235     return "java:" + getName();
236   }
237
238   jalview.ws.uimodel.AlignAnalysisUIText aaui;
239
240   public jalview.ws.uimodel.AlignAnalysisUIText getAlignAnalysisUI()
241   {
242     return aaui;
243   }
244
245   /**
246    * initialise a parameter store for this service
247    * 
248    * @param userParameterStore
249    *          - the user ParamManager (e.g. Desktop.getUserParameterStore() )
250    */
251   @Override
252   public void initParamStore(ParamManager userParameterStore)
253   {
254     if (paramStore == null)
255     {
256       paramStore = new JabaParamStore(this, userParameterStore);
257     }
258   }
259
260   /**
261    * an object that implements one or more interfaces in jalview.ws.api
262    * 
263    * @return
264    */
265   @Override
266   public Object getEndpoint()
267   {
268     if (aaui!=null) {
269       // TODO complete
270       return null;
271     } else {
272       if (service instanceof MsaWS<?>)
273       {
274       return new JabawsMsaInstance(this);
275     } else {
276         // TODO complete
277         // service is for sequence analysis
278         return null;
279     }
280   }
281 }
282 }