From 12d94ada78039f7ec4de682fbd0373e85d21aacf Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 5 Sep 2019 13:48:24 -0400 Subject: [PATCH] Added examples showing the command API Signed-off-by: Joe Osborn --- org.eclipse.ice.demo/.classpath | 7 +- .../demo/commands/CommandFactoryExample.java | 124 +++++++++++ .../ice/demo/commands/FileHandlerExample.java | 202 ++++++++++++++++++ 3 files changed, 332 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/CommandFactoryExample.java create mode 100644 org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/FileHandlerExample.java diff --git a/org.eclipse.ice.demo/.classpath b/org.eclipse.ice.demo/.classpath index eca7bdba8..51bfd8771 100644 --- a/org.eclipse.ice.demo/.classpath +++ b/org.eclipse.ice.demo/.classpath @@ -1,7 +1,12 @@ - + + + + + + diff --git a/org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/CommandFactoryExample.java b/org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/CommandFactoryExample.java new file mode 100644 index 000000000..7953788a2 --- /dev/null +++ b/org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/CommandFactoryExample.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Examples for running Commands API package org.eclipse.ice.commands + * Joe Osborn + *******************************************************************************/ +package org.eclipse.ice.demo.commands; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.eclipse.ice.commands.Command; +import org.eclipse.ice.commands.CommandConfiguration; +import org.eclipse.ice.commands.CommandFactory; +import org.eclipse.ice.commands.CommandStatus; +import org.eclipse.ice.commands.ConnectionConfiguration; + +/** + * This class shows an example for how to use the CommandFactory class to + * generate a Command which can execute on a local machine. + * + * @author Joe Osborn + * + */ +public class CommandFactoryExample { + + /** + * @param args + */ + public static void main(String[] args) { + + // Run an example test script + runLocalCommand(); + + return; + } + + /** + * This method runs a test dummy script on one's local computer. The dummy script is locating + * in the JUnit test directory of the Commands API project. + */ + static void runLocalCommand() { + + /** + * Create a CommandConfiguration with the necessary information to execute a + * Command. See {@link org.eclipse.ice.commands.CommandConfiguration} for + * relevant member variables/constructor. + */ + + // Create a factory to get the Command + CommandFactory factory = new CommandFactory(); + + // Get the local hostname for processing + String hostname = getLocalHostname(); + + // Get the present working directory + String pwd = System.getProperty("user.dir"); + + // Create the path relative to the current directory where the test script lives + String scriptDir = pwd + "/../org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/"; + + // Create the script path + String script = scriptDir + "test_code_execution.sh"; + + // Create the input file path + String inputFile = scriptDir + "someInputFile.txt"; + + // Set the CommandConfiguration class + CommandConfiguration commandConfig = new CommandConfiguration(); + commandConfig.setCommandId(1); + commandConfig.setExecutable(script); + commandConfig.setInputFile(inputFile); + commandConfig.setErrFileName("someErrFile.txt"); + commandConfig.setOutFileName("someOutFile.txt"); + commandConfig.setNumProcs("1"); + commandConfig.setInstallDirectory(""); + commandConfig.setOS(System.getProperty("os.name")); + commandConfig.setWorkingDirectory(pwd); + commandConfig.setAppendInput(true); + + // Make a ConnectionConfiguration to indicate that we want to run locally + ConnectionConfiguration connectionConfig = new ConnectionConfiguration(hostname); + + // Get the command + Command localCommand = null; + try { + localCommand = factory.getCommand(commandConfig, connectionConfig); + } catch (IOException e) { + e.printStackTrace(); + } + + // Run it + CommandStatus status = localCommand.execute(); + + System.out.println("Status of Command after execution: " + status); + return; + } + + /** + * This function just returns the local hostname of your local computer + * + * @return - String - local hostname + */ + protected static String getLocalHostname() { + // Get the hostname for your local computer + InetAddress addr = null; + try { + addr = InetAddress.getLocalHost(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + + String hostname = addr.getHostName(); + + return hostname; + } + +} diff --git a/org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/FileHandlerExample.java b/org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/FileHandlerExample.java new file mode 100644 index 000000000..513ed6284 --- /dev/null +++ b/org.eclipse.ice.demo/src/org/eclipse/ice/demo/commands/FileHandlerExample.java @@ -0,0 +1,202 @@ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Examples for running Commands API package org.eclipse.ice.commands + * Joe Osborn + *******************************************************************************/ +package org.eclipse.ice.demo.commands; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.ice.commands.CommandStatus; +import org.eclipse.ice.commands.FileHandlerFactory; +import org.eclipse.ice.commands.IFileHandler; + +/** + * This class is intended to be an example showing how to use the local + * FileHandler API as given in the Commands API + * + * @author Joe Osborn + * + */ +public class FileHandlerExample { + + static String localSource; + + static String localDestination; + + // A string with a new name for the move command to use + static String newName = "/newfilename.txt"; + + /** + * Main function to show an example of how the Commands API moves and copies + * files + * + * @param args + */ + public static void main(String[] args) { + + // Create a temporary dummy file and directory locally to play around with + createDummyLocalFile(); + + // Copy the file to the directory + copyFileLocally(); + + // Move the file to the same directory with a new name + moveFileLocally(); + + // Delete the created file and directory + cleanUpFiles(); + + return; + } + + /** + * This function copies a test file to a test directory locally. It demonstrates + * the use of the FileHandler API within the Commands API + */ + public static void copyFileLocally() { + FileHandlerFactory factory = new FileHandlerFactory(); + IFileHandler handler = null; + + // Get the appropriate remote/local FileHandler + try { + handler = factory.getFileHandler(); + } catch (IOException e) { + e.printStackTrace(); + } + + // Now try to copy the file + try { + CommandStatus status = handler.copy(localSource, localDestination); + if (status != CommandStatus.SUCCESS) + System.out.println("Copy file failed! Check console for error messages"); + } catch (IOException e) { + e.printStackTrace(); + } + + // We can also check that the file exists + try { + boolean exist = handler.exists(localDestination); + if (exist) + System.out.println("Copy file successful!"); + } catch (IOException e) { + e.printStackTrace(); + } + + return; + } + + /** + * This function moves a test file to a test directory locally and gives it a + * new name. It demonstrates the use of the FileHandler API within the Commands + * API + */ + public static void moveFileLocally() { + FileHandlerFactory factory = new FileHandlerFactory(); + IFileHandler handler = null; + + // Get the appropriate remote/local FileHandler + try { + handler = factory.getFileHandler(); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + CommandStatus status = handler.move(localSource, localDestination + newName); + if (status != CommandStatus.SUCCESS) + System.out.println("Copy file failed! Check console for error messages"); + } catch (IOException e) { + e.printStackTrace(); + } + + // We can also check that the file exists + try { + boolean exist = handler.exists(localDestination + newName); + if (exist) + System.out.println("Move file successful!"); + } catch (IOException e) { + e.printStackTrace(); + } + + return; + } + + /** + * This function creates a dummy temporary file locally for the copy and move + * commands to use. It also creates a dummy temporary directory to copy/move the + * file to. + */ + public static void createDummyLocalFile() { + // First create a dummy text file to test + String source = "dummyfile.txt"; + Path sourcePath = null; + try { + sourcePath = Files.createTempFile(null, source); + } catch (IOException e) { + e.printStackTrace(); + } + // Turn the path into a string to pass to the command + localSource = sourcePath.toString(); + System.out.println("Created source file at: " + localSource); + + // Do the same for the destination + Path destinationPath = null; + String dest = "testCopyDirectory"; + try { + destinationPath = Files.createTempDirectory(dest); + } catch (IOException e) { + e.printStackTrace(); + } + // Turn the path into a string to give to the command + localDestination = destinationPath.toString(); + System.out.println("Created destination file at: " + localDestination); + } + + /** + * This function cleans up the temporary files and directory created + */ + public static void cleanUpFiles() { + boolean deleteDestination = false; + + // Don't need to delete the source file since we moved it, so by definition the temporary file + // is no longer at it's source location + deleteDestination = deleteDirectory(new File(localDestination)); + if (!deleteDestination) { + System.out.println("Couldn't delete destination file/directory at: " + localDestination); + } + else { + System.out.println("Deleted files successfully!"); + } + + return; + } + + /** + * A simple test method to recursively delete temporary files/directories + * created in this test class + * + * @param directory - top level directory from which to delete everything + * underneath + * @return - boolean - true if everything deleted, false if not + */ + public static boolean deleteDirectory(File directory) { + File[] contents = directory.listFiles(); + if (contents != null) { + for (File file : contents) { + deleteDirectory(file); + } + } + return directory.delete(); + } + +}