From: <bh...@us...> - 2006-09-22 17:11:29
|
Revision: 206 http://svn.sourceforge.net/cishell/?rev=206&view=rev Author: bh2 Date: 2006-09-22 10:11:22 -0700 (Fri, 22 Sep 2006) Log Message: ----------- Fixed two bugs with static executable runner: * tokenized the command string so that we can eliminate spurious quotation marks * output files are correctly linked to now 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 2006-09-21 13:58:29 UTC (rev 205) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2006-09-22 17:11:22 UTC (rev 206) @@ -70,7 +70,7 @@ String algDir = tempDir + File.separator + props.getProperty("Algorithm-Directory") + File.separator; chmod(algDir); - File[] output = execute(algDir + getTemplate(), algDir); + File[] output = execute(getTemplate(algDir), algDir); return toData(output); } catch (Exception e) { @@ -137,11 +137,11 @@ } } - protected File[] execute(String execString, String baseDir) throws Exception { + protected File[] execute(String[] cmdarray, String baseDir) throws Exception { File dir = new File(baseDir); String[] beforeFiles = dir.list(); - Process process = Runtime.getRuntime().exec(execString, null, new File(baseDir)); + Process process = Runtime.getRuntime().exec(cmdarray, null, new File(baseDir)); logStream(LogService.LOG_INFO, process.getInputStream()); logStream(LogService.LOG_ERROR, process.getErrorStream()); @@ -154,7 +154,6 @@ Arrays.sort(afterFiles); List outputs = new ArrayList(); - String tempDir = this.tempDir + File.separator; int beforeIndex = 0; int afterIndex = 0; @@ -164,14 +163,14 @@ beforeIndex++; afterIndex++; } else { - outputs.add(new File(tempDir + afterFiles[afterIndex])); + outputs.add(new File(baseDir + afterFiles[afterIndex])); afterIndex++; } - } + } //get any remaining new files while (afterIndex < afterFiles.length) { - outputs.add(new File(tempDir + afterFiles[afterIndex])); + outputs.add(new File(baseDir + afterFiles[afterIndex])); afterIndex++; } @@ -196,14 +195,24 @@ } } - protected String getTemplate() { - String template = props.getProperty("template"); + protected String[] getTemplate(String algDir) { + String template = "" + props.getProperty("template"); + String[] cmdarray = template.split("\\s"); - template = template.replaceAll("\\$\\{executable\\}", props.getProperty("executable")); + for (int i=0; i < cmdarray.length; i++) { + cmdarray[i] = substiteVars(cmdarray[i]); + } + cmdarray[0] = algDir + cmdarray[0]; + return cmdarray; + } + + protected String substiteVars(String str) { + str = str.replaceAll("\\$\\{executable\\}", props.getProperty("executable")); + for (int i=0; i < data.length; i++) { String file = ((File) data[i].getData()).getAbsolutePath(); - template = template.replaceAll("\\$\\{inFile\\["+i+"\\]\\}", "\""+file+"\""); + str = str.replaceAll("\\$\\{inFile\\["+i+"\\]\\}", file); } for (Enumeration i=parameters.keys(); i.hasMoreElements(); ) { @@ -212,10 +221,10 @@ if (value == null) value = ""; - template = template.replaceAll("\\$\\{"+key+"\\}", value.toString()); + str = str.replaceAll("\\$\\{"+key+"\\}", value.toString()); } - return template; + return str; } public File getTempDirectory() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <hu...@us...> - 2006-10-17 21:35:13
|
Revision: 299 http://svn.sourceforge.net/cishell/?rev=299&view=rev Author: huangb Date: 2006-10-17 14:35:00 -0700 (Tue, 17 Oct 2006) Log Message: ----------- Bug Fix: Check process exitValue. If the static executable algorithm crashes in the middle of the runtime, display the error using the guibuilder 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 2006-10-17 21:31:54 UTC (rev 298) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2006-10-17 21:35:00 UTC (rev 299) @@ -35,6 +35,7 @@ import org.cishell.framework.data.BasicData; import org.cishell.framework.data.Data; import org.cishell.framework.data.DataProperty; +import org.cishell.service.guibuilder.GUIBuilderService; import org.cishell.templates.Activator; import org.osgi.framework.BundleContext; import org.osgi.service.log.LogService; @@ -45,10 +46,12 @@ */ public class StaticExecutableRunner implements Algorithm { protected final String tempDir; + protected final GUIBuilderService guiBuilder; protected final Data[] data; protected Dictionary parameters; protected Properties props; protected CIShellContext ciContext; + public StaticExecutableRunner(BundleContext bContext, CIShellContext ciContext, Properties props, Dictionary parameters, Data[] data) throws IOException { this.ciContext = ciContext; @@ -56,6 +59,9 @@ this.parameters = parameters; this.data = data; + guiBuilder = (GUIBuilderService)ciContext.getService(GUIBuilderService.class.getName()); + + if (data == null) data = new Data[0]; if (parameters == null) parameters = new Hashtable(); @@ -147,6 +153,14 @@ logStream(LogService.LOG_ERROR, process.getErrorStream()); process.waitFor(); + //successfully ran? + if (process.exitValue() != 0) { + //display the error message using gui builder + guiBuilder.showError("Algorithm Could Not Finish Execution", "Sorry, the algorithm could not finish execution.", + "Please check the console window for the error log messages and report the bug.\n" + +"Thank you."); + } + //get the outputted files String[] afterFiles = dir.list(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bh...@us...> - 2006-10-23 16:50:14
|
Revision: 304 http://svn.sourceforge.net/cishell/?rev=304&view=rev Author: bh2 Date: 2006-10-23 09:50:05 -0700 (Mon, 23 Oct 2006) Log Message: ----------- 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 2006-10-19 19:22:57 UTC (rev 303) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2006-10-23 16:50:05 UTC (rev 304) @@ -111,6 +111,20 @@ String label = props.getProperty("outFile["+i+"].label", f.getName()); data[i].getMetaData().put(DataProperty.LABEL, label); + + String type = props.getProperty("outFile["+i+"].type", DataProperty.OTHER_TYPE); + + if (type.equalsIgnoreCase(DataProperty.MATRIX_TYPE)) { + type = DataProperty.MATRIX_TYPE; + } else if (type.equalsIgnoreCase(DataProperty.NETWORK_TYPE)) { + type = DataProperty.NETWORK_TYPE; + } else if (type.equalsIgnoreCase(DataProperty.TREE_TYPE)) { + type = DataProperty.TREE_TYPE; + } else { + type = DataProperty.OTHER_TYPE; + } + + data[i].getMetaData().put(DataProperty.TYPE, type); } } else { Iterator iter = nameToFileMap.values().iterator(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <hu...@us...> - 2006-10-23 20:45:41
|
Revision: 305 http://svn.sourceforge.net/cishell/?rev=305&view=rev Author: huangb Date: 2006-10-23 13:45:24 -0700 (Mon, 23 Oct 2006) Log Message: ----------- trim the string of the "outFile[*].type" and get rid of whitespace if any. 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 2006-10-23 16:50:05 UTC (rev 304) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2006-10-23 20:45:24 UTC (rev 305) @@ -113,7 +113,7 @@ data[i].getMetaData().put(DataProperty.LABEL, label); String type = props.getProperty("outFile["+i+"].type", DataProperty.OTHER_TYPE); - + type = type.trim(); if (type.equalsIgnoreCase(DataProperty.MATRIX_TYPE)) { type = DataProperty.MATRIX_TYPE; } else if (type.equalsIgnoreCase(DataProperty.NETWORK_TYPE)) { @@ -123,7 +123,7 @@ } else { type = DataProperty.OTHER_TYPE; } - + data[i].getMetaData().put(DataProperty.TYPE, type); } } else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bh...@us...> - 2006-10-24 14:34:59
|
Revision: 312 http://svn.sourceforge.net/cishell/?rev=312&view=rev Author: bh2 Date: 2006-10-24 07:34:55 -0700 (Tue, 24 Oct 2006) Log Message: ----------- now runner checks to see if there is any outData for an executable program 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 2006-10-24 14:05:50 UTC (rev 311) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2006-10-24 14:34:55 UTC (rev 312) @@ -86,8 +86,15 @@ } protected Data[] toData(File[] files) { - String[] formats = ((String)props.get(AlgorithmProperty.OUT_DATA)).split(","); + String outData = (String)props.get(AlgorithmProperty.OUT_DATA); + //if out data is null then it returns no data + if ((""+outData).trim().equalsIgnoreCase(AlgorithmProperty.NULL_DATA)) { + return null; + } + + String[] formats = outData.split(","); + Map nameToFileMap = new HashMap(); for (int i=0; i < files.length; i++) { nameToFileMap.put(files[i].getName(), files[i]); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <hu...@us...> - 2006-10-26 21:27:30
|
Revision: 323 http://svn.sourceforge.net/cishell/?rev=323&view=rev Author: huangb Date: 2006-10-26 14:27:25 -0700 (Thu, 26 Oct 2006) Log Message: ----------- add the support for text and grace file format 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 2006-10-26 20:15:50 UTC (rev 322) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2006-10-26 21:27:25 UTC (rev 323) @@ -127,6 +127,10 @@ type = DataProperty.NETWORK_TYPE; } else if (type.equalsIgnoreCase(DataProperty.TREE_TYPE)) { type = DataProperty.TREE_TYPE; + } else if(type.equalsIgnoreCase(DataProperty.TEXT_TYPE)){ + type = DataProperty.TEXT_TYPE; + } else if (type.equalsIgnoreCase(DataProperty.GRACE_TYPE)){ + type = DataProperty.GRACE_TYPE; } else { type = DataProperty.OTHER_TYPE; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <hu...@us...> - 2006-12-05 20:52:42
|
Revision: 344 http://svn.sourceforge.net/cishell/?rev=344&view=rev Author: huangb Date: 2006-12-05 12:52:38 -0800 (Tue, 05 Dec 2006) Log Message: ----------- Changed executable runner to check for .bat file, and will execute a batch file if an executable does not exists (for Windows). 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 2006-12-04 22:10:24 UTC (rev 343) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2006-12-05 20:52:38 UTC (rev 344) @@ -241,6 +241,13 @@ for (int i=0; i < cmdarray.length; i++) { cmdarray[i] = substiteVars(cmdarray[i]); } + + //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"; + } + } cmdarray[0] = algDir + cmdarray[0]; return cmdarray; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fu...@us...> - 2008-01-30 16:55:13
|
Revision: 608 http://cishell.svn.sourceforge.net/cishell/?rev=608&view=rev Author: fugu13 Date: 2008-01-30 08:54:01 -0800 (Wed, 30 Jan 2008) Log Message: ----------- This is the potential fix via spinning off a separate thread for each logStream method. 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 2008-01-28 22:54:39 UTC (rev 607) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2008-01-30 16:54:01 UTC (rev 608) @@ -172,10 +172,22 @@ File dir = new File(baseDir); String[] beforeFiles = dir.list(); - Process process = Runtime.getRuntime().exec(cmdarray, null, new File(baseDir)); - - logStream(LogService.LOG_INFO, process.getInputStream()); - logStream(LogService.LOG_ERROR, process.getErrorStream()); + final Process process = Runtime.getRuntime().exec(cmdarray, null, new File(baseDir)); + + process.getOutputStream().close(); + + new Thread(new Runnable() { + public void run() { + logStream(LogService.LOG_INFO, process.getInputStream()); + } + }).start(); + + new Thread(new Runnable() { + public void run() { + logStream(LogService.LOG_INFO, process.getErrorStream()); + } + }).start(); + process.waitFor(); //successfully ran? This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fu...@us...> - 2008-01-30 19:14:11
|
Revision: 610 http://cishell.svn.sourceforge.net/cishell/?rev=610&view=rev Author: fugu13 Date: 2008-01-30 11:13:58 -0800 (Wed, 30 Jan 2008) Log Message: ----------- Hook error stream up to the right logging type. 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 2008-01-30 17:06:03 UTC (rev 609) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2008-01-30 19:13:58 UTC (rev 610) @@ -184,7 +184,7 @@ new Thread(new Runnable() { public void run() { - logStream(LogService.LOG_INFO, process.getErrorStream()); + logStream(LogService.LOG_ERROR, process.getErrorStream()); } }).start(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bh...@us...> - 2008-02-01 01:48:15
|
Revision: 613 http://cishell.svn.sourceforge.net/cishell/?rev=613&view=rev Author: bh2 Date: 2008-01-31 17:48:12 -0800 (Thu, 31 Jan 2008) Log Message: ----------- The road to buggy software is paved with many 'quick fixes'... Updated StaticExecutableRunner to no longer spawn any threads. All logging and polling is done in the thread the algorithm was executed in. 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 2008-01-31 21:07:23 UTC (rev 612) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableRunner.java 2008-02-01 01:48:12 UTC (rev 613) @@ -13,11 +13,10 @@ * ***************************************************************************/ package org.cishell.templates.staticexecutable; -import java.io.BufferedReader; +import java.io.EOFException; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Dictionary; @@ -46,318 +45,341 @@ * @author Bruce Herr (bh...@bh...) */ public class StaticExecutableRunner implements Algorithm { - protected final String tempDir; - protected final GUIBuilderService guiBuilder; - protected final Data[] data; - protected Dictionary parameters; - protected Properties props; - protected CIShellContext ciContext; - - protected ProgressMonitor monitor; - - protected Boolean processRunning = new Boolean(true); - protected Boolean killedOnPurpose = new Boolean(false); - + protected final String tempDir; + protected final GUIBuilderService guiBuilder; + protected final Data[] data; + protected Dictionary parameters; + protected Properties props; + protected CIShellContext ciContext; + protected ProgressMonitor monitor; - public StaticExecutableRunner(BundleContext bContext, CIShellContext ciContext, Properties props, - Dictionary parameters, Data[] data, ProgressMonitor monitor) throws IOException { - this.ciContext = ciContext; - this.props = props; - this.parameters = parameters; - this.data = data; - - this.monitor = monitor; - - guiBuilder = (GUIBuilderService)ciContext.getService(GUIBuilderService.class.getName()); + public StaticExecutableRunner(BundleContext bContext, + CIShellContext ciContext, Properties props, Dictionary parameters, + Data[] data, ProgressMonitor monitor) throws IOException { + this.ciContext = ciContext; + this.props = props; + this.parameters = parameters; + this.data = data; + this.monitor = monitor; + if (monitor == null) + this.monitor = ProgressMonitor.NULL_MONITOR; + if (data == null) + data = new Data[0]; + if (parameters == null) + parameters = new Hashtable(); - - if (data == null) data = new Data[0]; - if (parameters == null) parameters = new Hashtable(); - - tempDir = makeTempDirectory(); - } - - /** - * @see org.cishell.framework.algorithm.Algorithm#execute() - */ - public Data[] execute() { - try { - String algDir = tempDir + File.separator + props.getProperty("Algorithm-Directory") - + File.separator; - - chmod(algDir); - File[] output = execute(getTemplate(algDir), algDir); - - return toData(output); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); - } - } - - protected Data[] toData(File[] files) { - String outData = (String)props.get(AlgorithmProperty.OUT_DATA); - - //if out data is null then it returns no data - if ((""+outData).trim().equalsIgnoreCase(AlgorithmProperty.NULL_DATA)) { - return null; - } - - String[] formats = outData.split(","); - - Map nameToFileMap = new HashMap(); - for (int i=0; i < files.length; i++) { - nameToFileMap.put(files[i].getName(), files[i]); - } - - Data[] data = null; - if (formats.length > files.length) { - data = new Data[formats.length]; - } else { - data = new Data[files.length]; - } - - for (int i=0; i < data.length; i++) { - String file = props.getProperty("outFile["+i+"]", null); - - if (i < formats.length) { - File f = (File) nameToFileMap.remove(file); - - if (f != null) { - data[i] = new BasicData(f,formats[i]); - - String label = props.getProperty("outFile["+i+"].label", f.getName()); - data[i].getMetaData().put(DataProperty.LABEL, label); - - String type = props.getProperty("outFile["+i+"].type", DataProperty.OTHER_TYPE); - type = type.trim(); - if (type.equalsIgnoreCase(DataProperty.MATRIX_TYPE)) { - type = DataProperty.MATRIX_TYPE; - } else if (type.equalsIgnoreCase(DataProperty.NETWORK_TYPE)) { - type = DataProperty.NETWORK_TYPE; - } else if (type.equalsIgnoreCase(DataProperty.TREE_TYPE)) { - type = DataProperty.TREE_TYPE; - } else if(type.equalsIgnoreCase(DataProperty.TEXT_TYPE)){ - type = DataProperty.TEXT_TYPE; - } else if (type.equalsIgnoreCase(DataProperty.GRACE_TYPE)){ - type = DataProperty.GRACE_TYPE; - } else { - type = DataProperty.OTHER_TYPE; - } - - data[i].getMetaData().put(DataProperty.TYPE, type); - } - } else { - Iterator iter = nameToFileMap.values().iterator(); - while (iter.hasNext()) { - File f = (File) iter.next(); - - data[i] = new BasicData(f, "file:text/plain"); - data[i].getMetaData().put(DataProperty.LABEL, f.getName()); - - i++; - } - break; - } - } - - return data; - } - - protected void chmod(String baseDir) { - //FIXME: Surely java has a way to do this!!!! - if (new File("/bin/chmod").exists()) { - try { - String executable = baseDir + props.getProperty("executable"); - Runtime.getRuntime().exec("/bin/chmod +x " + executable).waitFor(); - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - protected File[] execute(String[] cmdarray, String baseDir) throws Exception { - File dir = new File(baseDir); - String[] beforeFiles = dir.list(); - - final Process process = Runtime.getRuntime().exec(cmdarray, null, new File(baseDir)); - - process.getOutputStream().close(); - - this.processRunning = new Boolean(true); - - //start thread to consume stdout of process - - new Thread(new Runnable() { - public void run() { - logStream(LogService.LOG_INFO, process.getInputStream()); + guiBuilder = (GUIBuilderService) ciContext + .getService(GUIBuilderService.class.getName()); + tempDir = makeTempDirectory(); + } + + /** + * @see org.cishell.framework.algorithm.Algorithm#execute() + */ + public Data[] execute() { + try { + String algDir = tempDir + File.separator + + props.getProperty("Algorithm-Directory") + File.separator; + + chmod(algDir); + File[] output = execute(getTemplate(algDir), algDir); + + return toData(output); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + protected Data[] toData(File[] files) { + String outData = (String) props.get(AlgorithmProperty.OUT_DATA); + + // if out data is null then it returns no data + if (("" + outData).trim().equalsIgnoreCase(AlgorithmProperty.NULL_DATA)) { + return null; + } + + String[] formats = outData.split(","); + + Map nameToFileMap = new HashMap(); + for (int i = 0; i < files.length; i++) { + nameToFileMap.put(files[i].getName(), files[i]); + } + + Data[] data = null; + if (formats.length > files.length) { + data = new Data[formats.length]; + } else { + data = new Data[files.length]; + } + + for (int i = 0; i < data.length; i++) { + String file = props.getProperty("outFile[" + i + "]", null); + + if (i < formats.length) { + File f = (File) nameToFileMap.remove(file); + + if (f != null) { + data[i] = new BasicData(f, formats[i]); + + String label = props.getProperty( + "outFile[" + i + "].label", f.getName()); + data[i].getMetaData().put(DataProperty.LABEL, label); + + String type = props.getProperty("outFile[" + i + "].type", + DataProperty.OTHER_TYPE); + type = type.trim(); + if (type.equalsIgnoreCase(DataProperty.MATRIX_TYPE)) { + type = DataProperty.MATRIX_TYPE; + } else if (type.equalsIgnoreCase(DataProperty.NETWORK_TYPE)) { + type = DataProperty.NETWORK_TYPE; + } else if (type.equalsIgnoreCase(DataProperty.TREE_TYPE)) { + type = DataProperty.TREE_TYPE; + } else if (type.equalsIgnoreCase(DataProperty.TEXT_TYPE)) { + type = DataProperty.TEXT_TYPE; + } else if (type.equalsIgnoreCase(DataProperty.GRACE_TYPE)) { + type = DataProperty.GRACE_TYPE; + } else { + type = DataProperty.OTHER_TYPE; + } + + data[i].getMetaData().put(DataProperty.TYPE, type); + } + } else { + Iterator iter = nameToFileMap.values().iterator(); + while (iter.hasNext()) { + File f = (File) iter.next(); + + data[i] = new BasicData(f, "file:text/plain"); + data[i].getMetaData().put(DataProperty.LABEL, f.getName()); + + i++; + } + break; } - }).start(); - - //start thread to consume stderr of process - - new Thread(new Runnable() { - public void run() { - logStream(LogService.LOG_ERROR, process.getErrorStream()); + } + + return data; + } + + protected void chmod(String baseDir) { + // FIXME: Surely java has a way to do this!!!! + if (new File("/bin/chmod").exists()) { + try { + String executable = baseDir + props.getProperty("executable"); + Runtime.getRuntime().exec("/bin/chmod +x " + executable) + .waitFor(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); } - }).start(); - - //if we have a monitor... - if (this.monitor != null) { - this.monitor.start(ProgressMonitor.CANCELLABLE, -1); - - //start thread that checks if the user wants to cancel the process - - new Thread(new Runnable() { - public void run() { - checkForCancellation(process); - } - }).start(); - } + } + } - process.waitFor(); - - - this.processRunning = new Boolean(false); - - if (this.monitor != null) { - - this.monitor.done(); - } + protected File[] execute(String[] cmdarray, String baseDir) + throws Exception { + File dir = new File(baseDir); + String[] beforeFiles = dir.list(); - //if the process failed unexpectedly... - if (process.exitValue() != 0 && this.killedOnPurpose.booleanValue() != true) { - //display the error message using gui builder - guiBuilder.showError("Algorithm Could Not Finish Execution", "Sorry, the algorithm could not finish execution.", - "Please check the console window for the error log messages and report the bug.\n" - +"Thank you."); - } - - //get the files output from the process - - String[] afterFiles = dir.list(); - - Arrays.sort(beforeFiles); - Arrays.sort(afterFiles); - - List outputs = new ArrayList(); - - int beforeIndex = 0; - int afterIndex = 0; - - while (beforeIndex < beforeFiles.length && afterIndex < afterFiles.length) { - if (beforeFiles[beforeIndex].equals(afterFiles[afterIndex])) { - beforeIndex++; - afterIndex++; - } else { - outputs.add(new File(baseDir + afterFiles[afterIndex])); - afterIndex++; - } - } - - //get any remaining new files - while (afterIndex < afterFiles.length) { - outputs.add(new File(baseDir + afterFiles[afterIndex])); - afterIndex++; - } - - return (File[]) outputs.toArray(new File[]{}); - } - - protected void logStream(int logLevel, InputStream is) { - LogService log = (LogService) ciContext.getService(LogService.class.getName()); - - if (log == null) return; - - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + final Process process = Runtime.getRuntime().exec(cmdarray, null, + new File(baseDir)); + process.getOutputStream().close(); + monitor.start(ProgressMonitor.CANCELLABLE, -1); + + InputStream in = process.getInputStream(); + StringBuffer in_buffer = new StringBuffer(); + + InputStream err = process.getErrorStream(); + StringBuffer err_buffer = new StringBuffer(); - try { - String line = reader.readLine(); - while (line != null) { - log.log(logLevel, line); - line = reader.readLine(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - protected void checkForCancellation(Process process) { - while (StaticExecutableRunner.this.processRunning.booleanValue()) { - if (StaticExecutableRunner.this.monitor.isCanceled()) { - StaticExecutableRunner.this.killedOnPurpose = new Boolean(true); - process.destroy(); - } else { - } - - try { - Thread.sleep(100); - } catch (InterruptedException e) { - } - } - } + Integer exitValue = null; + boolean killedOnPurpose = false; + while (!killedOnPurpose && exitValue == null) { + in_buffer = logStream(LogService.LOG_INFO, in, in_buffer); + err_buffer = logStream(LogService.LOG_ERROR, err, err_buffer); - protected String[] getTemplate(String algDir) { - String template = "" + props.getProperty("template"); - String[] cmdarray = template.split("\\s"); - - for (int i=0; i < cmdarray.length; i++) { - cmdarray[i] = substiteVars(cmdarray[i]); - } - - //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"; - } - } - cmdarray[0] = algDir + cmdarray[0]; - - return cmdarray; - } - - protected String substiteVars(String str) { - str = str.replaceAll("\\$\\{executable\\}", props.getProperty("executable")); - - for (int i=0; i < data.length; i++) { - String file = ((File) data[i].getData()).getAbsolutePath(); - - if (File.separatorChar == '\\') { - file = file.replace(File.separatorChar, '/'); - } - - str = str.replaceAll("\\$\\{inFile\\["+i+"\\]\\}", file); - - if (File.separatorChar == '\\') { - str = str.replace('/',File.separatorChar); - } - } - - 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() { - return new File(tempDir); - } - - protected String makeTempDirectory() throws IOException { - File sessionDir = Activator.getTempDirectory(); - File dir = File.createTempFile("StaticExecutableRunner-", "", sessionDir); - - dir.delete(); - dir.mkdirs(); - - return dir.getAbsolutePath(); - } + if (monitor.isCanceled()) { + killedOnPurpose = true; + process.destroy(); + } + + try { + int value = process.exitValue(); + exitValue = new Integer(value); + } catch (IllegalThreadStateException e) { + // thrown if the process isn't done. + // kinda nasty, but there looks to be no other option. + } + + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // possibly normal operation + } + } + + monitor.done(); + + // if the process failed unexpectedly... + if (process.exitValue() != 0 && !killedOnPurpose) { + // display the error message using gui builder + guiBuilder.showError( + "Algorithm Could Not Finish Execution", + "Sorry, the algorithm could not finish execution.", + "Please check the console window for the error log messages and report the bug.\n" + + "Thank you."); + } + + // get the files output from the process + String[] afterFiles = dir.list(); + + Arrays.sort(beforeFiles); + Arrays.sort(afterFiles); + + List outputs = new ArrayList(); + + int beforeIndex = 0; + int afterIndex = 0; + + while (beforeIndex < beforeFiles.length + && afterIndex < afterFiles.length) { + if (beforeFiles[beforeIndex].equals(afterFiles[afterIndex])) { + beforeIndex++; + afterIndex++; + } else { + outputs.add(new File(baseDir + afterFiles[afterIndex])); + afterIndex++; + } + } + + // get any remaining new files + while (afterIndex < afterFiles.length) { + outputs.add(new File(baseDir + afterFiles[afterIndex])); + afterIndex++; + } + + return (File[]) outputs.toArray(new File[] {}); + } + + protected StringBuffer logStream(int logLevel, InputStream is, + StringBuffer buffer) { + try { + int available = is.available(); + if (available > 0) { + byte[] b = new byte[available]; + is.read(b); + buffer.append(new String(b)); + + buffer = log(logLevel, buffer); + } + } catch (EOFException e) { + //normal operation + } catch (IOException e) { + e.printStackTrace(); + } + + return buffer; + } + + 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()); + + int lastGoodIndex = 0; + int fromIndex = 0; + // print out each new line + while (fromIndex != -1 && fromIndex < buffer.length()) { + int toIndex = buffer.indexOf("\n", fromIndex); + + if (toIndex != -1) { + String message = buffer.substring(fromIndex, toIndex); + + if (log == null) { + // This will probably never come up, but if it does + // we'll still get some output. + System.out.println(message); + } else { + log.log(logLevel, message); + } + fromIndex = toIndex+1; + lastGoodIndex = toIndex+1; + } else { + fromIndex = -1; + } + } + // save the last part of the string that doesn't end in a newline + if (lastGoodIndex > 0) { + buffer = new StringBuffer(buffer.substring(lastGoodIndex)); + } + } + + return buffer; + } + + protected String[] getTemplate(String algDir) { + String template = "" + props.getProperty("template"); + String[] cmdarray = template.split("\\s"); + + for (int i = 0; i < cmdarray.length; i++) { + cmdarray[i] = substiteVars(cmdarray[i]); + } + + // 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"; + } + } + cmdarray[0] = algDir + cmdarray[0]; + + return cmdarray; + } + + protected String substiteVars(String str) { + str = str.replaceAll("\\$\\{executable\\}", props + .getProperty("executable")); + + for (int i = 0; i < data.length; i++) { + String file = ((File) data[i].getData()).getAbsolutePath(); + + if (File.separatorChar == '\\') { + file = file.replace(File.separatorChar, '/'); + } + + str = str.replaceAll("\\$\\{inFile\\[" + i + "\\]\\}", file); + + if (File.separatorChar == '\\') { + str = str.replace('/', File.separatorChar); + } + } + + 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() { + return new File(tempDir); + } + + protected String makeTempDirectory() throws IOException { + File sessionDir = Activator.getTempDirectory(); + File dir = File.createTempFile("StaticExecutableRunner-", "", + sessionDir); + + dir.delete(); + dir.mkdirs(); + + return dir.getAbsolutePath(); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |