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.bin.Console;
25 import jalview.io.JalviewFileChooser;
26 import jalview.io.JalviewFileView;
27 import jalview.util.MessageManager;
28 import jalview.ws.params.ParamDatastoreI;
29 import jalview.ws.params.ParamManager;
30 import jalview.ws.params.WsParamSetI;
31 import jalview.xml.binding.jalview.ObjectFactory;
32 import jalview.xml.binding.jalview.WebServiceParameterSet;
35 import java.io.FileInputStream;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStreamReader;
39 import java.io.OutputStreamWriter;
40 import java.io.PrintWriter;
41 import java.util.ArrayList;
42 import java.util.Hashtable;
43 import java.util.List;
44 import java.util.StringTokenizer;
46 import javax.xml.bind.JAXBContext;
47 import javax.xml.bind.JAXBElement;
48 import javax.xml.bind.Marshaller;
49 import javax.xml.stream.XMLInputFactory;
50 import javax.xml.stream.XMLStreamReader;
53 * store and retrieve web service parameter sets.
58 public class WsParamSetManager implements ParamManager
60 private static final String WS_PARAM_FILES = "WS_PARAM_FILES";
61 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<>();
64 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
65 boolean modifiable, boolean unmodifiable)
67 String files = Cache.getProperty(WS_PARAM_FILES);
72 StringTokenizer st = new StringTokenizer(files, "|");
74 List<WsParamSetI> params = new ArrayList<>();
75 while (st.hasMoreTokens())
77 pfile = st.nextToken();
80 WsParamSetI[] pset = parseParamFile(pfile);
81 for (WsParamSetI p : pset)
84 if (serviceUrl != null)
86 for (String url : p.getApplicableUrls())
88 if (url.equals(serviceUrl))
98 add &= (modifiable == p.isModifiable()
99 || unmodifiable == !p.isModifiable());
100 add &= name == null || p.getName().equals(name);
109 } catch (IOException e)
111 Console.info("Failed to parse parameter file " + pfile
112 + " (Check that all JALVIEW_WSPARAMFILES 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 Console.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 = Cache.getDefault("WS_PARAM_FILES", filename);
224 if (paramFiles.indexOf(filename) == -1)
226 if (paramFiles.length() > 0)
228 paramFiles = paramFiles.concat("|");
230 paramFiles = paramFiles.concat(filename);
232 Cache.setProperty(WS_PARAM_FILES, paramFiles);
234 WebServiceParameterSet paramxml = new WebServiceParameterSet();
236 paramxml.setName(parameterSet.getName());
237 paramxml.setDescription(parameterSet.getDescription());
238 for (String url : parameterSet.getApplicableUrls())
240 paramxml.getServiceURL().add(url);
242 paramxml.setVersion("1.0");
245 paramxml.setParameters(
246 parser.generateServiceParameterFile(parameterSet));
247 PrintWriter out = new PrintWriter(new OutputStreamWriter(
248 new FileOutputStream(outfile), "UTF-8"));
249 JAXBContext jaxbContext = JAXBContext
250 .newInstance(WebServiceParameterSet.class);
251 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
252 jaxbMarshaller.marshal(
253 new ObjectFactory().createWebServiceParameterSet(paramxml),
256 parameterSet.setSourceFile(filename);
257 } catch (Exception e)
259 Console.error("Couldn't write parameter file to " + outfile, e);
266 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
267 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
268 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
269 * jalview.io.JalviewFileView());
270 * chooser.setDialogTitle("Load colour scheme");
271 * chooser.setToolTipText("Load");
273 * int value = chooser.showOpenDialog(this);
275 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
276 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
277 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
278 * "USER_DEFINED_COLOURS", choice.getPath()); if
279 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
280 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
283 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
287 public void deleteParameterSet(WsParamSetI parameterSet)
289 String filename = parameterSet.getSourceFile();
290 if (filename == null || filename.trim().length() < 1)
294 String paramFiles = Cache.getDefault(WS_PARAM_FILES, "");
295 if (paramFiles.indexOf(filename) > -1)
297 String nparamFiles = new String();
298 StringTokenizer st = new StringTokenizer(paramFiles, "|");
299 while (st.hasMoreElements())
301 String fl = st.nextToken();
302 if (!fl.equals(filename))
304 nparamFiles = nparamFiles.concat("|").concat(fl);
307 Cache.setProperty(WS_PARAM_FILES, nparamFiles);
312 File pfile = new File(filename);
313 if (pfile.exists() && pfile.canWrite())
315 if (JvOptionPane.showConfirmDialog(Desktop.getInstance(),
316 "Delete the preset's file, too ?", "Delete User Preset ?",
317 JvOptionPane.OK_CANCEL_OPTION) == JvOptionPane.OK_OPTION)
322 } catch (Exception e)
325 "Exception when trying to delete webservice user preset: ",
331 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
333 paramparsers.put(hosturl, paramdataStore);