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