1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.ws.client;
\r
20 import static compbio.ws.client.Constraints.pseparator;
\r
22 import java.util.ArrayList;
\r
23 import java.util.Collections;
\r
24 import java.util.List;
\r
25 import java.net.MalformedURLException;
\r
26 import java.net.URL;
\r
28 import compbio.data.msa.Metadata;
\r
29 import compbio.metadata.Limit;
\r
30 import compbio.metadata.LimitsManager;
\r
31 import compbio.metadata.Option;
\r
32 import compbio.metadata.Preset;
\r
33 import compbio.metadata.PresetManager;
\r
34 import compbio.metadata.RunnerConfig;
\r
35 import compbio.metadata.WrongParameterException;
\r
37 public class MetadataHelper {
\r
40 * Returns a list of options supported by web service
\r
46 * @return List of options supported by a web service
\r
48 static <T> List<Option<T>> getParametersList(Metadata<T> msaws) {
\r
49 assert msaws != null;
\r
50 RunnerConfig<T> config = msaws.getRunnerOptions();
\r
51 if (config == null) {
\r
52 return Collections.emptyList();
\r
54 return config.getArguments();
\r
58 * Returns a list of options supported by web service
\r
65 * the server URL, as a string
\r
66 * @return List of options supported by a web service
\r
68 static <T> List<Option<T>> getParametersList(Metadata<T> msaws, String host) throws MalformedURLException {
\r
69 assert msaws != null;
\r
70 RunnerConfig<T> config = msaws.getRunnerOptions();
\r
71 if (config == null) {
\r
72 return Collections.emptyList();
\r
74 List<Option<T>> opts = config.getArguments();
\r
75 for (Option<T> o : opts) {
\r
76 o.setBasicURL(new URL(host + "/"));
\r
82 * Returns an objects from which the list of presets supported by web
\r
83 * service <T> can be obtained
\r
89 * @return PresetManager, object which operates on presets
\r
91 static <T> PresetManager<T> getPresetList(Metadata<T> msaws) {
\r
92 assert msaws != null;
\r
93 PresetManager<T> presetman = msaws.getPresets();
\r
98 * Returns a list of limits supported by web service Each limit correspond
\r
99 * to a particular preset.
\r
104 * web service proxy
\r
105 * @return List of limits supported by a web service
\r
107 static <T> List<Limit<T>> getLimits(Metadata<T> msaws) {
\r
108 assert msaws != null;
\r
109 LimitsManager<T> lmanger = msaws.getLimits();
\r
111 return lmanger != null ? lmanger.getLimits() : null;
\r
115 * Returns {@code Preset} by its name
\r
120 * @param presetName
\r
121 * @return Return a Preset by its optionName
\r
123 static <T> Preset<T> getPreset(Metadata<T> msaws, String presetName) {
\r
124 assert presetName != null;
\r
125 PresetManager<T> presets = MetadataHelper.getPresetList(msaws);
\r
126 if (presets == null) {
\r
127 System.out.println("No presets are supported by the service! Ignoring -r directive!");
\r
130 Preset<T> pre = presets.getPresetByName(presetName);
\r
132 System.out.println("Cannot find preset: " + presetName + " WARN: ignoring -r directive!");
\r
138 * Converts options supplied via parameters file into {@code Option} objects
\r
144 * @return List of Options of type T
\r
146 static <T> List<Option<T>> processParameters(List<String> params,
\r
147 RunnerConfig<T> options) {
\r
148 List<Option<T>> chosenOptions = new ArrayList<Option<T>>();
\r
149 for (String param : params) {
\r
150 String oname = null;
\r
151 if (isParameter(param)) {
\r
152 oname = getParamName(param);
\r
156 Option<T> o = options.getArgumentByOptionName(oname);
\r
158 System.out.println("WARN ignoring unsuppoted parameter: " + oname);
\r
161 if (isParameter(param)) {
\r
163 o.setValue(getParamValue(param));
\r
164 } catch (WrongParameterException e) {
\r
165 System.out.println("Problem setting value for the parameter: " + param);
\r
166 e.printStackTrace();
\r
169 chosenOptions.add(o);
\r
171 return chosenOptions;
\r
174 static String getParamName(String fullName) {
\r
175 assert isParameter(fullName);
\r
176 return fullName.substring(0, fullName.indexOf(pseparator));
\r
179 static String getParamValue(String fullName) {
\r
180 assert isParameter(fullName);
\r
181 return fullName.substring(fullName.indexOf(pseparator) + 1);
\r
184 static boolean isParameter(String param) {
\r
185 return param.contains(pseparator);
\r