2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.bin.Cache;
24 import jalview.io.JalviewFileChooser;
25 import jalview.io.JalviewFileView;
26 import jalview.util.MessageManager;
27 import jalview.ws.params.ParamDatastoreI;
28 import jalview.ws.params.ParamManager;
29 import jalview.ws.params.WsParamSetI;
30 import jalview.xml.binding.jalview.ObjectFactory;
31 import jalview.xml.binding.jalview.WebServiceParameterSet;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStreamReader;
38 import java.io.OutputStreamWriter;
39 import java.io.PrintWriter;
40 import java.util.ArrayList;
41 import java.util.Hashtable;
42 import java.util.List;
43 import java.util.StringTokenizer;
45 import javax.xml.bind.JAXBContext;
46 import javax.xml.bind.JAXBElement;
47 import javax.xml.bind.Marshaller;
48 import javax.xml.stream.XMLInputFactory;
49 import javax.xml.stream.XMLStreamReader;
52 * store and retrieve web service parameter sets.
57 public class WsParamSetManager implements ParamManager
59 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<>();
62 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
63 boolean modifiable, boolean unmodifiable)
65 String files = Cache.getProperty("WS_PARAM_FILES");
70 StringTokenizer st = new StringTokenizer(files, "|");
72 List<WsParamSetI> params = new ArrayList<>();
73 while (st.hasMoreTokens())
75 pfile = st.nextToken();
78 WsParamSetI[] pset = parseParamFile(pfile);
79 for (WsParamSetI p : pset)
82 if (serviceUrl != null)
84 for (String url : p.getApplicableUrls())
86 if (url.equals(serviceUrl))
96 add &= (modifiable == p.isModifiable()
97 || unmodifiable == !p.isModifiable());
98 add &= name == null || p.getName().equals(name);
107 } catch (IOException e)
109 Cache.log.info("Failed to parse parameter file " + pfile
110 + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
114 return params.toArray(new WsParamSetI[0]);
117 private WsParamSetI[] parseParamFile(String filename) throws IOException
119 List<WsParamSetI> psets = new ArrayList<>();
120 InputStreamReader is = new InputStreamReader(
121 new FileInputStream(new File(filename)), "UTF-8");
123 WebServiceParameterSet wspset = null;
126 JAXBContext jc = JAXBContext
127 .newInstance("jalview.xml.binding.jalview");
128 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
129 XMLStreamReader streamReader = XMLInputFactory.newInstance()
130 .createXMLStreamReader(is);
131 JAXBElement<WebServiceParameterSet> jbe = um.unmarshal(streamReader,
132 WebServiceParameterSet.class);
133 wspset = jbe.getValue();
134 } catch (Exception ex)
136 throw new IOException(ex);
139 if (wspset != null && wspset.getParameters().length() > 0)
141 List<String> urls = wspset.getServiceURL();
142 final String[] urlArray = urls.toArray(new String[urls.size()]);
144 for (String url : urls)
146 ParamDatastoreI parser = paramparsers.get(url);
149 WsParamSetI pset = parser.parseServiceParameterFile(
150 wspset.getName(), wspset.getDescription(),
151 urlArray, wspset.getParameters());
154 pset.setSourceFile(filename);
162 return psets.toArray(new WsParamSetI[0]);
166 public void storeParameterSet(WsParamSetI parameterSet)
168 String filename = parameterSet.getSourceFile();
172 if (filename != null && !((outfile = new File(filename)).canWrite()))
174 Cache.log.warn("Can't write to " + filename
175 + " - Prompting for new file to write to.");
178 } catch (Exception e)
183 ParamDatastoreI parser = null;
184 for (String urls : parameterSet.getApplicableUrls())
188 parser = paramparsers.get(urls);
193 throw new Error(MessageManager.getString(
194 "error.implementation_error_cannot_find_marshaller_for_param_set"));
196 if (filename == null)
198 // TODO: JAL-3048 webservice - not required for Jalview-JS
200 JalviewFileChooser chooser = new JalviewFileChooser("wsparams",
201 "Web Service Parameter File");
202 chooser.setFileView(new JalviewFileView());
203 chooser.setDialogTitle(MessageManager
204 .getString("label.choose_filename_for_param_file"));
205 chooser.setToolTipText(MessageManager.getString("action.save"));
206 int value = chooser.showSaveDialog(Desktop.instance);
207 if (value == JalviewFileChooser.APPROVE_OPTION)
209 outfile = chooser.getSelectedFile();
210 Cache.setProperty("LAST_DIRECTORY", outfile.getParent());
211 filename = outfile.getAbsolutePath();
212 if (!filename.endsWith(".wsparams"))
214 filename = filename.concat(".wsparams");
215 outfile = new File(filename);
221 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES",
223 if (paramFiles.indexOf(filename) == -1)
225 if (paramFiles.length() > 0)
227 paramFiles = paramFiles.concat("|");
229 paramFiles = paramFiles.concat(filename);
231 Cache.setProperty("WS_PARAM_FILES", paramFiles);
233 WebServiceParameterSet paramxml = new WebServiceParameterSet();
235 paramxml.setName(parameterSet.getName());
236 paramxml.setDescription(parameterSet.getDescription());
237 for (String url : parameterSet.getApplicableUrls())
239 paramxml.getServiceURL().add(url);
241 paramxml.setVersion("1.0");
244 paramxml.setParameters(
245 parser.generateServiceParameterFile(parameterSet));
246 PrintWriter out = new PrintWriter(new OutputStreamWriter(
247 new FileOutputStream(outfile), "UTF-8"));
248 JAXBContext jaxbContext = JAXBContext
249 .newInstance(WebServiceParameterSet.class);
250 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
251 jaxbMarshaller.marshal(
252 new ObjectFactory().createWebServiceParameterSet(paramxml),
255 parameterSet.setSourceFile(filename);
256 } catch (Exception e)
258 Cache.log.error("Couldn't write parameter file to " + outfile, e);
265 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
266 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
267 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
268 * jalview.io.JalviewFileView());
269 * chooser.setDialogTitle("Load colour scheme");
270 * chooser.setToolTipText("Load");
272 * int value = chooser.showOpenDialog(this);
274 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
275 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
276 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
277 * "USER_DEFINED_COLOURS", choice.getPath()); if
278 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
279 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
282 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
286 public void deleteParameterSet(WsParamSetI parameterSet)
288 String filename = parameterSet.getSourceFile();
289 if (filename == null || filename.trim().length() < 1)
293 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES", "");
294 if (paramFiles.indexOf(filename) > -1)
296 String nparamFiles = new String();
297 StringTokenizer st = new StringTokenizer(paramFiles, "|");
298 while (st.hasMoreElements())
300 String fl = st.nextToken();
301 if (!fl.equals(filename))
303 nparamFiles = nparamFiles.concat("|").concat(fl);
306 jalview.bin.Cache.setProperty("WS_PARAM_FILES", nparamFiles);
311 File pfile = new File(filename);
312 if (pfile.exists() && pfile.canWrite())
314 if (JvOptionPane.showConfirmDialog(Desktop.instance,
315 "Delete the preset's file, too ?", "Delete User Preset ?",
316 JvOptionPane.OK_CANCEL_OPTION) == JvOptionPane.OK_OPTION)
321 } catch (Exception e)
324 "Exception when trying to delete webservice user preset: ",
330 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
332 paramparsers.put(hosturl, paramdataStore);