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