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 private static final String WS_PARAM_FILES = "WS_PARAM_FILES";
60 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<>();
63 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
64 boolean modifiable, boolean unmodifiable)
66 String files = Cache.getProperty(WS_PARAM_FILES);
71 StringTokenizer st = new StringTokenizer(files, "|");
73 List<WsParamSetI> params = new ArrayList<>();
74 while (st.hasMoreTokens())
76 pfile = st.nextToken();
79 WsParamSetI[] pset = parseParamFile(pfile);
80 for (WsParamSetI p : pset)
83 if (serviceUrl != null)
85 for (String url : p.getApplicableUrls())
87 if (url.equals(serviceUrl))
97 add &= (modifiable == p.isModifiable()
98 || unmodifiable == !p.isModifiable());
99 add &= name == null || p.getName().equals(name);
108 } catch (IOException e)
110 Cache.log.info("Failed to parse parameter file " + pfile
111 + " (Check that all " + WS_PARAM_FILES
112 + " entries are valid!)",
116 return params.toArray(new WsParamSetI[0]);
119 private WsParamSetI[] parseParamFile(String filename) throws IOException
121 List<WsParamSetI> psets = new ArrayList<>();
122 InputStreamReader is = new InputStreamReader(
123 new FileInputStream(new File(filename)), "UTF-8");
125 WebServiceParameterSet wspset = null;
128 JAXBContext jc = JAXBContext
129 .newInstance("jalview.xml.binding.jalview");
130 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
131 XMLStreamReader streamReader = XMLInputFactory.newInstance()
132 .createXMLStreamReader(is);
133 JAXBElement<WebServiceParameterSet> jbe = um.unmarshal(streamReader,
134 WebServiceParameterSet.class);
135 wspset = jbe.getValue();
136 } catch (Exception ex)
138 throw new IOException(ex);
141 if (wspset != null && wspset.getParameters().length() > 0)
143 List<String> urls = wspset.getServiceURL();
144 final String[] urlArray = urls.toArray(new String[urls.size()]);
146 for (String url : urls)
148 ParamDatastoreI parser = paramparsers.get(url);
151 WsParamSetI pset = parser.parseServiceParameterFile(
152 wspset.getName(), wspset.getDescription(),
153 urlArray, wspset.getParameters());
156 pset.setSourceFile(filename);
164 return psets.toArray(new WsParamSetI[0]);
168 public void storeParameterSet(WsParamSetI parameterSet)
170 String filename = parameterSet.getSourceFile();
174 if (filename != null && !((outfile = new File(filename)).canWrite()))
176 Cache.log.warn("Can't write to " + filename
177 + " - Prompting for new file to write to.");
180 } catch (Exception e)
185 ParamDatastoreI parser = null;
186 for (String urls : parameterSet.getApplicableUrls())
190 parser = paramparsers.get(urls);
195 throw new Error(MessageManager.getString(
196 "error.implementation_error_cannot_find_marshaller_for_param_set"));
198 if (filename == null)
200 // TODO: JAL-3048 webservice - not required for Jalview-JS
202 JalviewFileChooser chooser = new JalviewFileChooser("wsparams",
203 "Web Service Parameter File");
204 chooser.setFileView(new JalviewFileView());
205 chooser.setDialogTitle(MessageManager
206 .getString("label.choose_filename_for_param_file"));
207 chooser.setToolTipText(MessageManager.getString("action.save"));
208 int value = chooser.showSaveDialog(Desktop.getInstance());
209 if (value == JalviewFileChooser.APPROVE_OPTION)
211 outfile = chooser.getSelectedFile();
212 Cache.setProperty("LAST_DIRECTORY", outfile.getParent());
213 filename = outfile.getAbsolutePath();
214 if (!filename.endsWith(".wsparams"))
216 filename = filename.concat(".wsparams");
217 outfile = new File(filename);
223 String paramFiles = jalview.bin.Cache.getDefault(WS_PARAM_FILES,
225 if (paramFiles.indexOf(filename) == -1)
227 if (paramFiles.length() > 0)
229 paramFiles = paramFiles.concat("|");
231 paramFiles = paramFiles.concat(filename);
234 Cache.setProperty(WS_PARAM_FILES, paramFiles);
236 WebServiceParameterSet paramxml = new WebServiceParameterSet();
238 paramxml.setName(parameterSet.getName());
239 paramxml.setDescription(parameterSet.getDescription());
240 for (String url : parameterSet.getApplicableUrls())
242 paramxml.getServiceURL().add(url);
244 paramxml.setVersion("1.0");
247 paramxml.setParameters(
248 parser.generateServiceParameterFile(parameterSet));
249 PrintWriter out = new PrintWriter(new OutputStreamWriter(
250 new FileOutputStream(outfile), "UTF-8"));
251 JAXBContext jaxbContext = JAXBContext
252 .newInstance(WebServiceParameterSet.class);
253 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
254 jaxbMarshaller.marshal(
255 new ObjectFactory().createWebServiceParameterSet(paramxml),
258 parameterSet.setSourceFile(filename);
259 } catch (Exception e)
261 Cache.log.error("Couldn't write parameter file to " + outfile, e);
268 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
269 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
270 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
271 * jalview.io.JalviewFileView());
272 * chooser.setDialogTitle("Load colour scheme");
273 * chooser.setToolTipText("Load");
275 * int value = chooser.showOpenDialog(this);
277 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
278 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
279 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
280 * "USER_DEFINED_COLOURS", choice.getPath()); if
281 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
282 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
285 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
289 public void deleteParameterSet(WsParamSetI parameterSet)
291 String filename = parameterSet.getSourceFile();
292 if (filename == null || filename.trim().length() < 1)
296 String paramFiles = jalview.bin.Cache.getDefault(WS_PARAM_FILES, "");
297 if (paramFiles.indexOf(filename) > -1)
299 String nparamFiles = new String();
300 StringTokenizer st = new StringTokenizer(paramFiles, "|");
301 while (st.hasMoreElements())
303 String fl = st.nextToken();
304 if (!fl.equals(filename))
306 nparamFiles = nparamFiles.concat("|").concat(fl);
309 jalview.bin.Cache.setProperty(WS_PARAM_FILES, nparamFiles);
314 File pfile = new File(filename);
315 if (pfile.exists() && pfile.canWrite())
317 if (JvOptionPane.showConfirmDialog(Desktop.getInstance(),
318 "Delete the preset's file, too ?", "Delete User Preset ?",
319 JvOptionPane.OK_CANCEL_OPTION) == JvOptionPane.OK_OPTION)
324 } catch (Exception e)
327 "Exception when trying to delete webservice user preset: ",
333 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
335 paramparsers.put(hosturl, paramdataStore);