From: <pat...@us...> - 2009-09-05 23:45:36
|
Revision: 946 http://cishell.svn.sourceforge.net/cishell/?rev=946&view=rev Author: pataphil Date: 2009-09-05 23:45:29 +0000 (Sat, 05 Sep 2009) Log Message: ----------- Very minor code cleanup/refactoring. Modified Paths: -------------- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java Modified: trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java =================================================================== --- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2009-09-04 21:08:26 UTC (rev 945) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2009-09-05 23:45:29 UTC (rev 946) @@ -106,7 +106,6 @@ tempDirPath = makeTempDirectory(); algDirPath = tempDirPath + File.separator + props.getProperty("Algorithm-Directory") + File.separator; - } /** @@ -194,15 +193,18 @@ } } - protected File[] executeProgram(String[] cmdarray, String baseDirPath) throws AlgorithmExecutionException { - //remember which files were in the directory before we ran the program + protected File[] executeProgram(String[] commandArray, String baseDirPath) throws AlgorithmExecutionException { + /* + * Remember which files were in the directory before we ran + * the program. + */ File baseDir = new File(baseDirPath); String[] beforeFiles = baseDir.list(); //create and run the executing process Process process = null; try { - process = Runtime.getRuntime().exec(cmdarray, null, new File(baseDirPath)); + process = Runtime.getRuntime().exec(commandArray, null, new File(baseDirPath)); process.getOutputStream().close(); } catch (IOException e1) { throw new AlgorithmExecutionException(e1.getMessage(), e1); @@ -440,13 +442,15 @@ str = str.replaceAll("\\$\\{executable\\}", props.getProperty("executable")); for (int i = 0; i < data.length; i++) { - String file = ((File) data[i].getData()).getAbsolutePath(); + File inFile = (File)data[i].getData(); + String filePath = inFile.getAbsolutePath(); + System.err.println("inFile[" + i + "]: \"" + filePath + "\" exists: " + inFile.exists()); if (File.separatorChar == '\\') { - file = file.replace(File.separatorChar, '/'); + filePath = filePath.replace(File.separatorChar, '/'); } - str = str.replaceAll("\\$\\{inFile\\[" + i + "\\]\\}", file); + str = str.replaceAll("\\$\\{inFile\\[" + i + "\\]\\}", filePath); if (File.separatorChar == '\\') { str = str.replace('/', File.separatorChar); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pat...@us...> - 2009-09-05 23:47:59
|
Revision: 947 http://cishell.svn.sourceforge.net/cishell/?rev=947&view=rev Author: pataphil Date: 2009-09-05 23:47:51 +0000 (Sat, 05 Sep 2009) Log Message: ----------- Forgot to remove a debug message before. Modified Paths: -------------- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java Modified: trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java =================================================================== --- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2009-09-05 23:45:29 UTC (rev 946) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2009-09-05 23:47:51 UTC (rev 947) @@ -444,7 +444,6 @@ for (int i = 0; i < data.length; i++) { File inFile = (File)data[i].getData(); String filePath = inFile.getAbsolutePath(); - System.err.println("inFile[" + i + "]: \"" + filePath + "\" exists: " + inFile.exists()); if (File.separatorChar == '\\') { filePath = filePath.replace(File.separatorChar, '/'); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pat...@us...> - 2011-02-04 14:43:16
|
Revision: 1195 http://cishell.svn.sourceforge.net/cishell/?rev=1195&view=rev Author: pataphil Date: 2011-02-04 14:43:09 +0000 (Fri, 04 Feb 2011) Log Message: ----------- * Minor code cleanup. Modified Paths: -------------- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java Modified: trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java =================================================================== --- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-01-27 20:52:02 UTC (rev 1194) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-02-04 14:43:09 UTC (rev 1195) @@ -57,7 +57,7 @@ public static final String IN_FILE_PLACEHOLDER = "inFile"; private String ALGORITHM; - private String ALGORITHM_MACOSX_PPC; + private String macOSX_PPC_DirectoryPath; private String MACOSX; private String ALGORITHM_WIN32; private String WIN32; @@ -65,51 +65,68 @@ private String LINUX; private String ALGORITHM_DEFAULT; - protected final String algDirPath; - protected final String tempDirPath; - protected final Data[] data; - protected Dictionary parameters; - protected Properties props; - protected CIShellContext ciContext; - protected ProgressMonitor monitor; - protected BundleContext bContext; - protected String algName; + private String algorithmDirectoryPath; + private String temporaryDirectoryPath; + private Data[] data; + private Dictionary<String, Object> parameters; + private Properties properties; + private CIShellContext ciShellContext; + private ProgressMonitor monitor; + private BundleContext bundleContext; + private String algorithmName; - public StaticExecutableRunner(BundleContext bContext, CIShellContext ciContext, Properties props, - Dictionary parameters, Data[] data, ProgressMonitor monitor, String algName) throws IOException { + public StaticExecutableRunner( + BundleContext bundleContext, + CIShellContext ciShellContext, + Properties properties, + Dictionary<String, Object> parameters, + Data[] data, + ProgressMonitor monitor, + String algorithmName) throws IOException { + // Assign normal member variables. - // assign normal member-variables - - this.bContext = bContext; - this.ciContext = ciContext; - this.props = props; + this.bundleContext = bundleContext; + this.ciShellContext = ciShellContext; + this.properties = properties; this.parameters = parameters; this.data = data; this.monitor = monitor; - this.algName = algName; + this.algorithmName = algorithmName; - // determine directory paths for each platform, based on algName + // Determine directory paths for each platform, based on algorithmName. - ALGORITHM = algName + "/"; - ALGORITHM_MACOSX_PPC = ALGORITHM + "macosx.ppc/"; - MACOSX = "macosx"; - ALGORITHM_WIN32 = ALGORITHM + "win32/"; - WIN32 = "win32"; - ALGORITHM_LINUX_X86 = ALGORITHM + "linux.x86/"; - LINUX = "linux"; - ALGORITHM_DEFAULT = ALGORITHM + "default/"; + this.ALGORITHM = algorithmName + "/"; + this.macOSX_PPC_DirectoryPath = ALGORITHM + "macosx.ppc/"; + this.MACOSX = "macosx"; + this.ALGORITHM_WIN32 = ALGORITHM + "win32/"; + this.WIN32 = "win32"; + this.ALGORITHM_LINUX_X86 = ALGORITHM + "linux.x86/"; + this.LINUX = "linux"; + this.ALGORITHM_DEFAULT = ALGORITHM + "default/"; // if a constructor variable was null, use a null object version of it if possible - if (monitor == null) this.monitor = ProgressMonitor.NULL_MONITOR; - if (data == null) data = new Data[0]; - if (parameters == null) parameters = new Hashtable(); + if (this.monitor == null) { + this.monitor = ProgressMonitor.NULL_MONITOR; + } - // make a temporary directory to run the executable in + if (this.data == null) { + this.data = new Data[0]; + } - tempDirPath = makeTempDirectory(); + if (this.parameters == null) { + this.parameters = new Hashtable<String, Object>(); + } - algDirPath = tempDirPath + File.separator + props.getProperty("Algorithm-Directory") + File.separator; + // Make a temporary directory to run the executable in. + + this.temporaryDirectoryPath = makeTemporaryDirectory(); + this.algorithmDirectoryPath = String.format( + "%s%s%s%s", + temporaryDirectoryPath, + File.separator, + properties.getProperty("Algorithm-Directory"), + File.separator); } /** @@ -117,18 +134,19 @@ */ public Data[] execute() throws AlgorithmExecutionException { copyFilesUsedByExecutableIntoDir(getTempDirectory()); - makeDirExecutable(algDirPath); + makeDirExecutable(algorithmDirectoryPath); - String[] commandLineArguments = createCommandLineArguments(algDirPath, data, parameters); + String[] commandLineArguments = + createCommandLineArguments(algorithmDirectoryPath, this.data, this.parameters); - File[] rawOutput = executeProgram(commandLineArguments, algDirPath); + File[] rawOutput = executeProgram(commandLineArguments, algorithmDirectoryPath); return formatAsData(rawOutput); } private void copyFilesUsedByExecutableIntoDir(File dir) throws AlgorithmExecutionException { try { - Enumeration e = bContext.getBundle().getEntryPaths("/" + algName); + Enumeration e = bundleContext.getBundle().getEntryPaths("/" + algorithmName); Set entries = new HashSet(); @@ -140,11 +158,11 @@ } } - dir = new File(dir.getPath() + File.separator + algName); + dir = new File(dir.getPath() + File.separator + algorithmName); dir.mkdirs(); - String os = bContext.getProperty("osgi.os"); - String arch = bContext.getProperty("osgi.arch"); + String os = bundleContext.getProperty("osgi.os"); + String arch = bundleContext.getProperty("osgi.arch"); String path = null; @@ -159,8 +177,8 @@ // but override with platform idiosyncracies if (os.equals(WIN32) && entries.contains(ALGORITHM_WIN32)) { path = ALGORITHM_WIN32; - } else if (os.equals(MACOSX) && entries.contains(ALGORITHM_MACOSX_PPC)) { - path = ALGORITHM_MACOSX_PPC; + } else if (os.equals(MACOSX) && entries.contains(macOSX_PPC_DirectoryPath)) { + path = macOSX_PPC_DirectoryPath; } else if (os.equals(LINUX) && entries.contains(ALGORITHM_LINUX_X86)) { path = ALGORITHM_LINUX_X86; } @@ -187,7 +205,7 @@ // FIXME: Surely java has a way to do this!!!! if (new File("/bin/chmod").exists()) { try { - String executable = baseDir + props.getProperty("executable"); + String executable = baseDir + properties.getProperty("executable"); Runtime.getRuntime().exec("/bin/chmod +x " + executable).waitFor(); } catch (IOException e) { throw new AlgorithmExecutionException(e); @@ -295,7 +313,7 @@ } protected Data[] formatAsData(File[] files) { - String outData = (String) props.get(AlgorithmProperty.OUT_DATA); + String outData = (String) properties.get(AlgorithmProperty.OUT_DATA); // if out data is null then it returns no data if (("" + outData).trim().equalsIgnoreCase(AlgorithmProperty.NULL_DATA)) { @@ -317,7 +335,7 @@ } for (int i = 0; i < data.length; i++) { - String file = props.getProperty("outFile[" + i + "]", null); + String file = properties.getProperty("outFile[" + i + "]", null); if (i < formats.length) { File f = (File) nameToFileMap.remove(file); @@ -325,10 +343,10 @@ if (f != null) { data[i] = new BasicData(f, formats[i]); - String label = props.getProperty("outFile[" + i + "].label", f.getName()); + String label = properties.getProperty("outFile[" + i + "].label", f.getName()); data[i].getMetadata().put(DataProperty.LABEL, label); - String type = props.getProperty("outFile[" + i + "].type", DataProperty.OTHER_TYPE); + String type = properties.getProperty("outFile[" + i + "].type", DataProperty.OTHER_TYPE); type = type.trim(); if (type.equalsIgnoreCase(DataProperty.MATRIX_TYPE)) { type = DataProperty.MATRIX_TYPE; @@ -387,7 +405,7 @@ protected StringBuffer log(int logLevel, StringBuffer buffer) { if (buffer.indexOf("\n") != -1) { // any new newlines to output? - LogService log = (LogService) ciContext.getService(LogService.class.getName()); + LogService log = (LogService) ciShellContext.getService(LogService.class.getName()); int lastGoodIndex = 0; int fromIndex = 0; @@ -420,23 +438,25 @@ return buffer; } - protected String[] createCommandLineArguments(String algDir, Data[] data, Dictionary parameters) { - String template = "" + props.getProperty("template"); - String[] cmdarray = template.split("\\s"); + protected String[] createCommandLineArguments( + String algorithmDirectory, Data[] data, Dictionary<String, Object> parameters) { + String template = "" + this.properties.getProperty("template"); + String[] commands = template.split("\\s"); - for (int i = 0; i < cmdarray.length; i++) { - cmdarray[i] = substituteVars(cmdarray[i], data, parameters); + for (int ii = 0; ii < commands.length; ii++) { + commands[ii] = substituteVars(commands[ii], data, parameters); } // TODO: Expanded later to support .cmd and other extensions - if (!new File(algDir + cmdarray[0]).exists()) { - if (new File(algDir + cmdarray[0] + ".bat").exists()) { - cmdarray[0] = cmdarray[0] + ".bat"; + if (!new File(algorithmDirectory + commands[0]).exists()) { + if (new File(algorithmDirectory + commands[0] + ".bat").exists()) { + commands[0] = commands[0] + ".bat"; } } - cmdarray[0] = algDir + cmdarray[0]; - return cmdarray; + commands[0] = algorithmDirectory + commands[0]; + + return commands; } // replaces place-holder variables in the template with the actual arguments the executable needs to work. @@ -444,9 +464,10 @@ // (also, real values like "6" or "dog" instead of placeholders for parameters) protected String substituteVars(String str, Data[] data, Dictionary parameters) { str = str.replaceAll( - "\\$\\{" + EXECUTABLE_PLACEHOLDER + "\\}", props.getProperty(EXECUTABLE_PLACEHOLDER)); + "\\$\\{" + EXECUTABLE_PLACEHOLDER + "\\}", properties.getProperty(EXECUTABLE_PLACEHOLDER)); for (int ii = 0; ii < data.length; ii++) { + System.err.println(data[0]); String label = data[0].getMetadata().get(DataProperty.LABEL).toString(); String escapedLabel = label.replaceAll("\\\\", "/"); str = str.replaceAll( @@ -479,10 +500,10 @@ } public File getTempDirectory() { - return new File(tempDirPath); + return new File(temporaryDirectoryPath); } - protected String makeTempDirectory() throws IOException { + protected String makeTemporaryDirectory() throws IOException { File sessionDir = Activator.getTempDirectory(); File dir = File.createTempFile("StaticExecutableRunner-", "", sessionDir); @@ -493,7 +514,7 @@ } private void copyDir(File dir, String dirPath, int depth) throws IOException { - Enumeration e = bContext.getBundle().getEntryPaths(dirPath); + Enumeration e = bundleContext.getBundle().getEntryPaths(dirPath); // dirPath = dirPath.replace('/', File.separatorChar); @@ -515,7 +536,7 @@ } private void copyFile(File dir, String path) throws IOException { - URL entry = bContext.getBundle().getEntry(path); + URL entry = bundleContext.getBundle().getEntry(path); // path = path.replace('/', File.separatorChar); String file = getName(path); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jrb...@us...> - 2011-03-30 21:04:40
|
Revision: 1239 http://cishell.svn.sourceforge.net/cishell/?rev=1239&view=rev Author: jrbibers Date: 2011-03-30 21:04:34 +0000 (Wed, 30 Mar 2011) Log Message: ----------- StaticExecutableRunner now only checks each datum's label and file path when necessary, that is, when it occurs in the static executable's command-line template (as specified in config.properties files). Additionally, when either of these pieces of information are requested, we now check whether they are null before calling toString. In the case that we are null, we get the semi-palatable defaults "unknown_data_label" and "unknown_file_path", respectively. Reviewed by Chin Hua. Modified Paths: -------------- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java Modified: trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java =================================================================== --- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-03-07 19:35:47 UTC (rev 1238) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-03-30 21:04:34 UTC (rev 1239) @@ -34,6 +34,7 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.regex.Pattern; import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; @@ -462,41 +463,85 @@ // replaces place-holder variables in the template with the actual arguments the executable needs to work. // (real names of files instead of inFile[i], for instance) // (also, real values like "6" or "dog" instead of placeholders for parameters) - protected String substituteVars(String str, Data[] data, Dictionary parameters) { - str = str.replaceAll( + protected String substituteVars(String template, Data[] data, Dictionary parameters) { + template = template.replaceAll( "\\$\\{" + EXECUTABLE_PLACEHOLDER + "\\}", properties.getProperty(EXECUTABLE_PLACEHOLDER)); + /* + * Re-think: + * Shall we just name the same in config.propertities? + * Why not use parameter key for all inFile and data? + */ for (int ii = 0; ii < data.length; ii++) { - System.err.println(data[0]); - String label = data[0].getMetadata().get(DataProperty.LABEL).toString(); + template = substituteForDataLabel(template, data, ii); + template = substituteForFilePath(template, data, ii); + } + + for (Enumeration i = parameters.keys(); i.hasMoreElements();) { + String key = (String) i.nextElement(); + Object value = parameters.get(key); + + if (value == null) value = ""; + + template = template.replaceAll("\\$\\{" + key + "\\}", value.toString()); + } + + return template; + } + + private String substituteForDataLabel(String template, Data[] data, int ii) { + String key = String.format("${%s[%d]}", DATA_LABEL_PLACEHOLDER, ii); + + if (!template.contains(key)) { + return template; + } else { + Object labelObject = data[ii].getMetadata().get(DataProperty.LABEL); + + String label = "unknown_data_label"; + if (labelObject != null) { + label = labelObject.toString(); + } + + /* TODO Re-think converting backslash to slash for the following expects: + * i) Need to consider what is the system file separator + * ii) The backslash in data_label might have special meaning. + * iii) may be we should use " to quote the data_label + */ String escapedLabel = label.replaceAll("\\\\", "/"); - str = str.replaceAll( - "\\$\\{" + DATA_LABEL_PLACEHOLDER + "\\[" + ii + "\\]\\}", escapedLabel); + + return template.replaceAll(Pattern.quote(key), escapedLabel); + } + } - File inFile = (File)data[ii].getData(); - String filePath = inFile.getAbsolutePath(); - + private String substituteForFilePath(String template, Data[] data, int ii) { + String key = String.format("${%s[%d]}", IN_FILE_PLACEHOLDER, ii); + + if (!template.contains(key)) { + return template; + } else { + Object datumObject = data[ii].getData(); + + String filePath = "unknown_file_path"; + if (datumObject != null && datumObject instanceof File) { + File file = (File) datumObject; + filePath = file.getAbsolutePath(); + } + if (File.separatorChar == '\\') { filePath = filePath.replace(File.separatorChar, '/'); } - str = str.replaceAll("\\$\\{" + IN_FILE_PLACEHOLDER + "\\[" + ii + "\\]\\}", filePath); + String substituted = template.replaceAll(Pattern.quote(key), filePath); + /* TODO: Re-think - This auto convert every backslash and slash. + * Which might convert everything that you don't intend to convert. + */ if (File.separatorChar == '\\') { - str = str.replace('/', File.separatorChar); + substituted = substituted.replace('/', File.separatorChar); } + + return substituted; } - - for (Enumeration i = parameters.keys(); i.hasMoreElements();) { - String key = (String) i.nextElement(); - Object value = parameters.get(key); - - if (value == null) value = ""; - - str = str.replaceAll("\\$\\{" + key + "\\}", value.toString()); - } - - return str; } public File getTempDirectory() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jrb...@us...> - 2011-04-07 17:26:51
|
Revision: 1243 http://cishell.svn.sourceforge.net/cishell/?rev=1243&view=rev Author: jrbibers Date: 2011-04-07 17:26:42 +0000 (Thu, 07 Apr 2011) Log Message: ----------- Fixing static executable problems stemming from CIShell revision 1191 (January 11 2001). Specifically, since the GUESS static executable substitutes its arguments into (on Windows) a batch file or (on *nix) a shell script, the presence of whitespace or special/shell characters was not working cross-platform. In this revision we simplify the string substitution performed at the StaticExecutableRunner level, attempt to clean the string of troublesome characters like ">" and "&", and apply platform-appropriate argument quotation in the mentioned GUESS batch files and shell scripts. Renaming unrelated variables to conform to our CheckStyle. Reviewed by Micah. Revision Links: -------------- http://cishell.svn.sourceforge.net/cishell/?rev=1191&view=rev Modified Paths: -------------- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java Modified: trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java =================================================================== --- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-04-04 15:35:31 UTC (rev 1242) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-04-07 17:26:42 UTC (rev 1243) @@ -24,6 +24,7 @@ import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Dictionary; import java.util.Enumeration; import java.util.HashMap; @@ -34,7 +35,6 @@ import java.util.Map; import java.util.Properties; import java.util.Set; -import java.util.regex.Pattern; import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; @@ -53,18 +53,33 @@ * @author Bruce Herr (bh...@bh...) */ public class StaticExecutableRunner implements Algorithm { + public static final String DEFAULT_SAFE_SUBSTITUTE = "_"; + public static final Map<String, String> TROUBLE_CHARACTER_SUBSTITUTIONS; + static { + Map<String, String> m = new HashMap<String, String>(); + m.put("\"", "''"); + m.put(";", DEFAULT_SAFE_SUBSTITUTE); + m.put(",", DEFAULT_SAFE_SUBSTITUTE); + m.put("&", DEFAULT_SAFE_SUBSTITUTE); + m.put("|", DEFAULT_SAFE_SUBSTITUTE); + m.put("<", DEFAULT_SAFE_SUBSTITUTE); + m.put(">", DEFAULT_SAFE_SUBSTITUTE); + + TROUBLE_CHARACTER_SUBSTITUTIONS = Collections.unmodifiableMap(m); + } + public static final String EXECUTABLE_PLACEHOLDER = "executable"; public static final String DATA_LABEL_PLACEHOLDER = "data_label"; public static final String IN_FILE_PLACEHOLDER = "inFile"; - private String ALGORITHM; - private String macOSX_PPC_DirectoryPath; - private String MACOSX; - private String ALGORITHM_WIN32; - private String WIN32; - private String ALGORITHM_LINUX_X86; - private String LINUX; - private String ALGORITHM_DEFAULT; + private String algorithm; + private String macOsXPpcDirectoryPath; + private String macOsX; + private String algorithmWin32; + private String win32; + private String algorithmLinuxX86; + private String linux; + private String algorithmDefault; private String algorithmDirectoryPath; private String temporaryDirectoryPath; @@ -96,14 +111,14 @@ // Determine directory paths for each platform, based on algorithmName. - this.ALGORITHM = algorithmName + "/"; - this.macOSX_PPC_DirectoryPath = ALGORITHM + "macosx.ppc/"; - this.MACOSX = "macosx"; - this.ALGORITHM_WIN32 = ALGORITHM + "win32/"; - this.WIN32 = "win32"; - this.ALGORITHM_LINUX_X86 = ALGORITHM + "linux.x86/"; - this.LINUX = "linux"; - this.ALGORITHM_DEFAULT = ALGORITHM + "default/"; + this.algorithm = algorithmName + "/"; + this.macOsXPpcDirectoryPath = algorithm + "macosx.ppc/"; + this.macOsX = "macosx"; + this.algorithmWin32 = algorithm + "win32/"; + this.win32 = "win32"; + this.algorithmLinuxX86 = algorithm + "linux.x86/"; + this.linux = "linux"; + this.algorithmDefault = algorithm + "default/"; // if a constructor variable was null, use a null object version of it if possible @@ -168,26 +183,26 @@ String path = null; // take the default, if there - if (entries.contains(ALGORITHM_DEFAULT)) { - String default_path = ALGORITHM_DEFAULT; + if (entries.contains(algorithmDefault)) { + String defaultPath = algorithmDefault; // logger.log(LogService.LOG_DEBUG, "base path: "+default_path+ // "\n\t"+dir.getAbsolutePath() + "\n\n"); - copyDir(dir, default_path, 0); + copyDir(dir, defaultPath, 0); } // but override with platform idiosyncracies - if (os.equals(WIN32) && entries.contains(ALGORITHM_WIN32)) { - path = ALGORITHM_WIN32; - } else if (os.equals(MACOSX) && entries.contains(macOSX_PPC_DirectoryPath)) { - path = macOSX_PPC_DirectoryPath; - } else if (os.equals(LINUX) && entries.contains(ALGORITHM_LINUX_X86)) { - path = ALGORITHM_LINUX_X86; + if (os.equals(win32) && entries.contains(algorithmWin32)) { + path = algorithmWin32; + } else if (os.equals(macOsX) && entries.contains(macOsXPpcDirectoryPath)) { + path = macOsXPpcDirectoryPath; + } else if (os.equals(linux) && entries.contains(algorithmLinuxX86)) { + path = algorithmLinuxX86; } - String platform_path = ALGORITHM + os + "." + arch + "/"; + String platformPath = algorithm + os + "." + arch + "/"; // and always override anything with an exact match - if (entries.contains(platform_path)) { - path = platform_path; + if (entries.contains(platformPath)) { + path = platformPath; } if (path == null) { @@ -216,7 +231,8 @@ } } - protected File[] executeProgram(String[] commandArray, String baseDirPath) throws AlgorithmExecutionException { + protected File[] executeProgram(String[] commandArray, String baseDirPath) + throws AlgorithmExecutionException { /* * Remember which files were in the directory before we ran * the program. @@ -226,8 +242,11 @@ //create and run the executing process Process process = null; - try { - process = Runtime.getRuntime().exec(commandArray, null, new File(baseDirPath)); + try { + ProcessBuilder processBuilder = new ProcessBuilder(commandArray); + processBuilder.directory(new File(baseDirPath)); + process = processBuilder.start(); + process.getOutputStream().close(); } catch (IOException e1) { throw new AlgorithmExecutionException(e1.getMessage(), e1); @@ -238,10 +257,10 @@ monitor.start(ProgressMonitor.CANCELLABLE, -1); InputStream in = process.getInputStream(); - StringBuffer in_buffer = new StringBuffer(); + StringBuffer inBuffer = new StringBuffer(); InputStream err = process.getErrorStream(); - StringBuffer err_buffer = new StringBuffer(); + StringBuffer errBuffer = new StringBuffer(); Integer exitValue = null; boolean killedOnPurpose = false; @@ -249,8 +268,8 @@ while (!killedOnPurpose && exitValue == null) { //print its output, and watch to see if it has finished/died. - in_buffer = logStream(LogService.LOG_INFO, in, in_buffer); - err_buffer = logStream(LogService.LOG_ERROR, err, err_buffer); + inBuffer = logStream(LogService.LOG_INFO, in, inBuffer); + errBuffer = logStream(LogService.LOG_ERROR, err, errBuffer); if (monitor.isCanceled()) { killedOnPurpose = true; @@ -276,7 +295,9 @@ // if the process failed unexpectedly... if (process.exitValue() != 0 && !killedOnPurpose) { - throw new AlgorithmExecutionException("Algorithm exited unexpectedly (exit value: " + process.exitValue() + throw new AlgorithmExecutionException( + "Algorithm exited unexpectedly (exit value: " + + process.exitValue() + "). Please check the console window for any error messages."); } @@ -347,7 +368,8 @@ String label = properties.getProperty("outFile[" + i + "].label", f.getName()); data[i].getMetadata().put(DataProperty.LABEL, label); - String type = properties.getProperty("outFile[" + i + "].type", DataProperty.OTHER_TYPE); + String type = + properties.getProperty("outFile[" + i + "].type", DataProperty.OTHER_TYPE); type = type.trim(); if (type.equalsIgnoreCase(DataProperty.MATRIX_TYPE)) { type = DataProperty.MATRIX_TYPE; @@ -398,7 +420,8 @@ } catch (EOFException e) { // normal operation } catch (IOException e) { - throw new AlgorithmExecutionException("Error when processing the algorithm's screen output", e); + throw new AlgorithmExecutionException( + "Error when processing the algorithm's screen output", e); } return buffer; @@ -459,13 +482,16 @@ return commands; } - - // replaces place-holder variables in the template with the actual arguments the executable needs to work. - // (real names of files instead of inFile[i], for instance) - // (also, real values like "6" or "dog" instead of placeholders for parameters) + + /* replaces place-holder variables in the template with the actual arguments the executable + * needs to work. + * (real names of files instead of inFile[i], for instance) + * (also, real values like "6" or "dog" instead of placeholders for parameters) + */ protected String substituteVars(String template, Data[] data, Dictionary parameters) { - template = template.replaceAll( - "\\$\\{" + EXECUTABLE_PLACEHOLDER + "\\}", properties.getProperty(EXECUTABLE_PLACEHOLDER)); + template = template.replace( + String.format("${%s}", EXECUTABLE_PLACEHOLDER), + properties.getProperty(EXECUTABLE_PLACEHOLDER)); /* * Re-think: @@ -481,9 +507,11 @@ String key = (String) i.nextElement(); Object value = parameters.get(key); - if (value == null) value = ""; + if (value == null) { + value = ""; + } - template = template.replaceAll("\\$\\{" + key + "\\}", value.toString()); + template = template.replace(String.format("${%s}", key), value.toString()); } return template; @@ -502,17 +530,30 @@ label = labelObject.toString(); } - /* TODO Re-think converting backslash to slash for the following expects: - * i) Need to consider what is the system file separator - * ii) The backslash in data_label might have special meaning. - * iii) may be we should use " to quote the data_label - */ - String escapedLabel = label.replaceAll("\\\\", "/"); + String cleanedLabel = cleanDataLabel(label); - return template.replaceAll(Pattern.quote(key), escapedLabel); + return template.replace(key, cleanedLabel); } } + /* Replace each double-quote with two single-quotes. + * This alleviates some cross-platform problems with command-line template substitution. + * In particular, GUESS substitutes its values into batch/shell scripts and will not behave + * properly on every platform if these troublesome characters occur. + */ + private String cleanDataLabel(String label) { + String cleanedLabel = label; + + for (String troubleCharacter : TROUBLE_CHARACTER_SUBSTITUTIONS.keySet()) { + cleanedLabel = + cleanedLabel.replace( + troubleCharacter, + TROUBLE_CHARACTER_SUBSTITUTIONS.get(troubleCharacter)); + } + + return cleanedLabel; + } + private String substituteForFilePath(String template, Data[] data, int ii) { String key = String.format("${%s[%d]}", IN_FILE_PLACEHOLDER, ii); @@ -526,20 +567,9 @@ File file = (File) datumObject; filePath = file.getAbsolutePath(); } - - if (File.separatorChar == '\\') { - filePath = filePath.replace(File.separatorChar, '/'); - } - String substituted = template.replaceAll(Pattern.quote(key), filePath); + String substituted = template.replace(key, filePath); - /* TODO: Re-think - This auto convert every backslash and slash. - * Which might convert everything that you don't intend to convert. - */ - if (File.separatorChar == '\\') { - substituted = substituted.replace('/', File.separatorChar); - } - return substituted; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pat...@us...> - 2011-01-11 17:26:27
|
Revision: 1191 http://cishell.svn.sourceforge.net/cishell/?rev=1191&view=rev Author: pataphil Date: 2011-01-11 17:26:18 +0000 (Tue, 11 Jan 2011) Log Message: ----------- * Added ${data_label[x]} placeholder syntax support to static executable config.properties files (for: http://cns-jira.slis.indiana.edu/browse/SCISQUARED-354 ) * Reviewed by Joseph. Modified Paths: -------------- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java Modified: trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java =================================================================== --- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-01-10 02:34:09 UTC (rev 1190) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2011-01-11 17:26:18 UTC (rev 1191) @@ -52,6 +52,10 @@ * @author Bruce Herr (bh...@bh...) */ public class StaticExecutableRunner implements Algorithm { + public static final String EXECUTABLE_PLACEHOLDER = "executable"; + public static final String DATA_LABEL_PLACEHOLDER = "data_label"; + public static final String IN_FILE_PLACEHOLDER = "inFile"; + private String ALGORITHM; private String ALGORITHM_MACOSX_PPC; private String MACOSX; @@ -439,17 +443,23 @@ // (real names of files instead of inFile[i], for instance) // (also, real values like "6" or "dog" instead of placeholders for parameters) protected String substituteVars(String str, Data[] data, Dictionary parameters) { - str = str.replaceAll("\\$\\{executable\\}", props.getProperty("executable")); + str = str.replaceAll( + "\\$\\{" + EXECUTABLE_PLACEHOLDER + "\\}", props.getProperty(EXECUTABLE_PLACEHOLDER)); - for (int i = 0; i < data.length; i++) { - File inFile = (File)data[i].getData(); + for (int ii = 0; ii < data.length; ii++) { + String label = data[0].getMetadata().get(DataProperty.LABEL).toString(); + String escapedLabel = label.replaceAll("\\\\", "/"); + str = str.replaceAll( + "\\$\\{" + DATA_LABEL_PLACEHOLDER + "\\[" + ii + "\\]\\}", escapedLabel); + + File inFile = (File)data[ii].getData(); String filePath = inFile.getAbsolutePath(); if (File.separatorChar == '\\') { filePath = filePath.replace(File.separatorChar, '/'); } - str = str.replaceAll("\\$\\{inFile\\[" + i + "\\]\\}", filePath); + str = str.replaceAll("\\$\\{" + IN_FILE_PLACEHOLDER + "\\[" + ii + "\\]\\}", filePath); if (File.separatorChar == '\\') { str = str.replace('/', File.separatorChar); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |