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