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
47 * @throws MalformedURLException
\r
49 static <T> List<Option<T>> getParametersList(Metadata<T> msaws, String host) throws MalformedURLException {
\r
50 assert msaws != null;
\r
51 RunnerConfig<T> config = msaws.getRunnerOptions();
\r
52 if (config == null) {
\r
53 return Collections.emptyList();
\r
55 List<Option<T>> opts = config.getArguments();
\r
56 for (Option<T> o : opts) {
\r
57 o.setBasicURL(new URL(host + "/"));
\r
62 static <T> List<Option<T>> getParametersList(Metadata<T> msaws) throws MalformedURLException {
\r
63 return getParametersList (msaws, "http://unknown.jabaws.server.ac.uk");
\r
68 * Returns an objects from which the list of presets supported by web
\r
69 * service <T> can be obtained
\r
75 * @return PresetManager, object which operates on presets
\r
77 static <T> PresetManager<T> getPresetList(Metadata<T> msaws) {
\r
78 assert msaws != null;
\r
79 PresetManager<T> presetman = msaws.getPresets();
\r
84 * Returns a list of limits supported by web service Each limit correspond
\r
85 * to a particular preset.
\r
91 * @return List of limits supported by a web service
\r
93 static <T> List<Limit<T>> getLimits(Metadata<T> msaws) {
\r
94 assert msaws != null;
\r
95 LimitsManager<T> lmanger = msaws.getLimits();
\r
97 return lmanger != null ? lmanger.getLimits() : null;
\r
101 * Returns {@code Preset} by its name
\r
106 * @param presetName
\r
107 * @return Return a Preset by its optionName
\r
109 static <T> Preset<T> getPreset(Metadata<T> msaws, String presetName) {
\r
110 assert presetName != null;
\r
111 PresetManager<T> presets = MetadataHelper.getPresetList(msaws);
\r
112 if (presets == null) {
\r
113 System.out.println("No presets are supported by the service! Ignoring -r directive!");
\r
116 Preset<T> pre = presets.getPresetByName(presetName);
\r
118 System.out.println("Cannot find preset: " + presetName + " WARN: ignoring -r directive!");
\r
124 * Converts options supplied via parameters file into {@code Option} objects
\r
130 * @return List of Options of type T
\r
132 static <T> List<Option<T>> processParameters(List<String> params,
\r
133 RunnerConfig<T> options) {
\r
134 List<Option<T>> chosenOptions = new ArrayList<Option<T>>();
\r
135 for (String param : params) {
\r
136 String oname = null;
\r
137 if (isParameter(param)) {
\r
138 oname = getParamName(param);
\r
142 Option<T> o = options.getArgumentByOptionName(oname);
\r
144 System.out.println("WARN ignoring unsuppoted parameter: " + oname);
\r
147 if (isParameter(param)) {
\r
149 o.setValue(getParamValue(param));
\r
150 } catch (WrongParameterException e) {
\r
151 System.out.println("Problem setting value for the parameter: " + param);
\r
152 e.printStackTrace();
\r
155 chosenOptions.add(o);
\r
157 return chosenOptions;
\r
160 static String getParamName(String fullName) {
\r
161 assert isParameter(fullName);
\r
162 return fullName.substring(0, fullName.indexOf(pseparator));
\r
165 static String getParamValue(String fullName) {
\r
166 assert isParameter(fullName);
\r
167 return fullName.substring(fullName.indexOf(pseparator) + 1);
\r
170 static boolean isParameter(String param) {
\r
171 return param.contains(pseparator);
\r