package org.argeo.slc.core.execution.tasks;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.logging.LogFactory;
import org.argeo.slc.SlcException;
import org.argeo.slc.UnsupportedException;
+import org.argeo.slc.core.execution.ExecutionResources;
import org.argeo.slc.core.structure.tree.TreeSRelatedHelper;
import org.argeo.slc.core.test.SimpleResultPart;
import org.argeo.slc.test.TestResult;
private Resource stdOutFile = null;
private Resource stdErrFile = null;
private Resource stdInFile = null;
+ private Boolean redirectStdOut = false;
private Map<String, List<Object>> osCommands = new HashMap<String, List<Object>>();
private Map<String, String> osCmds = new HashMap<String, String>();
private TestResult testResult;
+ private ExecutionResources executionResources;
+
/** Empty constructor */
public SystemCall() {
/** Executes the system call. */
public void run() {
// Manage streams
- final Writer stdOutWriter;
- final Writer stdErrWriter;
- final InputStream stdInStream;
- if (stdOutFile != null) {
- stdOutWriter = createWriter(stdOutFile);
- } else {
- stdOutWriter = null;
- }
+ Writer stdOutWriter = null;
+ OutputStream stdOutputStream = null;
+ Writer stdErrWriter = null;
+ InputStream stdInStream = null;
+ if (stdOutFile != null)
+ if (redirectStdOut)
+ stdOutputStream = createOutputStream(stdOutFile);
+ else
+ stdOutWriter = createWriter(stdOutFile, true);
if (stdErrFile != null) {
- stdErrWriter = createWriter(stdErrFile);
+ stdErrWriter = createWriter(stdErrFile, true);
} else {
- if (stdOutFile != null) {
- stdErrWriter = createWriter(stdOutFile);
- } else {
- stdErrWriter = null;
- }
+ if (stdOutFile != null && !redirectStdOut)
+ stdErrWriter = createWriter(stdOutFile, true);
}
if (stdInFile != null)
throw new SlcException("Cannot open a stream for " + stdInFile,
e2);
}
- else
- stdInStream = null;
if (log.isTraceEnabled()) {
log.debug("os.name=" + System.getProperty("os.name"));
if (redirectStreams) {
// Redirect standard streams
executor.setStreamHandler(createExecuteStreamHandler(stdOutWriter,
- stdErrWriter, stdInStream));
+ stdOutputStream, stdErrWriter, stdInStream));
} else {
// Dummy stream handler (otherwise pump is used)
executor.setStreamHandler(new DummyexecuteStreamHandler());
* logging mechanism.
*/
protected ExecuteStreamHandler createExecuteStreamHandler(
- final Writer stdOutWriter, final Writer stdErrWriter,
- final InputStream stdInStream) {
+ final Writer stdOutWriter, final OutputStream stdOutputStream,
+ final Writer stdErrWriter, final InputStream stdInStream) {
// Log writers
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(
- new LogOutputStream() {
- protected void processLine(String line, int level) {
- log(stdOutLogLevel, line);
- if (stdOutWriter != null)
- appendLineToFile(stdOutWriter, line);
- }
- }, new LogOutputStream() {
+ stdOutputStream != null ? stdOutputStream
+ : new LogOutputStream() {
+ protected void processLine(String line, int level) {
+ log(stdOutLogLevel, line);
+ if (stdOutWriter != null)
+ appendLineToFile(stdOutWriter, line);
+ }
+ }, new LogOutputStream() {
protected void processLine(String line, int level) {
log(stdErrLogLevel, line);
if (stdErrWriter != null)
}
}
- /** Creates the writer for the log files. */
- protected Writer createWriter(Resource target) {
+ /** Creates the writer for the output/err files. */
+ protected Writer createWriter(Resource target, Boolean append) {
FileWriter writer = null;
try {
- File file = target.getFile();
- writer = new FileWriter(file, true);
+
+ final File file;
+ if (executionResources != null)
+ file = new File(executionResources.getAsOsPath(target, true));
+ else
+ file = target.getFile();
+ writer = new FileWriter(file, append);
} catch (IOException e) {
- log.error("Cannot create log file " + target, e);
+ log.error("Cannot get file for " + target, e);
IOUtils.closeQuietly(writer);
}
return writer;
}
+ /** Creates an outputstream for the output/err files. */
+ protected OutputStream createOutputStream(Resource target) {
+ FileOutputStream OutputStream = null;
+ try {
+
+ final File file;
+ if (executionResources != null)
+ file = new File(executionResources.getAsOsPath(target, true));
+ else
+ file = target.getFile();
+ OutputStream = new FileOutputStream(file, false);
+ } catch (IOException e) {
+ log.error("Cannot get file for " + target, e);
+ IOUtils.closeQuietly(OutputStream);
+ }
+ return OutputStream;
+ }
+
/** Append the argument (for chaining) */
public SystemCall arg(String arg) {
command.add(arg);
this.generateScript = generateScript;
}
+ public void setExecutionResources(ExecutionResources executionResources) {
+ this.executionResources = executionResources;
+ }
+
+ public void setRedirectStdOut(Boolean redirectStdOut) {
+ this.redirectStdOut = redirectStdOut;
+ }
+
private class DummyexecuteStreamHandler implements ExecuteStreamHandler {
public void setProcessErrorStream(InputStream is) throws IOException {
--- /dev/null
+package org.argeo.slc.osgi;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.core.execution.FileExecutionResources;
+import org.osgi.framework.BundleContext;
+import org.springframework.core.io.Resource;
+import org.springframework.osgi.context.BundleContextAware;
+import org.springframework.osgi.io.OsgiBundleResource;
+
+public class OsgiExecutionResources extends FileExecutionResources implements
+ BundleContextAware {
+ private final static Log log = LogFactory
+ .getLog(OsgiExecutionResources.class);
+
+ private BundleContext bundleContext;
+
+ @Override
+ protected File fileFromResource(Resource resource) {
+ File file = super.fileFromResource(resource);
+ if (file != null)
+ return file;
+
+ if (!(resource instanceof OsgiBundleResource))
+ return null;
+
+ OsgiBundleResource osgiBundleResource = (OsgiBundleResource) resource;
+ try {
+ return osgiBundleResource.getFile();
+ } catch (IOException e) {
+ if (log.isTraceEnabled())
+ log.trace("Resource " + resource
+ + " is not available on the file system: " + e);
+ }
+
+ // TODO: ability to access resources in other bundles
+ String location = bundleContext.getBundle().getLocation();
+ String base = null;
+ if (location.startsWith("reference:file:"))
+ base = location.substring("reference:file:".length());
+ else if (location.startsWith("initial@reference:file:")) {
+ // TODO: Equinox specific?
+ String relPath = location.substring("initial@reference:file:"
+ .length());
+ if (relPath.startsWith("../"))// relative to the framework jar
+ relPath = relPath.substring("../".length());
+ String framework = System.getProperty("osgi.framework").substring(
+ "file:".length());
+ int sepIndex = framework.lastIndexOf(File.separatorChar);
+ framework = framework.substring(0, sepIndex);
+ base = framework + '/' + relPath;
+ } else {
+ return null;
+ }
+
+ String path = base + '/' + osgiBundleResource.getPathWithinContext();
+ try {
+ file = new File(path).getCanonicalFile();
+ } catch (IOException e) {
+ throw new SlcException("Cannot determine canonical path for "
+ + path, e);
+ }
+ if (log.isDebugEnabled())
+ log.debug("OSGi local resource: " + file + " from " + resource);
+ return file;
+ }
+
+ public void setBundleContext(BundleContext bundleContext) {
+ this.bundleContext = bundleContext;
+ }
+
+}