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