509b96fdb6ae6181360020ca49118c18bfa1c38f
[jalview.git] / src / jalview / ws / jws2 / jabaws2 / Jws2Instance.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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  */
18 package jalview.ws.jws2.jabaws2;
19
20 import jalview.gui.AlignFrame;
21 import jalview.gui.Desktop;
22 import jalview.ws.jws2.JabaParamStore;
23 import jalview.ws.jws2.MsaWSClient;
24 import jalview.ws.jws2.SequenceAnnotationWSClient;
25 import jalview.ws.params.ParamDatastoreI;
26
27 import java.io.Closeable;
28
29 import javax.swing.JMenu;
30
31 import compbio.data.msa.JABAService;
32 import compbio.data.msa.MsaWS;
33 import compbio.data.msa.SequenceAnnotation;
34 import compbio.metadata.PresetManager;
35 import compbio.metadata.RunnerConfig;
36
37 public class Jws2Instance
38 {
39   public String hosturl;
40
41   public String serviceType;
42
43   public String action;
44
45   public JABAService service;
46
47   public String description;
48
49   public String docUrl;
50
51   /**
52    * 
53    * @param hosturl Service endpoint
54    * @param serviceType Category for this service's analysis
55    * @param action text describing their action that service performs (eg 'aligning', 'analysing')
56    * @param description Description from JABAWS registry
57    * @param service JABAWS registry ID for service
58    */
59   public Jws2Instance(String hosturl, String serviceType, String action,
60           String description, JABAService service)
61   {
62     super();
63     this.hosturl = hosturl;
64     this.serviceType = serviceType;
65     this.service = service;
66     this.action = action;
67     this.description = description;
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                 + serviceType + " at " + hosturl);
109       }
110     }
111     return presets;
112   }
113
114   public String getHost()
115   {
116     return hosturl;
117     /*
118      * try { URL serviceurl = new URL(hosturl); if (serviceurl.getPort()!=80) {
119      * return serviceurl.getHost()+":"+serviceurl.getPort(); } return
120      * serviceurl.getHost(); } catch (Exception e) {
121      * System.err.println("Failed to parse service URL '" + hosturl +
122      * "' as a valid URL!"); } return null;
123      */
124   }
125
126   /**
127    * @return short description of what the service will do
128    */
129   public String getActionText()
130   {
131     return action + " with " + serviceType;
132   }
133
134   /**
135    * non-thread safe - blocks whilst accessing service to get complete set of
136    * available options and parameters
137    * 
138    * @return
139    */
140   public RunnerConfig getRunnerConfig()
141   {
142     if (service instanceof MsaWS<?>)
143     {
144       return ((MsaWS) service).getRunnerOptions();
145     }
146     if (service instanceof SequenceAnnotation<?>)
147     {
148       return ((SequenceAnnotation) service).getRunnerOptions();
149     }
150     throw new Error(
151             "Implementation Error: Runner Config not available for a JABAWS service of type "
152                     + serviceType + " (" + service.getClass() + ")");
153   }
154
155   @Override
156   protected void finalize() throws Throwable
157   {
158     if (service != null)
159     {
160       try
161       {
162         Closeable svc = (Closeable) service;
163         service = null;
164         svc.close();
165       } catch (Exception e)
166       {
167       }
168       ;
169     }
170     super.finalize();
171   }
172
173   public ParamDatastoreI getParamStore()
174   {
175     if (paramStore == null)
176     {
177       try
178       {
179         paramStore = new JabaParamStore(this,
180                 (Desktop.instance != null ? Desktop.getUserParameterStore()
181                         : null));
182       } catch (Exception ex)
183       {
184         System.err.println("Unexpected exception creating JabaParamStore.");
185         ex.printStackTrace();
186       }
187
188     }
189     return paramStore;
190   }
191
192   public String getUri()
193   {
194     // this is only valid for Jaba 1.0 - this formula might have to change!
195     return hosturl
196             + (hosturl.lastIndexOf("/") == (hosturl.length() - 1) ? ""
197                     : "/") + serviceType;
198   }
199
200   private boolean hasParams = false, lookedForParams = false;
201
202   public boolean hasParameters()
203   {
204     if (!lookedForParams)
205     {
206       lookedForParams = true;
207       try
208       {
209         hasParams = (getRunnerConfig().getArguments().size() > 0);
210       } catch (Exception e)
211       {
212
213       }
214     }
215     return hasParams;
216   }
217
218   public void attachWSMenuEntry(JMenu atpoint, AlignFrame alignFrame)
219   {
220     if (service instanceof MsaWS<?>)
221     {
222       new MsaWSClient().attachWSMenuEntry(atpoint, this, alignFrame);
223     }
224     else if (service instanceof SequenceAnnotation<?>)
225     {
226       new SequenceAnnotationWSClient().attachWSMenuEntry(atpoint, this,
227               alignFrame);
228     }
229   }
230
231   public String getServiceTypeURI()
232   {
233     return "java:" + serviceType;
234   }
235   jalview.ws.uimodel.AlignAnalysisUIText aaui;
236   public jalview.ws.uimodel.AlignAnalysisUIText getAlignAnalysisUI()
237   {
238     return aaui;
239   }
240 }