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