JAL-1517 source formatting
[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
57    *          Service endpoint
58    * @param serviceType
59    *          Category for this service's analysis
60    * @param action
61    *          text describing their action that service performs (eg 'aligning',
62    *          'analysing')
63    * @param description
64    *          Description from JABAWS registry
65    * @param service
66    *          JABAWS registry ID for service
67    */
68   public Jws2Instance(String hosturl, String serviceType, String action,
69           String description, JABAService service)
70   {
71     super();
72     this.hosturl = hosturl;
73     this.serviceType = serviceType;
74     this.service = service;
75     this.action = action;
76     this.description = description;
77     int p = description.indexOf("MORE INFORMATION:");
78     if (p > -1)
79     {
80       docUrl = description.substring(description.indexOf("http", p)).trim();
81       if (docUrl.indexOf('\n') > -1)
82       {
83         docUrl = docUrl.substring(0, docUrl.indexOf("\n")).trim();
84       }
85
86     }
87   }
88
89   PresetManager presets = null;
90
91   public JabaParamStore paramStore = null;
92
93   /**
94    * non thread safe - gets the presets for this service (blocks whilst it calls
95    * the service to get the preset set)
96    * 
97    * @return service presets or null if exceptions were raised.
98    */
99   public PresetManager getPresets()
100   {
101     if (presets == null)
102     {
103       try
104       {
105         if (service instanceof MsaWS<?>)
106         {
107           presets = ((MsaWS) service).getPresets();
108
109         }
110         if (service instanceof SequenceAnnotation<?>)
111         {
112           presets = ((SequenceAnnotation) service).getPresets();
113         }
114       } catch (Exception ex)
115       {
116         System.err.println("Exception when retrieving presets for service "
117                 + serviceType + " at " + hosturl);
118       }
119     }
120     return presets;
121   }
122
123   public String getHost()
124   {
125     return hosturl;
126     /*
127      * try { URL serviceurl = new URL(hosturl); if (serviceurl.getPort()!=80) {
128      * return serviceurl.getHost()+":"+serviceurl.getPort(); } return
129      * serviceurl.getHost(); } catch (Exception e) {
130      * System.err.println("Failed to parse service URL '" + hosturl +
131      * "' as a valid URL!"); } return null;
132      */
133   }
134
135   /**
136    * @return short description of what the service will do
137    */
138   public String getActionText()
139   {
140     return action + " with " + serviceType;
141   }
142
143   /**
144    * non-thread safe - blocks whilst accessing service to get complete set of
145    * available options and parameters
146    * 
147    * @return
148    */
149   public RunnerConfig getRunnerConfig()
150   {
151     if (service instanceof MsaWS<?>)
152     {
153       return ((MsaWS) service).getRunnerOptions();
154     }
155     if (service instanceof SequenceAnnotation<?>)
156     {
157       return ((SequenceAnnotation) service).getRunnerOptions();
158     }
159     throw new Error(
160             "Implementation Error: Runner Config not available for a JABAWS service of type "
161                     + serviceType + " (" + service.getClass() + ")");
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   jalview.ws.uimodel.AlignAnalysisUIText aaui;
246
247   public jalview.ws.uimodel.AlignAnalysisUIText getAlignAnalysisUI()
248   {
249     return aaui;
250   }
251 }