Search in sources :

Example 11 with File

use of java.io.File in project camel by apache.

the class DefaultExecCommandExecutor method prepareDefaultExecutor.

protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
    DefaultExecutor executor = new ExecDefaultExecutor();
    executor.setExitValues(null);
    if (execCommand.getWorkingDir() != null) {
        executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile());
    }
    if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) {
        executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout()));
    }
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    return executor;
}
Also used : ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) File(java.io.File) ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 12 with File

use of java.io.File in project camel by apache.

the class ExecJava8IssueTest method test.

@Test
public void test() throws Exception {
    if (!OS.isFamilyUnix()) {
        System.err.println("The test 'CamelExecTest' does not support the following OS : " + System.getProperty("os.name"));
        return;
    }
    String tempFilePath = tempDir.getAbsolutePath() + "/" + tempFileName;
    final File script = File.createTempFile("script", ".sh", tempDir);
    writeScript(script);
    final String exec = "bash?args=" + script.getAbsolutePath() + " " + tempFilePath + "&outFile=" + tempFilePath;
    DefaultCamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:source").to("file:" + tempDir.getAbsolutePath() + "?fileName=" + tempFileName).to("exec:" + exec).process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    String output = exchange.getIn().getBody(String.class);
                    assertEquals("hello world\n", output);
                }
            });
        }
    });
    context.start();
    ProducerTemplate pt = context.createProducerTemplate();
    String payload = "hello";
    pt.sendBody("direct:source", payload);
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) File(java.io.File) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with File

use of java.io.File in project camel by apache.

the class DefaultExecBinding method readInput.

@SuppressWarnings("unchecked")
public ExecCommand readInput(Exchange exchange, ExecEndpoint endpoint) {
    ObjectHelper.notNull(exchange, "exchange");
    ObjectHelper.notNull(endpoint, "endpoint");
    // do not convert args as we do that manually later
    Object args = exchange.getIn().removeHeader(EXEC_COMMAND_ARGS);
    String cmd = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_EXECUTABLE, endpoint.getExecutable(), String.class);
    String dir = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_WORKING_DIR, endpoint.getWorkingDir(), String.class);
    long timeout = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_TIMEOUT, endpoint.getTimeout(), Long.class);
    String outFilePath = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_OUT_FILE, endpoint.getOutFile(), String.class);
    boolean useStderrOnEmptyStdout = getAndRemoveHeader(exchange.getIn(), EXEC_USE_STDERR_ON_EMPTY_STDOUT, endpoint.isUseStderrOnEmptyStdout(), Boolean.class);
    InputStream input = exchange.getIn().getBody(InputStream.class);
    // If the args is a list of strings already..
    List<String> argsList = null;
    if (isListOfStrings(args)) {
        argsList = (List<String>) args;
    }
    if (argsList == null) {
        // no we could not do that, then parse it as a string to a list
        String s = endpoint.getArgs();
        if (args != null) {
            // use args from header instead from endpoint
            s = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, args);
        }
        LOG.debug("Parsing argument String to a List: {}", s);
        argsList = splitToWhiteSpaceSeparatedTokens(s);
    }
    File outFile = outFilePath == null ? null : new File(outFilePath);
    return new ExecCommand(cmd, argsList, dir, timeout, input, outFile, useStderrOnEmptyStdout);
}
Also used : InputStream(java.io.InputStream) ExecCommand(org.apache.camel.component.exec.ExecCommand) File(java.io.File)

Example 14 with File

use of java.io.File in project camel by apache.

the class ExecDocumentationExamplesTest method testExecWinAnt.

/**
     * The test assumes that Apache ant is installed
     */
@Test
@Ignore
public void testExecWinAnt() throws Exception {
    File f = new File(ANT_BUILD_FILE_NAME);
    f.createNewFile();
    FileUtils.writeStringToFile(f, ANT_BUILD_FILE_CONTENT);
    assertTrue("You must create a sample build file!", f.exists());
    ExecResult body = templateExecAnt.requestBody((Object) "test", ExecResult.class);
    String stdout = IOUtils.toString(body.getStdout());
    assertNull(body.getStderr());
    assertTrue("The ant script should print" + TEST_MSG, stdout.contains(TEST_MSG));
    f.delete();
}
Also used : File(java.io.File) ExecResult(org.apache.camel.component.exec.ExecResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with File

use of java.io.File in project camel by apache.

the class FlatpackDelimitedDataFormatTest method testUnmarshal.

@Test
public void testUnmarshal() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:unmarshal");
    // by default we get on big message
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(DataSetList.class);
    String data = IOConverter.toString(new File("src/test/data/delim/INVENTORY-CommaDelimitedWithQualifier.txt"), null);
    template.sendBody("direct:unmarshal", data);
    assertMockEndpointsSatisfied();
    DataSetList list = mock.getExchanges().get(0).getIn().getBody(DataSetList.class);
    assertEquals(4, list.size());
    Map<?, ?> row = list.get(0);
    assertEquals("SOME VALVE", row.get("ITEM_DESC"));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)45560 IOException (java.io.IOException)9550 Test (org.junit.Test)9009 FileOutputStream (java.io.FileOutputStream)3388 ArrayList (java.util.ArrayList)3273 FileInputStream (java.io.FileInputStream)2915 InputStream (java.io.InputStream)1783 FileNotFoundException (java.io.FileNotFoundException)1743 URL (java.net.URL)1649 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1478 Test (org.testng.annotations.Test)1411 HashMap (java.util.HashMap)1404 RandomAccessFile (java.io.RandomAccessFile)1154 FileWriter (java.io.FileWriter)1081 Properties (java.util.Properties)1019 ZipFile (java.util.zip.ZipFile)959 JarFile (java.util.jar.JarFile)866 List (java.util.List)850 BufferedReader (java.io.BufferedReader)840 OutputStream (java.io.OutputStream)811