formatting
[jalview.git] / src / jalview / ws / jws2 / jabaws2 / Jws2Instance.java
1 package jalview.ws.jws2.jabaws2;
2
3 import jalview.gui.AlignFrame;
4 import jalview.gui.Desktop;
5 import jalview.ws.jws2.JabaParamStore;
6 import jalview.ws.jws2.MsaWSClient;
7 import jalview.ws.jws2.SequenceAnnotationWSClient;
8 import jalview.ws.params.ParamDatastoreI;
9
10 import java.io.Closeable;
11
12 import javax.swing.JMenu;
13
14 import compbio.data.msa.JABAService;
15 import compbio.data.msa.MsaWS;
16 import compbio.data.msa.SequenceAnnotation;
17 import compbio.metadata.PresetManager;
18 import compbio.metadata.RunnerConfig;
19
20 public class Jws2Instance
21 {
22   public String hosturl;
23
24   public String serviceType;
25
26   public String action;
27
28   public JABAService service;
29
30   public String description;
31
32   public String docUrl;
33
34   public Jws2Instance(String hosturl, String serviceType, String action,
35           String description, JABAService service)
36   {
37     super();
38     this.hosturl = hosturl;
39     this.serviceType = serviceType;
40     this.service = service;
41     this.action = action;
42     this.description = description;
43     int p = description.indexOf("MORE INFORMATION:");
44     if (p > -1)
45     {
46       docUrl = description.substring(description.indexOf("http", p)).trim();
47       if (docUrl.indexOf('\n') > -1)
48       {
49         docUrl = docUrl.substring(0, docUrl.indexOf("\n")).trim();
50       }
51
52     }
53   }
54
55   PresetManager presets = null;
56
57   public JabaParamStore paramStore = null;
58
59   /**
60    * non thread safe - gets the presets for this service (blocks whilst it calls
61    * the service to get the preset set)
62    * 
63    * @return service presets or null if exceptions were raised.
64    */
65   public PresetManager getPresets()
66   {
67     if (presets == null)
68     {
69       try
70       {
71         if (service instanceof MsaWS<?>)
72         {
73           presets = ((MsaWS) service).getPresets();
74
75         }
76         if (service instanceof SequenceAnnotation<?>)
77         {
78           presets = ((SequenceAnnotation) service).getPresets();
79         }
80       } catch (Exception ex)
81       {
82         System.err.println("Exception when retrieving presets for service "
83                 + serviceType + " at " + hosturl);
84       }
85     }
86     return presets;
87   }
88
89   public String getHost()
90   {
91     return hosturl;
92     /*
93      * try { URL serviceurl = new URL(hosturl); if (serviceurl.getPort()!=80) {
94      * return serviceurl.getHost()+":"+serviceurl.getPort(); } return
95      * serviceurl.getHost(); } catch (Exception e) {
96      * System.err.println("Failed to parse service URL '" + hosturl +
97      * "' as a valid URL!"); } return null;
98      */
99   }
100
101   /**
102    * @return short description of what the service will do
103    */
104   public String getActionText()
105   {
106     return action + " with " + serviceType;
107   }
108
109   /**
110    * non-thread safe - blocks whilst accessing service to get complete set of
111    * available options and parameters
112    * 
113    * @return
114    */
115   public RunnerConfig getRunnerConfig()
116   {
117     if (service instanceof MsaWS<?>)
118     {
119       return ((MsaWS) service).getRunnerOptions();
120     }
121     if (service instanceof SequenceAnnotation<?>)
122     {
123       return ((SequenceAnnotation) service).getRunnerOptions();
124     }
125     throw new Error(
126             "Implementation Error: Runner Config not available for a JABAWS service of type "
127                     + serviceType + " (" + service.getClass() + ")");
128   }
129
130   @Override
131   protected void finalize() throws Throwable
132   {
133     if (service != null)
134     {
135       try
136       {
137         Closeable svc = (Closeable) service;
138         service = null;
139         svc.close();
140       } catch (Exception e)
141       {
142       }
143       ;
144     }
145     super.finalize();
146   }
147
148   public ParamDatastoreI getParamStore()
149   {
150     if (paramStore == null)
151     {
152       try
153       {
154         paramStore = new JabaParamStore(this,
155                 (Desktop.instance != null ? Desktop.getUserParameterStore()
156                         : null));
157       } catch (Exception ex)
158       {
159       }
160
161     }
162     return paramStore;
163   }
164
165   public String getUri()
166   {
167     // this is only valid for Jaba 1.0 - this formula might have to change!
168     return hosturl
169             + (hosturl.lastIndexOf("/") == (hosturl.length() - 1) ? ""
170                     : "/") + serviceType;
171   }
172
173   private boolean hasParams = false, lookedForParams = false;
174
175   public boolean hasParameters()
176   {
177     if (!lookedForParams)
178     {
179       lookedForParams = true;
180       try
181       {
182         hasParams = (getRunnerConfig().getArguments().size() > 0);
183       } catch (Exception e)
184       {
185
186       }
187     }
188     return hasParams;
189   }
190
191   public void attachWSMenuEntry(JMenu atpoint, AlignFrame alignFrame)
192   {
193     if (service instanceof MsaWS<?>)
194     {
195       new MsaWSClient().attachWSMenuEntry(atpoint, this, alignFrame);
196     }
197     else if (service instanceof SequenceAnnotation<?>)
198     {
199       new SequenceAnnotationWSClient().attachWSMenuEntry(atpoint, this,
200               alignFrame);
201     }
202   }
203
204   public String getServiceTypeURI()
205   {
206     return "java:" + serviceType;
207   }
208 }