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