/* Copyright (c) 2009 Peter Troshin * * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache * License for more details. * * A copy of the license is in apache_license.txt. It is also available here: * @see: http://www.apache.org/licenses/LICENSE-2.0.txt * * Any republication or derived work distributed in source code form * must include this copyright and license notice. */ package compbio.metadata; import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.List; import javax.xml.bind.ValidationException; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; import compbio.util.SysPrefs; import compbio.util.annotation.NotThreadSafe; /** * The list of {@link Parameter}s and {@link Option}s supported by executable. * * @author pvtroshin * * Date October 2009 * @param * type of an Executable */ @XmlRootElement @NotThreadSafe public class RunnerConfig { /** * Please note that the order of the fields is important to generate xml * compliant to the hand written schema!!! */ /** * The class name of a runnable e.g. T */ private String runnerClassName; List> options = new ArrayList>(); String prmSeparator; List> parameters = new ArrayList>(); @XmlTransient List> arguments; public RunnerConfig() { // JAXB default constructor } public RunnerConfig copyAndValidateRConfig(RunnerConfig runnerConf) { if (this.runnerClassName != runnerConf.runnerClassName) { throw new InvalidParameterException("Wrong runner configuration! "); } RunnerConfig newrconfig = new RunnerConfig(); newrconfig.runnerClassName = runnerConf.runnerClassName; newrconfig.options = new ArrayList>(options); return newrconfig; } /** * * @return list of {@link Option} supported by type T */ public List> getOptions() { return options; } public void addParameter(Parameter param) { assert param != null; parameters.add(param); } public void addOption(Option option) { assert option != null; options.add(option); } /** * * @return list of {@link Option} and {@link Parameter} supported by type T */ @XmlTransient public List> getArguments() { arguments = new ArrayList>(options); arguments.addAll(parameters); return arguments; } /** * * @return name value separator character */ public String getPrmSeparator() { return prmSeparator; } public void setPrmSeparator(String prmSeparator) { this.prmSeparator = prmSeparator; } public void setOptions(List> parameters) { this.options = parameters; } /** * * @return fully qualified class name for type T */ @XmlElement(required = true) public String getRunnerClassName() { return runnerClassName; } public void setRunnerClassName(String runnerClassName) { this.runnerClassName = runnerClassName; } public void setParameters(List> parameters) { this.parameters = parameters; } /** * * @return List of {@link Parameter} supported by type T. */ public List> getParameters() { return parameters; } @Override public String toString() { String value = "Runner: " + this.runnerClassName + SysPrefs.newlinechar; for (Option par : this.getArguments()) { value += par; } return value; } /* * Cast is safe as runnerClassNames equality checked (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (obj == null) { return false; } RunnerConfig rconf = null; if (obj instanceof RunnerConfig) { rconf = (RunnerConfig) obj; } if (!rconf.runnerClassName.equals(this.runnerClassName)) { return false; } if (this.options.size() != rconf.options.size()) { return false; } if (this.parameters.size() != rconf.parameters.size()) { return false; } if (!this.prmSeparator.equals(rconf.prmSeparator)) { return false; } // Size of option list is the same at that point for (Option op : options) { Option roption = (Option) rconf.getArgument(op.getName()); if (roption == null) { return false; } if (!op.equals(roption)) { return false; } } // Size of parameters list is the same at that point for (Parameter par : parameters) { Parameter rpar = (Parameter) rconf.getArgument(par.getName()); if (rpar == null) { return false; } if (!par.equals(rpar)) { return false; } } return true; } /** * Returns the argument by its name if found, NULL otherwise * * @param name * @return {@link Argument} */ public Option getArgument(String name) { for (Option par : getArguments()) { if (par.getName().equalsIgnoreCase(name)) { return par; } } return null; } /** * Removes the argument {@link Argument} if found. * * @param name * of the argument * @return true if argument was removed, false otherwise */ @SuppressWarnings("unchecked") // Just use raw type in instanceof this is safe public boolean removeArgument(String name) { Option par = getArgument(name); if (par != null) { if (par instanceof Parameter) { parameters.remove(par); return true; } else { this.options.remove(par); return true; } } return false; } /** * Returns the argument by option name, NULL if the argument is not found * * @param optionName * - the name of the option * @return Option */ public Option getArgumentByOptionName(String optionName) { for (Option opt : getArguments()) { for (String val : opt.getOptionNames()) { if (val.equalsIgnoreCase(optionName)) { return opt; } } } return null; } /** * Removes the argument * * @param optionName * @return true if argument with optionName exists and was removed, false * otherwise */ @SuppressWarnings("unchecked") // Just use raw type in instanceof this is safe public boolean removeArgumentByOptionName(String optionName) { Option par = getArgumentByOptionName(optionName); if (par != null) { if (par instanceof Parameter) { this.parameters.remove(par); return true; } else { this.options.remove(par); return true; } } return false; } /** * Validate the arguments * * @throws ValidationException * if any of the arguments found invalid which is when *
*
  • Parameter value outside {@link ValueConstrain} boundary
  • *
  • Parameter name is not listed in possible values
  • *
    */ public void validate() throws ValidationException { for (Option option : getArguments()) { option.validate(); } } }