0ac1df42b0e8655c6d96d6c540b7c5ef27afc267
[jalview.git] / src / jalview / ws / jws2 / jabaws2 / Jws2Instance.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.jws2.jabaws2;
20
21 import jalview.gui.AlignFrame;
22 import jalview.gui.Desktop;
23 import jalview.ws.jws2.JabaParamStore;
24 import jalview.ws.jws2.MsaWSClient;
25 import jalview.ws.jws2.SequenceAnnotationWSClient;
26 import jalview.ws.params.ParamDatastoreI;
27
28 import java.io.Closeable;
29
30 import javax.swing.JMenu;
31
32 import compbio.data.msa.JABAService;
33 import compbio.data.msa.MsaWS;
34 import compbio.data.msa.SequenceAnnotation;
35 import compbio.metadata.PresetManager;
36 import compbio.metadata.RunnerConfig;
37
38 public class Jws2Instance
39 {
40   public String hosturl;
41
42   public String serviceType;
43
44   public String action;
45
46   public JABAService service;
47
48   public String description;
49
50   public String docUrl;
51
52   /**
53    * 
54    * @param hosturl Service endpoint
55    * @param serviceType Category for this service's analysis
56    * @param action text describing their action that service performs (eg 'aligning', 'analysing')
57    * @param description Description from JABAWS registry
58    * @param service JABAWS registry ID for service
59    */
60   public Jws2Instance(String hosturl, String serviceType, String action,
61           String description, JABAService service)
62   {
63     super();
64     this.hosturl = hosturl;
65     this.serviceType = serviceType;
66     this.service = service;
67     this.action = action;
68     this.description = description;
69     int p = description.indexOf("MORE INFORMATION:");
70     if (p > -1)
71     {
72       docUrl = description.substring(description.indexOf("http", p)).trim();
73       if (docUrl.indexOf('\n') > -1)
74       {
75         docUrl = docUrl.substring(0, docUrl.indexOf("\n")).trim();
76       }
77
78     }
79   }
80
81   PresetManager presets = null;
82
83   public JabaParamStore paramStore = null;
84
85   /**
86    * non thread safe - gets the presets for this service (blocks whilst it calls
87    * the service to get the preset set)
88    * 
89    * @return service presets or null if exceptions were raised.
90    */
91   public PresetManager getPresets()
92   {
93     if (presets == null)
94     {
95       try
96       {
97         if (service instanceof MsaWS<?>)
98         {
99           presets = ((MsaWS) service).getPresets();
100
101         }
102         if (service instanceof SequenceAnnotation<?>)
103         {
104           presets = ((SequenceAnnotation) service).getPresets();
105         }
106       } catch (Exception ex)
107       {
108         System.err.println("Exception when retrieving presets for service "
109                 + serviceType + " at " + hosturl);
110       }
111     }
112     return presets;
113   }
114
115   public String getHost()
116   {
117     return hosturl;
118     /*
119      * try { URL serviceurl = new URL(hosturl); if (serviceurl.getPort()!=80) {
120      * return serviceurl.getHost()+":"+serviceurl.getPort(); } return
121      * serviceurl.getHost(); } catch (Exception e) {
122      * System.err.println("Failed to parse service URL '" + hosturl +
123      * "' as a valid URL!"); } return null;
124      */
125   }
126
127   /**
128    * @return short description of what the service will do
129    */
130   public String getActionText()
131   {
132     return action + " with " + serviceType;
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(
152             "Implementation Error: Runner Config not available for a JABAWS service of type "
153                     + serviceType + " (" + service.getClass() + ")");
154   }
155
156   @Override
157   protected void finalize() throws Throwable
158   {
159     if (service != null)
160     {
161       try
162       {
163         Closeable svc = (Closeable) service;
164         service = null;
165         svc.close();
166       } catch (Exception e)
167       {
168       }
169       ;
170     }
171     super.finalize();
172   }
173
174   public ParamDatastoreI getParamStore()
175   {
176     if (paramStore == null)
177     {
178       try
179       {
180         paramStore = new JabaParamStore(this,
181                 (Desktop.instance != 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   public String getUri()
194   {
195     // this is only valid for Jaba 1.0 - this formula might have to change!
196     return hosturl
197             + (hosturl.lastIndexOf("/") == (hosturl.length() - 1) ? ""
198                     : "/") + serviceType;
199   }
200
201   private boolean hasParams = false, lookedForParams = false;
202
203   public boolean hasParameters()
204   {
205     if (!lookedForParams)
206     {
207       lookedForParams = true;
208       try
209       {
210         hasParams = (getRunnerConfig().getArguments().size() > 0);
211       } catch (Exception e)
212       {
213
214       }
215     }
216     return hasParams;
217   }
218
219   public void attachWSMenuEntry(JMenu atpoint, AlignFrame alignFrame)
220   {
221     if (service instanceof MsaWS<?>)
222     {
223       new MsaWSClient().attachWSMenuEntry(atpoint, this, alignFrame);
224     }
225     else if (service instanceof SequenceAnnotation<?>)
226     {
227       new SequenceAnnotationWSClient().attachWSMenuEntry(atpoint, this,
228               alignFrame);
229     }
230   }
231
232   public String getServiceTypeURI()
233   {
234     return "java:" + serviceType;
235   }
236   jalview.ws.uimodel.AlignAnalysisUIText aaui;
237   public jalview.ws.uimodel.AlignAnalysisUIText getAlignAnalysisUI()
238   {
239     return aaui;
240   }
241 }