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