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