3bff20bbef5a05f132fbccf47fd2e9aa136a57f3
[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.ServiceWithParameters;
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 import jalview.ws.params.ParamManager;
32
33 import java.io.Closeable;
34
35 import javax.swing.JMenu;
36
37 import compbio.data.msa.JABAService;
38 import compbio.data.msa.MsaWS;
39 import compbio.data.msa.SequenceAnnotation;
40 import compbio.metadata.PresetManager;
41 import compbio.metadata.RunnerConfig;
42
43 public class Jws2Instance extends ServiceWithParameters
44 {
45
46   public JABAService service;
47
48   public String docUrl;
49
50   /**
51    * 
52    * @param hosturl
53    *          Service endpoint
54    * @param serviceType
55    *          Category for this service's analysis
56    * @param action
57    *          text describing their action that service performs (eg 'aligning',
58    *          'analysing')
59    * @param description
60    *          Description from JABAWS registry
61    * @param service
62    *          JABAWS registry ID for service
63    */
64   public Jws2Instance(String hosturl, String serviceType, String action,
65           String description, JABAService service)
66   {
67     super(action, action, serviceType, description, hosturl);
68     this.service = service;
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                 + getServiceType() + " at " + getHostURL());
110       }
111     }
112     return presets;
113   }
114
115   /**
116    * non-thread safe - blocks whilst accessing service to get complete set of
117    * available options and parameters
118    * 
119    * @return
120    */
121   public RunnerConfig getRunnerConfig()
122   {
123     if (service instanceof MsaWS<?>)
124     {
125       return ((MsaWS) service).getRunnerOptions();
126     }
127     if (service instanceof SequenceAnnotation<?>)
128     {
129       return ((SequenceAnnotation) service).getRunnerOptions();
130     }
131     throw new Error(MessageManager.formatMessage(
132             "error.implementation_error_runner_config_not_available",
133             new String[]
134             { getServiceType(), service.getClass().toString() }));
135   }
136
137   @Override
138   protected void finalize() throws Throwable
139   {
140     if (service != null)
141     {
142       try
143       {
144         ((Closeable) service).close();
145       } catch (Throwable t)
146       {
147         // ignore
148       }
149     }
150     super.finalize();
151   }
152
153   @Override
154   public ParamDatastoreI getParamStore()
155   {
156     if (paramStore == null)
157     {
158       try
159       {
160         paramStore = new JabaParamStore(this,
161                 (Desktop.instance != null ? Desktop.getUserParameterStore()
162                         : null));
163       } catch (Exception ex)
164       {
165         System.err.println("Unexpected exception creating JabaParamStore.");
166         ex.printStackTrace();
167       }
168
169     }
170     return paramStore;
171   }
172
173   public String getUri()
174   {
175     // TODO verify that service parameter sets in projects are consistent with
176     // Jalview 2.10.4
177     // this is only valid for Jaba 1.0 - this formula might have to change!
178     return getHostURL()
179             + (getHostURL().lastIndexOf("/") == (getHostURL().length() - 1)
180                     ? ""
181                     : "/")
182             + getName();
183   }
184
185   private boolean hasParams = false, lookedForParams = false;
186
187   public boolean hasParameters()
188   {
189     if (!lookedForParams)
190     {
191       lookedForParams = true;
192       try
193       {
194         hasParams = (getRunnerConfig().getArguments().size() > 0);
195       } catch (Exception e)
196       {
197
198       }
199     }
200     return hasParams;
201   }
202
203   public void attachWSMenuEntry(JMenu atpoint, AlignFrame alignFrame)
204   {
205     if (service instanceof MsaWS<?>)
206     {
207       new MsaWSClient().attachWSMenuEntry(atpoint, this, alignFrame);
208     }
209     else if (service instanceof SequenceAnnotation<?>)
210     {
211       new SequenceAnnotationWSClient().attachWSMenuEntry(atpoint, this,
212               alignFrame);
213     }
214   }
215
216   public String getNameURI()
217   {
218     return "java:" + getName();
219   }
220
221   jalview.ws.uimodel.AlignAnalysisUIText aaui;
222
223   public jalview.ws.uimodel.AlignAnalysisUIText getAlignAnalysisUI()
224   {
225     return aaui;
226   }
227
228   /**
229    * initialise a parameter store for this service
230    * 
231    * @param userParameterStore
232    *          - the user ParamManager (e.g. Desktop.getUserParameterStore() )
233    */
234   @Override
235   public void initParamStore(ParamManager userParameterStore)
236   {
237     if (paramStore == null)
238     {
239       paramStore = new JabaParamStore(this, userParameterStore);
240     }
241   }
242 }