You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@taverna.apache.org by re...@apache.org on 2015/03/24 11:45:26 UTC
[4/5] incubator-taverna-commandline git commit: package name changes
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/InputsHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/InputsHandler.java b/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/InputsHandler.java
deleted file mode 100644
index 6f91514..0000000
--- a/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/InputsHandler.java
+++ /dev/null
@@ -1,277 +0,0 @@
-/*******************************************************************************
- * Copyright (C) 2007 The University of Manchester
- *
- * Modifications to the initial code base are copyright of their
- * respective authors, or their employers as appropriate.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This program 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 GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- ******************************************************************************/
-package net.sf.taverna.t2.commandline.data;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardOpenOption;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import net.sf.taverna.t2.commandline.exceptions.InputMismatchException;
-import net.sf.taverna.t2.commandline.exceptions.InvalidOptionException;
-import net.sf.taverna.t2.commandline.exceptions.ReadInputException;
-import net.sf.taverna.t2.commandline.options.CommandLineOptions;
-import net.sf.taverna.t2.invocation.InvocationContext;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.log4j.Logger;
-import org.apache.taverna.databundle.DataBundles;
-import org.apache.taverna.robundle.Bundle;
-import org.apache.taverna.scufl2.api.port.InputWorkflowPort;
-
-/**
- * Handles the reading, or processing, or input values according to arguments provided to the
- * commandline.
- * The may be either as direct values, from a file, or from a Baclava document.
- * Also handles registering the input values with the Data Service, ready to initiate
- * the workflow run.
- *
- * @author Stuart Owen
- */
-public class InputsHandler {
-
- private static Logger logger = Logger.getLogger(InputsHandler.class);
-
- public void checkProvidedInputs(Map<String, InputWorkflowPort> portMap,
- CommandLineOptions options) throws InputMismatchException {
- // we dont check for the document
- Set<String> providedInputNames = new HashSet<String>();
- for (int i = 0; i < options.getInputFiles().length; i += 2) {
- // If it already contains a value for the input port, e.g
- // two inputs are provided for the same port
- if (providedInputNames.contains(options.getInputFiles()[i])) {
- throw new InputMismatchException(
- "Two input values were provided for the same input port "
- + options.getInputFiles()[i] + ".", null, null);
- }
- providedInputNames.add(options.getInputFiles()[i]);
- }
-
- for (int i = 0; i < options.getInputValues().length; i += 2) {
- // If it already contains a value for the input port, e.g
- // two inputs are provided for the same port
- if (providedInputNames.contains(options.getInputValues()[i])) {
- throw new InputMismatchException(
- "Two input values were provided for the same input port "
- + options.getInputValues()[i] + ".", null, null);
- }
- providedInputNames.add(options.getInputValues()[i]);
- }
-
- if (portMap.size() * 2 != (options.getInputFiles().length + options.getInputValues().length)) {
- throw new InputMismatchException(
- "The number of inputs provided does not match the number of input ports.",
- portMap.keySet(), providedInputNames);
- }
-
- for (String portName : portMap.keySet()) {
- if (!providedInputNames.contains(portName)) {
- throw new InputMismatchException(
- "The provided inputs does not contain an input for the port '"
- + portName + "'", portMap.keySet(), providedInputNames);
- }
- }
- }
-
- public Bundle registerInputs(Map<String, InputWorkflowPort> portMap,
- CommandLineOptions options, InvocationContext context) throws InvalidOptionException,
- ReadInputException, IOException {
- Bundle inputDataBundle;
- inputDataBundle = DataBundles.createBundle();
- inputDataBundle.setDeleteOnClose(false);
- System.out.println("Bundle: " + inputDataBundle.getSource());
-
- Path inputs = DataBundles.getInputs(inputDataBundle);
-
- URL url;
- try {
- url = new URL("file:");
- } catch (MalformedURLException e1) {
- // Should never happen, but just in case:
- throw new ReadInputException(
- "The was an internal error setting up the URL to open the inputs. You should contact Taverna support.",
- e1);
- }
-
- if (options.hasInputFiles()) {
- regesterInputsFromFiles(portMap, options, inputs, url);
- }
-
- if (options.hasInputValues()) {
- registerInputsFromValues(portMap, options, inputs);
-
- }
-
- return inputDataBundle;
- }
-
- private void registerInputsFromValues(Map<String, InputWorkflowPort> portMap,
- CommandLineOptions options, Path inputs) throws InvalidOptionException {
- String[] inputParams = options.getInputValues();
- for (int i = 0; i < inputParams.length; i = i + 2) {
- String inputName = inputParams[i];
- try {
- String inputValue = inputParams[i + 1];
- InputWorkflowPort port = portMap.get(inputName);
-
- if (port == null) {
- throw new InvalidOptionException("Cannot find an input port named '"
- + inputName + "'");
- }
-
- Path portPath = DataBundles.getPort(inputs, inputName);
- if (options.hasDelimiterFor(inputName)) {
- String delimiter = options.inputDelimiter(inputName);
- Object value = checkForDepthMismatch(1, port.getDepth(), inputName,
- inputValue.split(delimiter));
- setValue(portPath, value);
- } else {
- Object value = checkForDepthMismatch(0, port.getDepth(), inputName, inputValue);
- setValue(portPath, value);
- }
-
- } catch (IndexOutOfBoundsException e) {
- throw new InvalidOptionException("Missing input value for input " + inputName);
- } catch (IOException e) {
- throw new InvalidOptionException("Error creating value for input " + inputName);
- }
- }
- }
-
- private void regesterInputsFromFiles(Map<String, InputWorkflowPort> portMap,
- CommandLineOptions options, Path inputs, URL url) throws InvalidOptionException {
- String[] inputParams = options.getInputFiles();
- for (int i = 0; i < inputParams.length; i = i + 2) {
- String inputName = inputParams[i];
- try {
- URL inputURL = new URL(url, inputParams[i + 1]);
- InputWorkflowPort port = portMap.get(inputName);
-
- if (port == null) {
- throw new InvalidOptionException("Cannot find an input port named '"
- + inputName + "'");
- }
-
- Path portPath = DataBundles.getPort(inputs, inputName);
- if (options.hasDelimiterFor(inputName)) {
- String delimiter = options.inputDelimiter(inputName);
- Object value = IOUtils.toString(inputURL.openStream()).split(delimiter);
- value = checkForDepthMismatch(1, port.getDepth(), inputName, value);
- setValue(portPath, value);
- } else {
- Object value = IOUtils.toByteArray(inputURL.openStream());
- value = checkForDepthMismatch(0, port.getDepth(), inputName, value);
- setValue(portPath, value);
- }
- } catch (IndexOutOfBoundsException e) {
- throw new InvalidOptionException("Missing input filename for input " + inputName);
- } catch (IOException e) {
- throw new InvalidOptionException("Could not read input " + inputName + ": "
- + e.getMessage());
- }
- }
- }
-
- private void setValue(Path port, Object userInput) throws IOException {
- if (userInput instanceof File) {
- DataBundles.setReference(port, ((File) userInput).toURI());
- } else if (userInput instanceof URL) {
- try {
- DataBundles.setReference(port, ((URL) userInput).toURI());
- } catch (URISyntaxException e) {
- logger.warn(String.format("Error converting %1$s to URI", userInput), e);
- }
- } else if (userInput instanceof String) {
- DataBundles.setStringValue(port, (String) userInput);
- } else if (userInput instanceof byte[]) {
- Files.write(port, (byte[]) userInput, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
- } else if (userInput instanceof List<?>) {
- DataBundles.createList(port);
- List<?> list = (List<?>) userInput;
- for (Object object : list) {
- setValue(DataBundles.newListItem(port), object);
- }
- } else {
- logger.warn("Unknown input type : " + userInput.getClass().getName());
- }
- }
-
- private Object checkForDepthMismatch(int inputDepth, int portDepth, String inputName,
- Object inputValue) throws InvalidOptionException {
- if (inputDepth != portDepth) {
- if (inputDepth < portDepth) {
- logger.warn("Wrapping input for '" + inputName + "' from a depth of " + inputDepth
- + " to the required depth of " + portDepth);
- while (inputDepth < portDepth) {
- List<Object> l = new ArrayList<Object>();
- l.add(inputValue);
- inputValue = l;
- inputDepth++;
- }
- } else {
- String msg = "There is a mismatch between depth of the list for the input port '"
- + inputName + "' and the data presented. The input port requires a "
- + depthToString(portDepth) + " and the data presented is a "
- + depthToString(inputDepth);
- throw new InvalidOptionException(msg);
- }
- }
-
- return inputValue;
- }
-
- private String depthToString(int depth) {
- switch (depth) {
- case 0:
- return "single item";
- case 1:
- return "list";
- case 2:
- return "list of lists";
- default:
- return "list of depth " + depth;
- }
- }
-
- private int getObjectDepth(Object o) {
- int result = 0;
- if (o instanceof Iterable) {
- result++;
- Iterator i = ((Iterable) o).iterator();
-
- if (i.hasNext()) {
- Object child = i.next();
- result = result + getObjectDepth(child);
- }
- }
- return result;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/SaveResultsHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/SaveResultsHandler.java b/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/SaveResultsHandler.java
deleted file mode 100644
index 301feef..0000000
--- a/taverna-commandline-common/src/main/java/net/sf/taverna/t2/commandline/data/SaveResultsHandler.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*******************************************************************************
- * Copyright (C) 2007 The University of Manchester
- *
- * Modifications to the initial code base are copyright of their
- * respective authors, or their employers as appropriate.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This program 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 GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- ******************************************************************************/
-package net.sf.taverna.t2.commandline.data;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.List;
-
-import org.apache.taverna.databundle.DataBundles;
-
-/**
- * Handles all recording of results as they are received by the {@link CommandLineResultListener} or
- * when the workflow enactment has completed.
- * This includes saving as a Baclava Document, or storing individual results.
- *
- * @author Stuart Owen
- * @see BaclavaHandler
- * @see CommandLineResultListener
- */
-public class SaveResultsHandler {
-
- private final File outputDirectory;
-
- public SaveResultsHandler(File rootOutputDir) {
- this.outputDirectory = rootOutputDir;
- }
-
- /**
- * Given the Data on an output port, saves the data on a disk in the
- * output directory.
- *
- * @param portName
- * @param data
- * @throws IOException
- */
- public void saveResultsForPort(String portName, Path data) throws IOException {
- if (DataBundles.isList(data)) {
- Path outputPath = outputDirectory.toPath().resolve(portName);
- Files.createDirectories(outputPath);
- saveList(DataBundles.getList(data), outputPath);
- } else if (DataBundles.isError(data)) {
- Files.copy(data, outputDirectory.toPath().resolve(portName + ".error"));
- } else {
- Files.copy(data, outputDirectory.toPath().resolve(portName));
- }
- }
-
- private void saveList(List<Path> list, Path destination) throws IOException {
- int index = 1;
- for (Path data : list) {
- if (data != null) {
- if (DataBundles.isList(data)) {
- Path outputPath = destination.resolve(String.valueOf(index));
- Files.createDirectories(outputPath);
- saveList(DataBundles.getList(data), outputPath);
- } else if (DataBundles.isError(data)) {
- Files.copy(data, destination.resolve(String.valueOf(index) + ".error"));
- } else {
- Files.copy(data, destination.resolve(String.valueOf(index)));
- }
- }
- index++;
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineMasterPasswordProvider.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineMasterPasswordProvider.java b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineMasterPasswordProvider.java
new file mode 100644
index 0000000..ae19b8f
--- /dev/null
+++ b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineMasterPasswordProvider.java
@@ -0,0 +1,183 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline;
+
+import java.io.BufferedReader;
+import java.io.Console;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import org.apache.taverna.commandline.exceptions.CommandLineMasterPasswordException;
+import org.apache.taverna.commandline.options.CommandLineOptions;
+import org.apache.taverna.security.credentialmanager.MasterPasswordProvider;
+
+import org.apache.log4j.Logger;
+
+/**
+ * An implementation of {@link MasterPasswordProvider} that reads Credential Manager's
+ * master password from stdin (pipe or terminal) is -cmpassword option is present in command
+ * line arguments. Otherwise it tries to read it from a special file password.txt in a special
+ * directory specified by -cmdir option, if present.
+ *
+ * @author Alex Nenadic
+ */
+public class CommandLineMasterPasswordProvider implements MasterPasswordProvider {
+
+ private static final String CREDENTIAL_MANAGER_MASTER_PASSWORD_OPTION = "cmpassword";
+ private static final String CREDENTIAL_MANAGER_DIRECTORY_OPTION = "cmdir";
+
+ private static Logger logger = Logger.getLogger(CommandLineMasterPasswordProvider.class);
+
+ private String masterPassword = null;
+ private int priority = 200;
+
+ private boolean finishedReadingPassword = false;
+ private final CommandLineOptions commandLineOptions;
+
+ public CommandLineMasterPasswordProvider(CommandLineOptions commandLineOptions) {
+ this.commandLineOptions = commandLineOptions;
+ }
+
+ @Override
+ public String getMasterPassword(boolean firstTime) {
+ if (!finishedReadingPassword) {
+ // -cmpassword option was present in the command line arguments
+ if (commandLineOptions.hasOption(CREDENTIAL_MANAGER_MASTER_PASSWORD_OPTION)) {
+ // Try to read the password from stdin (terminal or pipe)
+ try {
+ masterPassword = getCredentialManagerPasswordFromStdin();
+ } catch (CommandLineMasterPasswordException e) {
+ masterPassword = null;
+ }
+ }
+ // -cmpassword option was not present in the command line arguments
+ // and -cmdir option was there - try to get the master password from
+ // the "special" password file password.txt inside the Cred. Manager directory.
+ else {
+ if (commandLineOptions.hasOption(CREDENTIAL_MANAGER_DIRECTORY_OPTION)) {
+ // Try to read the password from a special file located in
+ // Credential Manager directory (if the dir was not null)
+ try {
+ masterPassword = getCredentialManagerPasswordFromFile();
+ } catch (CommandLineMasterPasswordException ex) {
+ masterPassword = null;
+ }
+ }
+ }
+ finishedReadingPassword = true; // we do not want to attempt to read from stdin several
+ // times
+ }
+ return masterPassword;
+ }
+
+ public void setMasterPassword(String masterPassword) {
+ this.masterPassword = masterPassword;
+ finishedReadingPassword = true;
+ }
+
+ @Override
+ public int getProviderPriority() {
+ return priority;
+ }
+
+ private String getCredentialManagerPasswordFromStdin()
+ throws CommandLineMasterPasswordException {
+
+ String password = null;
+
+ Console console = System.console();
+
+ if (console == null) { // password is being piped in, not entered in the terminal by user
+ BufferedReader buffReader = null;
+ try {
+ buffReader = new BufferedReader(new InputStreamReader(System.in));
+ password = buffReader.readLine();
+ } catch (IOException ex) {
+ // For some reason the error of the exception thrown
+ // does not get printed from the Launcher so print it here as
+ // well as it gives more clue as to what is going wrong.
+ logger.error(
+ "An error occured while trying to read Credential Manager's password that was piped in: "
+ + ex.getMessage(), ex);
+ throw new CommandLineMasterPasswordException(
+ "An error occured while trying to read Credential Manager's password that was piped in: "
+ + ex.getMessage(), ex);
+ } finally {
+ try {
+ buffReader.close();
+ } catch (Exception ioe1) {
+ // Ignore
+ }
+ }
+ } else { // read the password from the terminal as entered by the user
+ try {
+ // Block until user enters password
+ char passwordArray[] = console.readPassword("Password for Credential Manager: ");
+ if (passwordArray != null) { // user did not abort input
+ password = new String(passwordArray);
+ } // else password will be null
+
+ } catch (Exception ex) {
+ // For some reason the error of the exception thrown
+ // does not get printed from the Launcher so print it here as
+ // well as it gives more clue as to what is going wrong.
+ logger.error(
+ "An error occured while trying to read Credential Manager's password from the terminal: "
+ + ex.getMessage(), ex);
+ throw new CommandLineMasterPasswordException(
+ "An error occured while trying to read Credential Manager's password from the terminal: "
+ + ex.getMessage(), ex);
+ }
+ }
+ return password;
+ }
+
+ private String getCredentialManagerPasswordFromFile() throws CommandLineMasterPasswordException {
+ String password = null;
+ if (commandLineOptions.hasOption(CREDENTIAL_MANAGER_DIRECTORY_OPTION)) {
+ String cmDir = commandLineOptions.getCredentialManagerDir();
+
+ File passwordFile = new File(cmDir, "password.txt");
+ BufferedReader buffReader = null;
+ try {
+ buffReader = new BufferedReader(new FileReader(passwordFile));
+ password = buffReader.readLine();
+ } catch (IOException ioe) {
+ // For some reason the error of the exception thrown
+ // does not get printed from the Launcher so print it here as
+ // well as it gives more clue as to what is going wrong.
+ logger.error("There was an error reading the Credential Manager password from "
+ + passwordFile.toString() + ": " + ioe.getMessage(), ioe);
+ throw new CommandLineMasterPasswordException(
+ "There was an error reading the Credential Manager password from "
+ + passwordFile.toString() + ": " + ioe.getMessage(), ioe);
+ } finally {
+ try {
+ buffReader.close();
+ } catch (Exception ioe1) {
+ // Ignore
+ }
+ }
+ }
+ return password;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineTool.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineTool.java b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineTool.java
new file mode 100644
index 0000000..92f6990
--- /dev/null
+++ b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/CommandLineTool.java
@@ -0,0 +1,436 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.naming.NamingException;
+
+import org.apache.taverna.commandline.data.DatabaseConfigurationHandler;
+import org.apache.taverna.commandline.data.InputsHandler;
+import org.apache.taverna.commandline.data.SaveResultsHandler;
+import org.apache.taverna.commandline.exceptions.DatabaseConfigurationException;
+import org.apache.taverna.commandline.exceptions.InputMismatchException;
+import org.apache.taverna.commandline.exceptions.InvalidOptionException;
+import org.apache.taverna.commandline.exceptions.OpenDataflowException;
+import org.apache.taverna.commandline.exceptions.ReadInputException;
+import org.apache.taverna.commandline.options.CommandLineOptions;
+import org.apache.taverna.security.credentialmanager.CMException;
+import org.apache.taverna.security.credentialmanager.CredentialManager;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.log4j.RollingFileAppender;
+import org.apache.taverna.databundle.DataBundles;
+import org.apache.taverna.robundle.Bundle;
+import org.apache.taverna.scufl2.api.common.NamedSet;
+import org.apache.taverna.scufl2.api.container.WorkflowBundle;
+import org.apache.taverna.scufl2.api.core.Workflow;
+import org.apache.taverna.scufl2.api.io.ReaderException;
+import org.apache.taverna.scufl2.api.io.WorkflowBundleIO;
+import org.apache.taverna.scufl2.api.port.InputWorkflowPort;
+import org.apache.taverna.scufl2.api.port.OutputWorkflowPort;
+import org.apache.taverna.scufl2.validation.ValidationException;
+import org.apache.taverna.scufl2.validation.correctness.CorrectnessValidator;
+import org.apache.taverna.scufl2.validation.correctness.ReportCorrectnessValidationListener;
+import org.apache.taverna.scufl2.validation.structural.ReportStructuralValidationListener;
+import org.apache.taverna.scufl2.validation.structural.StructuralValidator;
+
+import org.apache.taverna.configuration.database.DatabaseConfiguration;
+import org.apache.taverna.configuration.database.DatabaseManager;
+import org.apache.taverna.platform.execution.api.ExecutionEnvironment;
+import org.apache.taverna.platform.execution.api.InvalidExecutionIdException;
+import org.apache.taverna.platform.execution.api.InvalidWorkflowException;
+import org.apache.taverna.platform.report.State;
+import org.apache.taverna.platform.report.WorkflowReport;
+import org.apache.taverna.platform.run.api.InvalidRunIdException;
+import org.apache.taverna.platform.run.api.RunProfile;
+import org.apache.taverna.platform.run.api.RunProfileException;
+import org.apache.taverna.platform.run.api.RunService;
+import org.apache.taverna.platform.run.api.RunStateException;
+
+/**
+ * A utility class that wraps the process of executing a workflow, allowing workflows to be easily
+ * executed independently of the GUI.
+ *
+ * @author Stuart Owen
+ * @author Alex Nenadic
+ */
+public class CommandLineTool {
+ private static boolean BOOTSTRAP_LOGGING = false;
+ private static Logger logger = Logger.getLogger(CommandLineTool.class);
+
+ private RunService runService;
+ private CredentialManager credentialManager;
+ private CommandLineOptions commandLineOptions;
+ private WorkflowBundle workflowBundle;
+ private WorkflowBundleIO workflowBundleIO;
+ private DatabaseConfiguration databaseConfiguration;
+ private DatabaseManager databaseManager;
+
+ public void run() {
+ try {
+ if (BOOTSTRAP_LOGGING)
+ initialiseLogging();
+ int result = setupAndExecute();
+ System.exit(result);
+ } catch (InvalidOptionException | IOException | ReadInputException
+ | InvalidRunIdException | RunStateException
+ | InvalidExecutionIdException | OpenDataflowException
+ | RunProfileException e) {
+ error(e.getMessage());
+ } catch (CMException e) {
+ error("There was an error initializing Taverna's SSLSocketFactory from Credential Manager. "
+ + e.getMessage());
+ } catch (ReaderException e) {
+ error("There was an error reading the workflow: " + e.getMessage());
+ } catch (ValidationException e) {
+ error("There was an error validating the workflow: " + e.getMessage());
+ } catch (InvalidWorkflowException e) {
+ error("There was an error validating the workflow: " + e.getMessage());
+ } catch (DatabaseConfigurationException e) {
+ error("There was an error configuring the database: " + e.getMessage());
+ }
+ System.exit(1);
+ }
+
+ private void initialiseLogging() {
+ LogManager.resetConfiguration();
+
+ if (System.getProperty("log4j.configuration") == null) {
+ try {
+ PropertyConfigurator.configure(CommandLineTool.class.getClassLoader()
+ .getResource("cl-log4j.properties").toURI().toURL());
+ } catch (MalformedURLException e) {
+ logger.error("There was a serious error reading the default logging configuration",
+ e);
+ } catch (URISyntaxException e) {
+ logger.error("There was a serious error reading the default logging configuration",
+ e);
+ }
+
+ } else {
+ PropertyConfigurator.configure(System.getProperty("log4j.configuration"));
+ }
+
+ if (commandLineOptions.hasLogFile()) {
+ RollingFileAppender appender;
+ try {
+
+ PatternLayout layout = new PatternLayout("%-5p %d{ISO8601} (%c:%L) - %m%n");
+ appender = new RollingFileAppender(layout, commandLineOptions.getLogFile());
+ appender.setMaxFileSize("1MB");
+ appender.setEncoding("UTF-8");
+ appender.setMaxBackupIndex(4);
+ // Let root logger decide level
+ appender.setThreshold(Level.ALL);
+ LogManager.getRootLogger().addAppender(appender);
+ } catch (IOException e) {
+ System.err.println("Could not log to " + commandLineOptions.getLogFile());
+ }
+ }
+ }
+
+ public int setupAndExecute() throws InputMismatchException, InvalidOptionException,
+ CMException, OpenDataflowException, ReaderException, IOException, ValidationException,
+ ReadInputException, InvalidWorkflowException, RunProfileException,
+ InvalidRunIdException, RunStateException, InvalidExecutionIdException, DatabaseConfigurationException {
+
+ if (!commandLineOptions.askedForHelp()) {
+ setupDatabase(commandLineOptions);
+
+ if (commandLineOptions.getWorkflow() != null) {
+
+ /* Set context class loader to us,
+ * so that things such as JSON-LD caching of
+ * robundle works.
+ */
+
+ Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+
+
+ /*
+ * Initialise Credential Manager and SSL stuff quite early as
+ * parsing and validating the workflow may require it
+ */
+ String credentialManagerDirPath = commandLineOptions.getCredentialManagerDir();
+
+ /*
+ * If credentialManagerDirPath is null, the Credential Manager
+ * will be initialized from the default location in
+ * <TAVERNA_HOME>/security somewhere inside user's home
+ * directory. This should not be used when running command line
+ * tool on a server and the Credential Manager dir path should
+ * always be passed in as we do not want to store the security
+ * files in user's home directory on the server (we do not even
+ * know which user the command line tool will be running as).
+ */
+ if (credentialManagerDirPath != null) {
+ credentialManager.setConfigurationDirectoryPath(new File(
+ credentialManagerDirPath));
+ }
+
+ // Initialise the SSL stuff - set the SSLSocketFactory
+ // to use Taverna's Keystore and Truststore.
+ credentialManager.initializeSSL();
+
+ URL workflowURL = readWorkflowURL(commandLineOptions.getWorkflow());
+
+ workflowBundle = workflowBundleIO.readBundle(workflowURL, null);
+
+ logger.debug("Read the wf bundle");
+
+ validateWorkflowBundle(workflowBundle);
+ logger.debug("Validated the wf bundle");
+
+
+ Set<ExecutionEnvironment> executionEnvironments = runService
+ .getExecutionEnvironments();
+
+ ExecutionEnvironment executionEnvironment = null;
+
+ /*
+ * Find the right execution environment, e.g. local execution
+ * with the correct reference service based on command line
+ * options
+ */
+ while (executionEnvironments.iterator().hasNext()) {
+ // TODO Choose the right one
+ // take the fist one for now
+ executionEnvironment = executionEnvironments.iterator().next();
+ break;
+ }
+
+ logger.debug("Got the execution environment");
+
+ InputsHandler inputsHandler = new InputsHandler();
+ Map<String, InputWorkflowPort> portMap = new HashMap<String, InputWorkflowPort>();
+
+ Workflow workflow = workflowBundle.getMainWorkflow();
+
+ for (InputWorkflowPort port : workflow.getInputPorts()) {
+ portMap.put(port.getName(), port);
+ }
+ inputsHandler.checkProvidedInputs(portMap, commandLineOptions);
+ logger.debug("Checked inputs");
+
+ Bundle inputs = inputsHandler.registerInputs(portMap, commandLineOptions, null);
+ logger.debug("Registered inputs");
+
+ RunProfile runProfile = new RunProfile(executionEnvironment, workflowBundle, inputs);
+
+ String runId = runService.createRun(runProfile);
+
+ runService.start(runId);
+ logger.debug("Started wf run");
+
+ WorkflowReport report = runService.getWorkflowReport(runId);
+
+ while (!workflowFinished(report)) {
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ System.err.println("Interrupted while waiting for workflow to finish");
+ return 1;
+ }
+ }
+
+ NamedSet<OutputWorkflowPort> workflowOutputPorts = workflow.getOutputPorts();
+ if (!workflowOutputPorts.isEmpty()) {
+ File outputDir = null;
+
+ if (commandLineOptions.saveResultsToDirectory()) {
+ outputDir = determineOutputDir(commandLineOptions, workflowBundle.getName());
+ outputDir.mkdirs();
+ }
+
+ Path outputs = DataBundles.getOutputs(runService.getDataBundle(runId));
+
+ if (outputDir != null) {
+ SaveResultsHandler saveResultsHandler = new SaveResultsHandler(outputDir);
+
+ for (OutputWorkflowPort outputWorkflowPort : workflowOutputPorts) {
+ String workflowOutputPortName = outputWorkflowPort.getName();
+ Path output = DataBundles.getPort(outputs, workflowOutputPortName);
+ if (!DataBundles.isMissing(output)) {
+ saveResultsHandler.saveResultsForPort(workflowOutputPortName, output);
+ }
+ }
+ }
+ }
+ if (commandLineOptions.saveResultsToBundle() != null) {
+ Path bundlePath = Paths.get(commandLineOptions.saveResultsToBundle());
+ DataBundles.closeAndSaveBundle(runService.getDataBundle(runId), bundlePath);
+ System.out.println("Workflow Run Bundle saved to: " + bundlePath.toAbsolutePath());
+ } else {
+ System.out.println("Workflow Run Bundle: " + runService.getDataBundle(runId).getSource());
+ // For debugging we'll leave it in /tmp for now
+ runService.getDataBundle(runId).setDeleteOnClose(false);
+ DataBundles.closeBundle(runService.getDataBundle(runId));
+ }
+
+ if (report.getState().equals(State.FAILED)) {
+ System.out.println("Workflow failed - see report below.");
+ System.out.println(report);
+ } else if (report.getState().equals(State.COMPLETED)) {
+ System.out.println("Workflow completed.");
+ }
+
+ }
+ } else {
+ commandLineOptions.displayHelp();
+ }
+
+ // wait until user hits CTRL-C before exiting
+ if (commandLineOptions.getStartDatabaseOnly()) {
+ // FIXME: need to do this more gracefully.
+ while (true) {
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ return 0;
+ }
+ }
+ }
+
+ return 0;
+ }
+
+ private boolean workflowFinished(WorkflowReport report) {
+ State state = report.getState();
+ if (state == State.CANCELLED || state == State.COMPLETED || state == State.FAILED) {
+ return true;
+ }
+ return false;
+ }
+
+ protected void validateWorkflowBundle(WorkflowBundle workflowBundle) throws ValidationException {
+ CorrectnessValidator cv = new CorrectnessValidator();
+ ReportCorrectnessValidationListener rcvl = new ReportCorrectnessValidationListener();
+ cv.checkCorrectness(workflowBundle, true, rcvl);
+ if (rcvl.detectedProblems()) {
+ throw rcvl.getException();
+ }
+
+ StructuralValidator sv = new StructuralValidator();
+ ReportStructuralValidationListener rsvl = new ReportStructuralValidationListener();
+ sv.checkStructure(workflowBundle, rsvl);
+ if (rsvl.detectedProblems()) {
+ throw rcvl.getException();
+ }
+ }
+
+ private void setupDatabase(CommandLineOptions options)
+ throws DatabaseConfigurationException {
+ DatabaseConfigurationHandler dbHandler = new DatabaseConfigurationHandler(
+ options, databaseConfiguration, databaseManager);
+ dbHandler.configureDatabase();
+ try {
+ if (!options.isInMemory())
+ dbHandler.testDatabaseConnection();
+ } catch (NamingException e) {
+ throw new DatabaseConfigurationException(
+ "There was an error trying to setup the database datasource: "
+ + e.getMessage(), e);
+ } catch (SQLException e) {
+ if (options.isClientServer())
+ throw new DatabaseConfigurationException(
+ "There was an error whilst making a test database connection. If running with -clientserver you should check that a server is running (check -startdb or -dbproperties)",
+ e);
+ if (options.isEmbedded())
+ throw new DatabaseConfigurationException(
+ "There was an error whilst making a test database connection. If running with -embedded you should make sure that another process isn't using the database, or a server running through -startdb",
+ e);
+ }
+ }
+
+ private File determineOutputDir(CommandLineOptions options, String dataflowName) {
+ File result = new File(dataflowName + "_output");
+ int x = 1;
+ while (result.exists()) {
+ result = new File(dataflowName + "_output_" + x);
+ x++;
+ }
+ System.out.println("Outputs will be saved to the directory: "
+ + result.getAbsolutePath());
+ return result;
+ }
+
+ protected void error(String msg) {
+ System.err.println(msg);
+ }
+
+ private URL readWorkflowURL(String workflowOption) throws OpenDataflowException {
+ try {
+ return new URL(new URL("file:"), workflowOption);
+ } catch (MalformedURLException e) {
+ throw new OpenDataflowException("The was an error processing the URL to the workflow: "
+ + e.getMessage(), e);
+ }
+ }
+
+ public void setCommandLineOptions(CommandLineOptions commandLineOptions){
+ this.commandLineOptions = commandLineOptions;
+ }
+
+ public void setRunService(RunService runService) {
+ this.runService = runService;
+ }
+
+ public void setCredentialManager(CredentialManager credentialManager) {
+ this.credentialManager = credentialManager;
+ }
+
+ public void setWorkflowBundleIO(WorkflowBundleIO workflowBundleIO) {
+ this.workflowBundleIO = workflowBundleIO;
+ }
+
+ /**
+ * Sets the databaseConfiguration.
+ *
+ * @param databaseConfiguration the new value of databaseConfiguration
+ */
+ public void setDatabaseConfiguration(DatabaseConfiguration databaseConfiguration) {
+ this.databaseConfiguration = databaseConfiguration;
+ }
+
+ /**
+ * Sets the databaseManager.
+ *
+ * @param databaseManager the new value of databaseManager
+ */
+ public void setDatabaseManager(DatabaseManager databaseManager) {
+ this.databaseManager = databaseManager;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/DatabaseConfigurationHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/DatabaseConfigurationHandler.java b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/DatabaseConfigurationHandler.java
new file mode 100644
index 0000000..49b628e
--- /dev/null
+++ b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/DatabaseConfigurationHandler.java
@@ -0,0 +1,153 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline.data;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Properties;
+
+import javax.naming.NamingException;
+
+import org.apache.taverna.commandline.exceptions.DatabaseConfigurationException;
+import org.apache.taverna.commandline.options.CommandLineOptions;
+
+import org.apache.log4j.Logger;
+
+import org.apache.taverna.configuration.database.DatabaseConfiguration;
+import org.apache.taverna.configuration.database.DatabaseManager;
+
+/**
+ * Handles the initialisation and configuration of the data source according to
+ * the command line arguments, or a properties file.
+ * This also handles starting a network based instance of a Derby server, if requested.
+ *
+ * @author Stuart Owen
+ *
+ */
+public class DatabaseConfigurationHandler {
+
+ private static Logger logger = Logger.getLogger(DatabaseConfigurationHandler.class);
+ private final CommandLineOptions options;
+ private final DatabaseConfiguration dbConfig;
+ private final DatabaseManager databaseManager;
+
+ public DatabaseConfigurationHandler(CommandLineOptions options, DatabaseConfiguration databaseConfiguration, DatabaseManager databaseManager) {
+ this.options = options;
+ this.dbConfig = databaseConfiguration;
+ this.databaseManager = databaseManager;
+ databaseConfiguration.disableAutoSave();
+ }
+
+ public void configureDatabase() throws DatabaseConfigurationException {
+ overrideDefaults();
+ useOptions();
+ if (dbConfig.getStartInternalDerbyServer()) {
+ databaseManager.startDerbyNetworkServer();
+ System.out.println("Started Derby Server on Port: "
+ + dbConfig.getCurrentPort());
+ }
+ }
+
+ public DatabaseConfiguration getDBConfig() {
+ return dbConfig;
+ }
+
+ private void importConfigurationFromStream(InputStream inStr)
+ throws IOException {
+ Properties p = new Properties();
+ p.load(inStr);
+ for (Object key : p.keySet()) {
+ dbConfig.setProperty((String)key, p.getProperty((String)key).trim());
+ }
+ }
+
+ protected void overrideDefaults() throws DatabaseConfigurationException {
+
+ InputStream inStr = DatabaseConfigurationHandler.class.getClassLoader().getResourceAsStream("database-defaults.properties");
+ try {
+ importConfigurationFromStream(inStr);
+ } catch (IOException e) {
+ throw new DatabaseConfigurationException("There was an error reading the default database configuration settings: "+e.getMessage(),e);
+ }
+ }
+
+ protected void readConfigirationFromFile(String filename) throws IOException {
+ FileInputStream fileInputStream = new FileInputStream(filename);
+ importConfigurationFromStream(fileInputStream);
+ fileInputStream.close();
+ }
+
+ public void testDatabaseConnection()
+ throws DatabaseConfigurationException, NamingException, SQLException {
+ //try and get a connection
+ Connection con = null;
+ try {
+ con = databaseManager.getConnection();
+ } finally {
+ if (con!=null)
+ try {
+ con.close();
+ } catch (SQLException e) {
+ logger.warn("There was an SQL error whilst closing the test connection: "+e.getMessage(),e);
+ }
+ }
+ }
+
+ public void useOptions() throws DatabaseConfigurationException {
+
+ if (options.hasOption("port")) {
+ dbConfig.setPort(options.getDatabasePort());
+ }
+
+ if (options.hasOption("startdb")) {
+ dbConfig.setStartInternalDerbyServer(true);
+ }
+
+ if (options.hasOption("inmemory")) {
+ dbConfig.setInMemory(true);
+ }
+
+ if (options.hasOption("embedded")) {
+ dbConfig.setInMemory(false);
+ dbConfig.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
+ }
+
+ if (options.isProvenanceEnabled()) {
+ dbConfig.setProvenanceEnabled(true);
+ }
+
+ if (options.hasOption("clientserver")) {
+ dbConfig.setInMemory(false);
+ dbConfig.setDriverClassName("org.apache.derby.jdbc.ClientDriver");
+ dbConfig.setJDBCUri("jdbc:derby://localhost:" + dbConfig.getPort() + "/t2-database;create=true;upgrade=true");
+ }
+
+ if (options.hasOption("dbproperties")) {
+ try {
+ readConfigirationFromFile(options.getDatabaseProperties());
+ } catch (IOException e) {
+ throw new DatabaseConfigurationException("There was an error reading the database configuration options at "+options.getDatabaseProperties()+" : "+e.getMessage(),e);
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/ErrorValueHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/ErrorValueHandler.java b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/ErrorValueHandler.java
new file mode 100644
index 0000000..6a4b579
--- /dev/null
+++ b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/ErrorValueHandler.java
@@ -0,0 +1,71 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline.data;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.List;
+
+import javax.swing.tree.DefaultMutableTreeNode;
+
+import org.apache.taverna.databundle.DataBundles;
+import org.apache.taverna.databundle.ErrorDocument;
+
+/**
+ * Handles ErrorValues and transforming them into String representations
+ * that can be stored as a file, or within a Baclava document.
+ *
+ * @author Stuart Owen
+ * @author David Withers
+ */
+public class ErrorValueHandler {
+
+ /**
+ * Creates a string representation of the ErrorValue.
+ * @throws IOException
+ */
+ public static String buildErrorValueString(ErrorDocument errorValue) throws IOException {
+
+ String errDocumentString = errorValue.getMessage() + "\n";
+
+ String exceptionMessage = errorValue.getMessage();
+ if (exceptionMessage != null && !exceptionMessage.equals("")) {
+ DefaultMutableTreeNode exceptionMessageNode = new DefaultMutableTreeNode(
+ exceptionMessage);
+ errDocumentString += exceptionMessageNode + "\n";
+ errDocumentString += errorValue.getTrace();
+ }
+
+ List<Path> errorReferences = errorValue.getCausedBy();
+ if (!errorReferences.isEmpty()) {
+ errDocumentString += "Set of cause errors to follow." + "\n";
+ }
+ int errorCounter = 1;
+ for (Path cause : errorReferences) {
+ if (DataBundles.isError(cause)) {
+ errDocumentString += "ErrorValue " + (errorCounter++) + "\n";
+ errDocumentString += buildErrorValueString(DataBundles.getError(cause)) + "\n";
+ }
+ }
+
+ return errDocumentString;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/InputsHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/InputsHandler.java b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/InputsHandler.java
new file mode 100644
index 0000000..a0da481
--- /dev/null
+++ b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/InputsHandler.java
@@ -0,0 +1,276 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline.data;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.taverna.commandline.exceptions.InputMismatchException;
+import org.apache.taverna.commandline.exceptions.InvalidOptionException;
+import org.apache.taverna.commandline.exceptions.ReadInputException;
+import org.apache.taverna.commandline.options.CommandLineOptions;
+import org.apache.taverna.invocation.InvocationContext;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.log4j.Logger;
+import org.apache.taverna.databundle.DataBundles;
+import org.apache.taverna.robundle.Bundle;
+import org.apache.taverna.scufl2.api.port.InputWorkflowPort;
+
+/**
+ * Handles the reading, or processing, or input values according to arguments provided to the
+ * commandline.
+ * The may be either as direct values, from a file, or from a Baclava document.
+ * Also handles registering the input values with the Data Service, ready to initiate
+ * the workflow run.
+ *
+ * @author Stuart Owen
+ */
+public class InputsHandler {
+
+ private static Logger logger = Logger.getLogger(InputsHandler.class);
+
+ public void checkProvidedInputs(Map<String, InputWorkflowPort> portMap,
+ CommandLineOptions options) throws InputMismatchException {
+ // we dont check for the document
+ Set<String> providedInputNames = new HashSet<String>();
+ for (int i = 0; i < options.getInputFiles().length; i += 2) {
+ // If it already contains a value for the input port, e.g
+ // two inputs are provided for the same port
+ if (providedInputNames.contains(options.getInputFiles()[i])) {
+ throw new InputMismatchException(
+ "Two input values were provided for the same input port "
+ + options.getInputFiles()[i] + ".", null, null);
+ }
+ providedInputNames.add(options.getInputFiles()[i]);
+ }
+
+ for (int i = 0; i < options.getInputValues().length; i += 2) {
+ // If it already contains a value for the input port, e.g
+ // two inputs are provided for the same port
+ if (providedInputNames.contains(options.getInputValues()[i])) {
+ throw new InputMismatchException(
+ "Two input values were provided for the same input port "
+ + options.getInputValues()[i] + ".", null, null);
+ }
+ providedInputNames.add(options.getInputValues()[i]);
+ }
+
+ if (portMap.size() * 2 != (options.getInputFiles().length + options.getInputValues().length)) {
+ throw new InputMismatchException(
+ "The number of inputs provided does not match the number of input ports.",
+ portMap.keySet(), providedInputNames);
+ }
+
+ for (String portName : portMap.keySet()) {
+ if (!providedInputNames.contains(portName)) {
+ throw new InputMismatchException(
+ "The provided inputs does not contain an input for the port '"
+ + portName + "'", portMap.keySet(), providedInputNames);
+ }
+ }
+ }
+
+ public Bundle registerInputs(Map<String, InputWorkflowPort> portMap,
+ CommandLineOptions options, InvocationContext context) throws InvalidOptionException,
+ ReadInputException, IOException {
+ Bundle inputDataBundle;
+ inputDataBundle = DataBundles.createBundle();
+ inputDataBundle.setDeleteOnClose(false);
+ System.out.println("Bundle: " + inputDataBundle.getSource());
+
+ Path inputs = DataBundles.getInputs(inputDataBundle);
+
+ URL url;
+ try {
+ url = new URL("file:");
+ } catch (MalformedURLException e1) {
+ // Should never happen, but just in case:
+ throw new ReadInputException(
+ "The was an internal error setting up the URL to open the inputs. You should contact Taverna support.",
+ e1);
+ }
+
+ if (options.hasInputFiles()) {
+ regesterInputsFromFiles(portMap, options, inputs, url);
+ }
+
+ if (options.hasInputValues()) {
+ registerInputsFromValues(portMap, options, inputs);
+
+ }
+
+ return inputDataBundle;
+ }
+
+ private void registerInputsFromValues(Map<String, InputWorkflowPort> portMap,
+ CommandLineOptions options, Path inputs) throws InvalidOptionException {
+ String[] inputParams = options.getInputValues();
+ for (int i = 0; i < inputParams.length; i = i + 2) {
+ String inputName = inputParams[i];
+ try {
+ String inputValue = inputParams[i + 1];
+ InputWorkflowPort port = portMap.get(inputName);
+
+ if (port == null) {
+ throw new InvalidOptionException("Cannot find an input port named '"
+ + inputName + "'");
+ }
+
+ Path portPath = DataBundles.getPort(inputs, inputName);
+ if (options.hasDelimiterFor(inputName)) {
+ String delimiter = options.inputDelimiter(inputName);
+ Object value = checkForDepthMismatch(1, port.getDepth(), inputName,
+ inputValue.split(delimiter));
+ setValue(portPath, value);
+ } else {
+ Object value = checkForDepthMismatch(0, port.getDepth(), inputName, inputValue);
+ setValue(portPath, value);
+ }
+
+ } catch (IndexOutOfBoundsException e) {
+ throw new InvalidOptionException("Missing input value for input " + inputName);
+ } catch (IOException e) {
+ throw new InvalidOptionException("Error creating value for input " + inputName);
+ }
+ }
+ }
+
+ private void regesterInputsFromFiles(Map<String, InputWorkflowPort> portMap,
+ CommandLineOptions options, Path inputs, URL url) throws InvalidOptionException {
+ String[] inputParams = options.getInputFiles();
+ for (int i = 0; i < inputParams.length; i = i + 2) {
+ String inputName = inputParams[i];
+ try {
+ URL inputURL = new URL(url, inputParams[i + 1]);
+ InputWorkflowPort port = portMap.get(inputName);
+
+ if (port == null) {
+ throw new InvalidOptionException("Cannot find an input port named '"
+ + inputName + "'");
+ }
+
+ Path portPath = DataBundles.getPort(inputs, inputName);
+ if (options.hasDelimiterFor(inputName)) {
+ String delimiter = options.inputDelimiter(inputName);
+ Object value = IOUtils.toString(inputURL.openStream()).split(delimiter);
+ value = checkForDepthMismatch(1, port.getDepth(), inputName, value);
+ setValue(portPath, value);
+ } else {
+ Object value = IOUtils.toByteArray(inputURL.openStream());
+ value = checkForDepthMismatch(0, port.getDepth(), inputName, value);
+ setValue(portPath, value);
+ }
+ } catch (IndexOutOfBoundsException e) {
+ throw new InvalidOptionException("Missing input filename for input " + inputName);
+ } catch (IOException e) {
+ throw new InvalidOptionException("Could not read input " + inputName + ": "
+ + e.getMessage());
+ }
+ }
+ }
+
+ private void setValue(Path port, Object userInput) throws IOException {
+ if (userInput instanceof File) {
+ DataBundles.setReference(port, ((File) userInput).toURI());
+ } else if (userInput instanceof URL) {
+ try {
+ DataBundles.setReference(port, ((URL) userInput).toURI());
+ } catch (URISyntaxException e) {
+ logger.warn(String.format("Error converting %1$s to URI", userInput), e);
+ }
+ } else if (userInput instanceof String) {
+ DataBundles.setStringValue(port, (String) userInput);
+ } else if (userInput instanceof byte[]) {
+ Files.write(port, (byte[]) userInput, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
+ } else if (userInput instanceof List<?>) {
+ DataBundles.createList(port);
+ List<?> list = (List<?>) userInput;
+ for (Object object : list) {
+ setValue(DataBundles.newListItem(port), object);
+ }
+ } else {
+ logger.warn("Unknown input type : " + userInput.getClass().getName());
+ }
+ }
+
+ private Object checkForDepthMismatch(int inputDepth, int portDepth, String inputName,
+ Object inputValue) throws InvalidOptionException {
+ if (inputDepth != portDepth) {
+ if (inputDepth < portDepth) {
+ logger.warn("Wrapping input for '" + inputName + "' from a depth of " + inputDepth
+ + " to the required depth of " + portDepth);
+ while (inputDepth < portDepth) {
+ List<Object> l = new ArrayList<Object>();
+ l.add(inputValue);
+ inputValue = l;
+ inputDepth++;
+ }
+ } else {
+ String msg = "There is a mismatch between depth of the list for the input port '"
+ + inputName + "' and the data presented. The input port requires a "
+ + depthToString(portDepth) + " and the data presented is a "
+ + depthToString(inputDepth);
+ throw new InvalidOptionException(msg);
+ }
+ }
+
+ return inputValue;
+ }
+
+ private String depthToString(int depth) {
+ switch (depth) {
+ case 0:
+ return "single item";
+ case 1:
+ return "list";
+ case 2:
+ return "list of lists";
+ default:
+ return "list of depth " + depth;
+ }
+ }
+
+ private int getObjectDepth(Object o) {
+ int result = 0;
+ if (o instanceof Iterable) {
+ result++;
+ Iterator i = ((Iterable) o).iterator();
+
+ if (i.hasNext()) {
+ Object child = i.next();
+ result = result + getObjectDepth(child);
+ }
+ }
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/SaveResultsHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/SaveResultsHandler.java b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/SaveResultsHandler.java
new file mode 100644
index 0000000..1555b5a
--- /dev/null
+++ b/taverna-commandline-common/src/main/java/org/apache/taverna/commandline/data/SaveResultsHandler.java
@@ -0,0 +1,85 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline.data;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import org.apache.taverna.databundle.DataBundles;
+
+/**
+ * Handles all recording of results as they are received by the {@link CommandLineResultListener} or
+ * when the workflow enactment has completed.
+ * This includes saving as a Baclava Document, or storing individual results.
+ *
+ * @author Stuart Owen
+ * @see BaclavaHandler
+ * @see CommandLineResultListener
+ */
+public class SaveResultsHandler {
+
+ private final File outputDirectory;
+
+ public SaveResultsHandler(File rootOutputDir) {
+ this.outputDirectory = rootOutputDir;
+ }
+
+ /**
+ * Given the Data on an output port, saves the data on a disk in the
+ * output directory.
+ *
+ * @param portName
+ * @param data
+ * @throws IOException
+ */
+ public void saveResultsForPort(String portName, Path data) throws IOException {
+ if (DataBundles.isList(data)) {
+ Path outputPath = outputDirectory.toPath().resolve(portName);
+ Files.createDirectories(outputPath);
+ saveList(DataBundles.getList(data), outputPath);
+ } else if (DataBundles.isError(data)) {
+ Files.copy(data, outputDirectory.toPath().resolve(portName + ".error"));
+ } else {
+ Files.copy(data, outputDirectory.toPath().resolve(portName));
+ }
+ }
+
+ private void saveList(List<Path> list, Path destination) throws IOException {
+ int index = 1;
+ for (Path data : list) {
+ if (data != null) {
+ if (DataBundles.isList(data)) {
+ Path outputPath = destination.resolve(String.valueOf(index));
+ Files.createDirectories(outputPath);
+ saveList(DataBundles.getList(data), outputPath);
+ } else if (DataBundles.isError(data)) {
+ Files.copy(data, destination.resolve(String.valueOf(index) + ".error"));
+ } else {
+ Files.copy(data, destination.resolve(String.valueOf(index)));
+ }
+ }
+ index++;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context-osgi.xml
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context-osgi.xml b/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context-osgi.xml
index 18b5426..048bd64 100644
--- a/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context-osgi.xml
+++ b/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context-osgi.xml
@@ -7,16 +7,16 @@
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
- <service ref="commandLineMasterPasswordProvider" interface="net.sf.taverna.t2.security.credentialmanager.MasterPasswordProvider" />
+ <service ref="commandLineMasterPasswordProvider" interface="org.apache.taverna.security.credentialmanager.MasterPasswordProvider" />
- <reference id="commandLineOptions" interface="net.sf.taverna.t2.commandline.options.CommandLineOptions"/>
- <reference id="runService" interface="uk.org.taverna.platform.run.api.RunService"/>
+ <reference id="commandLineOptions" interface="org.apache.taverna.commandline.options.CommandLineOptions"/>
+ <reference id="runService" interface="org.apache.taverna.platform.run.api.RunService"/>
<reference id="credentialManager" interface="org.apache.taverna.security.credentialmanager.CredentialManager" />
- <reference id="databaseConfiguration" interface="uk.org.taverna.configuration.database.DatabaseConfiguration" />
- <reference id="databaseManager" interface="uk.org.taverna.configuration.database.DatabaseManager" />
+ <reference id="databaseConfiguration" interface="org.apache.taverna.configuration.database.DatabaseConfiguration" />
+ <reference id="databaseManager" interface="org.apache.taverna.configuration.database.DatabaseManager" />
- <reference id="workflowBundleIO" interface="uk.org.taverna.scufl2.api.io.WorkflowBundleIO" />
- <reference id="t2flowWorkflowBundleReader" interface="uk.org.taverna.scufl2.api.io.WorkflowBundleReader" filter="(mediaType=application/vnd.taverna.t2flow+xml)"/>
- <reference id="rdfXMLWorkflowBundleReader" interface="uk.org.taverna.scufl2.api.io.WorkflowBundleReader" filter="(org.springframework.osgi.bean.name=rdfXMLReader)"/>
+ <reference id="workflowBundleIO" interface="org.apache.taverna.scufl2.api.io.WorkflowBundleIO" />
+ <reference id="t2flowWorkflowBundleReader" interface="org.apache.taverna.scufl2.api.io.WorkflowBundleReader" filter="(mediaType=application/vnd.taverna.t2flow+xml)"/>
+ <reference id="rdfXMLWorkflowBundleReader" interface="org.apache.taverna.scufl2.api.io.WorkflowBundleReader" filter="(org.springframework.osgi.bean.name=rdfXMLReader)"/>
</beans:beans>
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context.xml
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context.xml b/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context.xml
index 52c0f29..082c9d0 100644
--- a/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context.xml
+++ b/taverna-commandline-common/src/main/resources/META-INF/spring/taverna-commandline-common-context.xml
@@ -4,7 +4,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="commandLineTool" class="net.sf.taverna.t2.commandline.CommandLineTool" init-method="run">
+ <bean id="commandLineTool" class="org.apache.taverna.commandline.CommandLineTool" init-method="run">
<property name="commandLineOptions" ref="commandLineOptions" />
<property name="runService" ref="runService" />
<property name="credentialManager" ref="credentialManager" />
@@ -14,7 +14,7 @@
</bean>
<bean id="commandLineMasterPasswordProvider"
- class="net.sf.taverna.t2.commandline.CommandLineMasterPasswordProvider">
+ class="org.apache.taverna.commandline.CommandLineMasterPasswordProvider">
<constructor-arg name="commandLineOptions"
ref="commandLineOptions" />
</bean>
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/test/java/net/sf/taverna/t2/commandline/TestDatabaseConfigurationHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/test/java/net/sf/taverna/t2/commandline/TestDatabaseConfigurationHandler.java b/taverna-commandline-common/src/test/java/net/sf/taverna/t2/commandline/TestDatabaseConfigurationHandler.java
deleted file mode 100644
index c8cbb9d..0000000
--- a/taverna-commandline-common/src/test/java/net/sf/taverna/t2/commandline/TestDatabaseConfigurationHandler.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package net.sf.taverna.t2.commandline;
-
-//import net.sf.taverna.t2.workbench.reference.config.DataManagementConfiguration;
-
-public class TestDatabaseConfigurationHandler {
-
-// @Test
-// public void testDefaults() throws Exception {
-// CommandLineOptions opts = new CommandLineOptions(new String[]{"myworkflow.t2flow"});
-// DatabaseConfigurationHandler handler = new DatabaseConfigurationHandler(opts);
-// handler.configureDatabase();
-// assertEquals("org.apache.derby.jdbc.EmbeddedDriver", DataManagementConfiguration.getInstance().getDriverClassName());
-// assertEquals(false, DataManagementConfiguration.getInstance().getStartInternalDerbyServer());
-// }
-}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-common/src/test/java/org/apache/taverna/commandline/TestDatabaseConfigurationHandler.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-common/src/test/java/org/apache/taverna/commandline/TestDatabaseConfigurationHandler.java b/taverna-commandline-common/src/test/java/org/apache/taverna/commandline/TestDatabaseConfigurationHandler.java
new file mode 100644
index 0000000..a93436e
--- /dev/null
+++ b/taverna-commandline-common/src/test/java/org/apache/taverna/commandline/TestDatabaseConfigurationHandler.java
@@ -0,0 +1,33 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline;
+
+
+public class TestDatabaseConfigurationHandler {
+
+// @Test
+// public void testDefaults() throws Exception {
+// CommandLineOptions opts = new CommandLineOptions(new String[]{"myworkflow.t2flow"});
+// DatabaseConfigurationHandler handler = new DatabaseConfigurationHandler(opts);
+// handler.configureDatabase();
+// assertEquals("org.apache.derby.jdbc.EmbeddedDriver", DataManagementConfiguration.getInstance().getDriverClassName());
+// assertEquals(false, DataManagementConfiguration.getInstance().getStartInternalDerbyServer());
+// }
+}
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-launcher/pom.xml
----------------------------------------------------------------------
diff --git a/taverna-commandline-launcher/pom.xml b/taverna-commandline-launcher/pom.xml
index 42208e3..e88bd66 100644
--- a/taverna-commandline-launcher/pom.xml
+++ b/taverna-commandline-launcher/pom.xml
@@ -4,7 +4,7 @@
<parent>
<groupId>org.apache.taverna.commandline</groupId>
<artifactId>taverna-commandline</artifactId>
- <version>3.1.0.incubating-SNAPSHOT</version>
+ <version>3.1.0-incubating-SNAPSHOT</version>
</parent>
<artifactId>taverna-commandline-launcher</artifactId>
<name>Apache Taverna Commandline Launcher</name>
http://git-wip-us.apache.org/repos/asf/incubator-taverna-commandline/blob/9965dffe/taverna-commandline-launcher/src/main/java/org/apache/taverna/commandline/CommandLineOptionsImpl.java
----------------------------------------------------------------------
diff --git a/taverna-commandline-launcher/src/main/java/org/apache/taverna/commandline/CommandLineOptionsImpl.java b/taverna-commandline-launcher/src/main/java/org/apache/taverna/commandline/CommandLineOptionsImpl.java
new file mode 100644
index 0000000..78c1863
--- /dev/null
+++ b/taverna-commandline-launcher/src/main/java/org/apache/taverna/commandline/CommandLineOptionsImpl.java
@@ -0,0 +1,444 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+package org.apache.taverna.commandline;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.taverna.commandline.exceptions.ArgumentsParsingException;
+import org.apache.taverna.commandline.exceptions.InvalidOptionException;
+import org.apache.taverna.commandline.options.CommandLineOptions;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.io.IOUtils;
+import org.apache.log4j.Logger;
+
+/**
+ * Handles the processing of command line arguments for enacting a workflow.
+ * This class encapsulates all command line options, and exposes them through higher-level
+ * accessors. Upon creation it checks the validity of the command line options and raises an
+ * {@link InvalidOptionException} if they are invalid.
+ *
+ * @author Stuart Owen
+ * @author David Withers
+ */
+public class CommandLineOptionsImpl implements CommandLineOptions {
+
+ private static final String OUTPUTDIR = "outputdir";
+ private static final String BUNDLE = "bundle";
+ private static final Logger logger = Logger.getLogger(CommandLineOptionsImpl.class);
+ private Options options;
+ private CommandLine commandLine;
+
+ public CommandLineOptionsImpl(String[] args) throws ArgumentsParsingException, InvalidOptionException {
+ this.options = intitialiseOptions();
+ this.commandLine = processArgs(args);
+ checkForInvalid();
+ }
+
+ @Override
+ public boolean askedForHelp() {
+ return hasOption("help") || (getArgs().length==0 && getOptions().length==0);
+ }
+
+ @Override
+ public boolean isProvenanceEnabled() {
+ return hasOption("provenance");
+ }
+
+ protected void checkForInvalid() throws InvalidOptionException {
+ if (askedForHelp()) return;
+ if (isProvenanceEnabled()
+ && !(hasOption("embedded") || hasOption("clientserver") || hasOption("dbproperties")))
+ throw new InvalidOptionException(
+ "You should be running with a database to use provenance");
+ if (isProvenanceEnabled() && hasOption("inmemory"))
+ throw new InvalidOptionException(
+ "You should be running with a database to use provenance");
+ if ((hasOption("inputfile") || hasOption("inputvalue"))
+ && hasOption("inputdoc"))
+ throw new InvalidOptionException(
+ "You can't provide both -input and -inputdoc arguments");
+
+ if (hasOption("inputdelimiter") && hasOption("inputdoc"))
+ throw new InvalidOptionException("You cannot combine the -inputdelimiter and -inputdoc arguments");
+
+ if (getArgs().length == 0
+ && !(hasOption("help") || hasOption("startdb")))
+ throw new InvalidOptionException("You must specify a workflow");
+
+ if (hasOption("inmemory") && hasOption("embedded"))
+ throw new InvalidOptionException(
+ "The options -embedded, -clientserver and -inmemory cannot be used together");
+ if (hasOption("inmemory") && hasOption("clientserver"))
+ throw new InvalidOptionException(
+ "The options -embedded, -clientserver and -inmemory cannot be used together");
+ if (hasOption("embedded") && hasOption("clientserver"))
+ throw new InvalidOptionException(
+ "The options -embedded, -clientserver and -inmemory cannot be used together");
+ }
+
+ @Override
+ public void displayHelp() {
+ boolean full = false;
+ if (hasOption("help")) full=true;
+ displayHelp(full);
+ }
+
+ @Override
+ public void displayHelp(boolean showFullText) {
+
+ HelpFormatter formatter = new HelpFormatter();
+ try {
+ formatter
+ .printHelp("executeworkflow [options] [workflow]", options);
+ if (showFullText) {
+ InputStream helpStream = CommandLineOptionsImpl.class
+ .getClassLoader().getResourceAsStream("help.txt");
+ String helpText = IOUtils.toString(helpStream);
+ System.out.println(helpText);
+ }
+
+ } catch (IOException e) {
+ logger.error("Failed to load the help document", e);
+ System.out.println("Failed to load the help document");
+ //System.exit(-1);
+ }
+ }
+
+ @Override
+ public String[] getArgs() {
+ return commandLine.getArgs();
+ }
+
+ /**
+ *
+ * @return the port that the database should run on
+ */
+ @Override
+ public String getDatabasePort() {
+ return getOptionValue("port");
+ }
+
+ /**
+ *
+ * @return a path to a properties file that contains database configuration
+ * settings
+ */
+ @Override
+ public String getDatabaseProperties() {
+ return getOptionValue("dbproperties");
+ }
+
+ /**
+ * Returns an array that alternates between a portname and path to a file
+ * containing the input values. Therefore the array will always contain an
+ * even number of elements
+ *
+ * @return an array of portname and path to files containing individual
+ * inputs.
+ */
+ @Override
+ public String[] getInputFiles() {
+ if (hasInputFiles()) {
+ return getOptionValues("inputfile");
+ } else {
+ return new String[] {};
+ }
+ }
+
+ @Override
+ public String[] getInputValues() {
+ if (hasInputValues()) {
+ return getOptionValues("inputvalue");
+ } else {
+ return new String[] {};
+ }
+ }
+
+ @Override
+ public String getLogFile() {
+ return getOptionValue("logfile");
+ }
+
+ public Option [] getOptions() {
+ return commandLine.getOptions();
+ }
+
+ private String getOptionValue(String opt) {
+ return commandLine.getOptionValue(opt);
+ }
+
+ private String[] getOptionValues(String arg0) {
+ return commandLine.getOptionValues(arg0);
+ }
+
+ /**
+ *
+ * @return the directory to write the results to
+ */
+ @Override
+ public String getOutputDirectory() {
+ return getOptionValue(OUTPUTDIR);
+ }
+
+ @Override
+ public boolean getStartDatabase() {
+ return hasOption("startdb");
+ }
+
+ /**
+ * @return the directory with Credential Manager's files
+ */
+ @Override
+ public String getCredentialManagerDir() {
+ return getOptionValue(CREDENTIAL_MANAGER_DIR_OPTION);
+ }
+
+ @Override
+ public boolean getStartDatabaseOnly() throws InvalidOptionException {
+ return (getStartDatabase() && (getWorkflow() == null));
+ }
+
+ @Override
+ public String getWorkflow() throws InvalidOptionException {
+ if (getArgs().length == 0) {
+ return null;
+ } else if (getArgs().length != 1) {
+ throw new InvalidOptionException(
+ "You should only specify one workflow file");
+ } else {
+ return getArgs()[0];
+ }
+ }
+
+ @Override
+ public boolean hasDelimiterFor(String inputName) {
+ boolean result = false;
+ if (hasOption("inputdelimiter")) {
+ String [] values = getOptionValues("inputdelimiter");
+ for (int i=0;i<values.length;i+=2) {
+ if (values[i].equals(inputName))
+ {
+ result=true;
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public boolean hasInputFiles() {
+ return hasOption("inputfile");
+ }
+
+ @Override
+ public boolean hasInputValues() {
+ return hasOption("inputvalue");
+ }
+
+ @Override
+ public boolean hasLogFile() {
+ return hasOption("logfile");
+ }
+
+ @Override
+ public boolean hasOption(String option) {
+ return commandLine.hasOption(option);
+ }
+
+ @Override
+ public String inputDelimiter(String inputName) {
+ String result = null;
+ if (hasOption("inputdelimiter")) {
+ String [] values = getOptionValues("inputdelimiter");
+ for (int i=0;i<values.length;i+=2) {
+ if (values[i].equals(inputName))
+ {
+ result=values[i+1];
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ @SuppressWarnings("static-access")
+ private Options intitialiseOptions() {
+ Option helpOption = new Option("help", "Display comprehensive help information.");
+
+ Option outputOption = OptionBuilder
+ .withArgName("directory")
+ .hasArg()
+ .withDescription(
+ "Save outputs as files in directory, default "
+ + "is to make a new directory workflowName_output.")
+ .create(OUTPUTDIR);
+
+ Option bundleOption = OptionBuilder.withArgName(BUNDLE).hasArg()
+ .withDescription("Save outputs to a new Workflow Run Bundle (zip).")
+ .create(BUNDLE);
+
+ Option logFileOption = OptionBuilder
+ .withArgName("filename")
+ .hasArg()
+ .withDescription(
+ "The logfile to which more verbose logging will be written to.")
+ .create("logfile");
+
+ Option inputdocOption = OptionBuilder.withArgName("document").hasArg()
+ .withDescription("Load inputs from a Baclava document.").create(
+ "inputdoc");
+
+ Option inputFileOption = OptionBuilder
+ .withArgName("inputname filename").hasArgs(2)
+ .withValueSeparator(' ').withDescription(
+ "Load the named input from file or URL.").create(
+ "inputfile");
+
+ Option inputValueOption = OptionBuilder.withArgName("inputname value")
+ .hasArgs(2).withValueSeparator(' ').withDescription(
+ "Directly use the value for the named input.").create(
+ "inputvalue");
+
+ Option inputDelimiterOption = OptionBuilder
+ .withArgName("inputname delimiter")
+ .hasArgs(2)
+ .withValueSeparator(' ')
+ .withDescription(
+ "Cause an inputvalue or inputfile to be split into a list according to the delimiter. The associated workflow input must be expected to receive a list.")
+ .create("inputdelimiter");
+
+ Option dbProperties = OptionBuilder.withArgName("filename").hasArg()
+ .withDescription(
+ "Load a properties file to configure the database.")
+ .create("dbproperties");
+
+ Option port = OptionBuilder
+ .withArgName("portnumber")
+ .hasArg()
+ .withDescription(
+ "The port that the database is running on. If set requested to start its own internal server, this is the start port that will be used.")
+ .create("port");
+
+ Option embedded = new Option("embedded",
+ "Connect to an embedded Derby database. This can prevent mulitple invocations.");
+ Option clientserver = new Option("clientserver",
+ "Connect as a client to a derby server instance.");
+ Option inMemOption = new Option(
+ "inmemory",
+ "Run the workflow with data stored in-memory rather than in a database (this is the default option). This can give performance inprovements, at the cost of overall memory usage.");
+ Option startDB = new Option("startdb",
+ "Automatically start an internal Derby database server.");
+ Option provenance = new Option("provenance",
+ "Generate provenance information and store it in the database.");
+
+
+ Option credentialManagerDirectory = OptionBuilder.withArgName("directory path").
+ hasArg().withDescription(
+ "Absolute path to a directory where Credential Manager's files (keystore and truststore) are located.")
+ .create(CREDENTIAL_MANAGER_DIR_OPTION);
+ Option credentialManagerPassword = new Option(CREDENTIAL_MANAGER_PASSWORD_OPTION, "Indicate that the master password for Credential Manager will be provided on standard input."); // optional password option, to be read from standard input
+
+ Options options = new Options();
+ options.addOption(helpOption);
+ options.addOption(inputFileOption);
+ options.addOption(inputValueOption);
+ options.addOption(inputDelimiterOption);
+ options.addOption(inputdocOption);
+ options.addOption(outputOption);
+ options.addOption(bundleOption);
+ options.addOption(inMemOption);
+ options.addOption(embedded);
+ options.addOption(clientserver);
+ options.addOption(dbProperties);
+ options.addOption(port);
+ options.addOption(startDB);
+ options.addOption(provenance);
+ options.addOption(logFileOption);
+ options.addOption(credentialManagerDirectory);
+ options.addOption(credentialManagerPassword);
+
+ return options;
+ }
+
+ @Override
+ public boolean isClientServer() {
+ return hasOption("clientserver");
+ }
+
+ @Override
+ public boolean isEmbedded() {
+ return hasOption("embedded");
+ }
+
+ @Override
+ public boolean isInMemory() {
+ return hasOption("inmemory");
+ }
+
+ private CommandLine processArgs(String[] args) throws ArgumentsParsingException {
+ CommandLineParser parser = new GnuParser();
+ CommandLine line = null;
+ try {
+ // parse the command line arguments
+ line = parser.parse(options, args);
+ } catch (ParseException exp) {
+ // oops, something went wrong
+// System.err.println("Taverna command line arguments' parsing failed. Reason: " + exp.getMessage());
+// System.exit(1);
+ throw new ArgumentsParsingException("Taverna command line arguments' parsing failed. Reason: " + exp.getMessage(), exp);
+ }
+ return line;
+ }
+
+ /**
+ * Save the results to a directory if -outputdir has been explicitly defined,
+ * or if -outputdoc has not been defined.
+ *
+ * @return boolean
+ */
+ @Override
+ public boolean saveResultsToDirectory() {
+ return (options.hasOption(OUTPUTDIR) || !hasSaveResultsToBundle());
+ }
+
+ @Override
+ public String saveResultsToBundle() {
+ if (! hasSaveResultsToBundle()) {
+ return null;
+ }
+ return getOptionValue(BUNDLE);
+ }
+
+ @Override
+ public boolean hasSaveResultsToBundle() {
+ return hasOption(BUNDLE);
+ }
+
+}