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