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