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