From e14be27fcc4e43cc597ddb79bce2ce7aab8e0288 Mon Sep 17 00:00:00 2001 From: ypatil Date: Thu, 4 Feb 2016 14:10:46 +0530 Subject: [PATCH 1/3] Spring with Apache Camel for file processing --- spring-apache-camel/data/file1.txt | 1 + .../data/input/.camel/file1.txt | 1 + .../data/outputLowerCase/file1.txt | 1 + .../data/outputUppperCase/file1.txt | 1 + spring-apache-camel/pom.xml | 43 +++++++++++++++++++ .../main/java/org/apache/camel/main/App.java | 12 ++++++ .../apache/camel/processor/FileProcessor.java | 19 ++++++++ .../src/main/resources/camel-context.xml | 41 ++++++++++++++++++ 8 files changed, 119 insertions(+) create mode 100644 spring-apache-camel/data/file1.txt create mode 100644 spring-apache-camel/data/input/.camel/file1.txt create mode 100644 spring-apache-camel/data/outputLowerCase/file1.txt create mode 100644 spring-apache-camel/data/outputUppperCase/file1.txt create mode 100644 spring-apache-camel/pom.xml create mode 100644 spring-apache-camel/src/main/java/org/apache/camel/main/App.java create mode 100644 spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java create mode 100644 spring-apache-camel/src/main/resources/camel-context.xml diff --git a/spring-apache-camel/data/file1.txt b/spring-apache-camel/data/file1.txt new file mode 100644 index 0000000000..c8436b654b --- /dev/null +++ b/spring-apache-camel/data/file1.txt @@ -0,0 +1 @@ +THIS IS UPPERCASE CONTENT. this is lowecase content. \ No newline at end of file diff --git a/spring-apache-camel/data/input/.camel/file1.txt b/spring-apache-camel/data/input/.camel/file1.txt new file mode 100644 index 0000000000..c8436b654b --- /dev/null +++ b/spring-apache-camel/data/input/.camel/file1.txt @@ -0,0 +1 @@ +THIS IS UPPERCASE CONTENT. this is lowecase content. \ No newline at end of file diff --git a/spring-apache-camel/data/outputLowerCase/file1.txt b/spring-apache-camel/data/outputLowerCase/file1.txt new file mode 100644 index 0000000000..06a9866342 --- /dev/null +++ b/spring-apache-camel/data/outputLowerCase/file1.txt @@ -0,0 +1 @@ +this is uppercase content. this is lowecase content. \ No newline at end of file diff --git a/spring-apache-camel/data/outputUppperCase/file1.txt b/spring-apache-camel/data/outputUppperCase/file1.txt new file mode 100644 index 0000000000..ce07e477bc --- /dev/null +++ b/spring-apache-camel/data/outputUppperCase/file1.txt @@ -0,0 +1 @@ +THIS IS UPPERCASE CONTENT. THIS IS LOWECASE CONTENT. \ No newline at end of file diff --git a/spring-apache-camel/pom.xml b/spring-apache-camel/pom.xml new file mode 100644 index 0000000000..5b143fedb4 --- /dev/null +++ b/spring-apache-camel/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + org.apache.camel + spring-apache-camel + jar + 1.0-SNAPSHOT + spring-apache-camel + http://maven.apache.org + + + 2.16.1 + 4.2.4.RELEASE + + + + + + org.apache.camel + camel-core + ${env.camel.version} + + + + org.apache.camel + camel-spring + ${env.camel.version} + + + + org.apache.camel + camel-stream + ${env.camel.version} + + + + org.springframework + spring-context + ${env.spring.version} + + + + diff --git a/spring-apache-camel/src/main/java/org/apache/camel/main/App.java b/spring-apache-camel/src/main/java/org/apache/camel/main/App.java new file mode 100644 index 0000000000..4d0793903e --- /dev/null +++ b/spring-apache-camel/src/main/java/org/apache/camel/main/App.java @@ -0,0 +1,12 @@ +package org.apache.camel.main; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(final String[] args) throws Exception { + //Load application context + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml"); + Thread.sleep(5000); + applicationContext.close(); + } +} \ No newline at end of file diff --git a/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java b/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java new file mode 100644 index 0000000000..961f877806 --- /dev/null +++ b/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java @@ -0,0 +1,19 @@ +package org.apache.camel.processor; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; + +public class FileProcessor implements Processor { + + public void process(Exchange exchange) throws Exception { + //Read file content + String originalFileContent = (String)exchange.getIn().getBody(String.class); + + //Convert file content to upper case. + String upperCaseFileContent = originalFileContent.toUpperCase(); + + //Update file content. + exchange.getIn().setBody(upperCaseFileContent); + } + +} diff --git a/spring-apache-camel/src/main/resources/camel-context.xml b/spring-apache-camel/src/main/resources/camel-context.xml new file mode 100644 index 0000000000..d304ceb857 --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + ${body.toLowerCase()} + + + + + + + + .......... File content conversion completed .......... + + + + + + + + + \ No newline at end of file From c385d23cd53543d6bb0a0f6f7ec8c97a8679c31d Mon Sep 17 00:00:00 2001 From: ypatil Date: Thu, 4 Feb 2016 14:13:39 +0530 Subject: [PATCH 2/3] Readme file added --- spring-apache-camel/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 spring-apache-camel/README.md diff --git a/spring-apache-camel/README.md b/spring-apache-camel/README.md new file mode 100644 index 0000000000..28b95bdf89 --- /dev/null +++ b/spring-apache-camel/README.md @@ -0,0 +1,28 @@ + +

Configure and Use Apache Camel with Spring

+ +This article will demonstrate how to configure and use Apache Camel with Spring Framework. + +

Relevant Article

+ + + +

Framework Versions:

+ + + +

Build and Run Application

+ +To build this application execute following maven command in ApacheCamelFileProcessor directory. + +mvn clean install + +To run this application you can either run our main class org.apache.camel.main.App from your IDE or you can execute following maven command: + +mvn exec:java -Dexec.mainClass="org.apache.camel.main.App" \ No newline at end of file From aedffc1d139ce5c54fec86d1e0d411ee09a8c590 Mon Sep 17 00:00:00 2001 From: ypatil Date: Mon, 15 Feb 2016 15:29:13 +0530 Subject: [PATCH 3/3] data folder moved under test/data. Test cases added. Code & POM updated. --- spring-apache-camel/data/file1.txt | 1 - .../data/input/.camel/file1.txt | 1 - .../data/outputLowerCase/file1.txt | 1 - .../data/outputUppperCase/file1.txt | 1 - spring-apache-camel/pom.xml | 26 +++- .../main/java/org/apache/camel/main/App.java | 4 +- .../apache/camel/processor/FileProcessor.java | 7 +- .../src/main/resources/camel-context.xml | 35 +++--- .../src/test/data/sampleInputFile/file.txt | 1 + .../java/org/apache/camel/main/AppTest.java | 117 ++++++++++++++++++ 10 files changed, 163 insertions(+), 31 deletions(-) delete mode 100644 spring-apache-camel/data/file1.txt delete mode 100644 spring-apache-camel/data/input/.camel/file1.txt delete mode 100644 spring-apache-camel/data/outputLowerCase/file1.txt delete mode 100644 spring-apache-camel/data/outputUppperCase/file1.txt create mode 100644 spring-apache-camel/src/test/data/sampleInputFile/file.txt create mode 100644 spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java diff --git a/spring-apache-camel/data/file1.txt b/spring-apache-camel/data/file1.txt deleted file mode 100644 index c8436b654b..0000000000 --- a/spring-apache-camel/data/file1.txt +++ /dev/null @@ -1 +0,0 @@ -THIS IS UPPERCASE CONTENT. this is lowecase content. \ No newline at end of file diff --git a/spring-apache-camel/data/input/.camel/file1.txt b/spring-apache-camel/data/input/.camel/file1.txt deleted file mode 100644 index c8436b654b..0000000000 --- a/spring-apache-camel/data/input/.camel/file1.txt +++ /dev/null @@ -1 +0,0 @@ -THIS IS UPPERCASE CONTENT. this is lowecase content. \ No newline at end of file diff --git a/spring-apache-camel/data/outputLowerCase/file1.txt b/spring-apache-camel/data/outputLowerCase/file1.txt deleted file mode 100644 index 06a9866342..0000000000 --- a/spring-apache-camel/data/outputLowerCase/file1.txt +++ /dev/null @@ -1 +0,0 @@ -this is uppercase content. this is lowecase content. \ No newline at end of file diff --git a/spring-apache-camel/data/outputUppperCase/file1.txt b/spring-apache-camel/data/outputUppperCase/file1.txt deleted file mode 100644 index ce07e477bc..0000000000 --- a/spring-apache-camel/data/outputUppperCase/file1.txt +++ /dev/null @@ -1 +0,0 @@ -THIS IS UPPERCASE CONTENT. THIS IS LOWECASE CONTENT. \ No newline at end of file diff --git a/spring-apache-camel/pom.xml b/spring-apache-camel/pom.xml index 5b143fedb4..fbea9b779d 100644 --- a/spring-apache-camel/pom.xml +++ b/spring-apache-camel/pom.xml @@ -11,10 +11,19 @@ 2.16.1 4.2.4.RELEASE + 1.7 + 4.1 - + + + junit + junit + ${junit.version} + test + + org.apache.camel camel-core @@ -40,4 +49,19 @@ + + + + + maven-compiler-plugin + + true + true + ${java.version} + ${java.version} + ${java.version} + + + + diff --git a/spring-apache-camel/src/main/java/org/apache/camel/main/App.java b/spring-apache-camel/src/main/java/org/apache/camel/main/App.java index 4d0793903e..8674caefca 100644 --- a/spring-apache-camel/src/main/java/org/apache/camel/main/App.java +++ b/spring-apache-camel/src/main/java/org/apache/camel/main/App.java @@ -4,9 +4,9 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(final String[] args) throws Exception { - //Load application context ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml"); - Thread.sleep(5000); + // Keep main thread alive for some time to let application finish processing the input files. + Thread.sleep(5000); applicationContext.close(); } } \ No newline at end of file diff --git a/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java b/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java index 961f877806..a98c77feb8 100644 --- a/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java +++ b/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java @@ -6,13 +6,8 @@ import org.apache.camel.Processor; public class FileProcessor implements Processor { public void process(Exchange exchange) throws Exception { - //Read file content - String originalFileContent = (String)exchange.getIn().getBody(String.class); - - //Convert file content to upper case. + String originalFileContent = exchange.getIn().getBody(String.class); String upperCaseFileContent = originalFileContent.toUpperCase(); - - //Update file content. exchange.getIn().setBody(upperCaseFileContent); } diff --git a/spring-apache-camel/src/main/resources/camel-context.xml b/spring-apache-camel/src/main/resources/camel-context.xml index d304ceb857..75e2a61711 100644 --- a/spring-apache-camel/src/main/resources/camel-context.xml +++ b/spring-apache-camel/src/main/resources/camel-context.xml @@ -1,40 +1,39 @@ + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> - - - + + + - - + + - + - - + + ${body.toLowerCase()} - + - - + + - .......... File content conversion completed .......... + .......... File content conversion completed .......... + - + - + diff --git a/spring-apache-camel/src/test/data/sampleInputFile/file.txt b/spring-apache-camel/src/test/data/sampleInputFile/file.txt new file mode 100644 index 0000000000..c1df3791c9 --- /dev/null +++ b/spring-apache-camel/src/test/data/sampleInputFile/file.txt @@ -0,0 +1 @@ +THIS IS UPPERCASE CONTENT. this is lowercase content. \ No newline at end of file diff --git a/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java b/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java new file mode 100644 index 0000000000..75a161d8cc --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java @@ -0,0 +1,117 @@ +package org.apache.camel.main; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Paths; + +import junit.framework.TestCase; + +import org.apache.camel.util.FileUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class AppTest extends TestCase { + + private static final String FILE_NAME = "file.txt"; + private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/"; + private static final String TEST_INPUT_DIR = "src/test/data/input/"; + private static final String UPPERCARE_OUTPUT_DIR = "src/test/data/outputUpperCase/"; + private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/"; + + @Before + public void setUp() throws Exception { + // Prepare input file for test + copySampleFileToInputDirectory(); + } + + @After + public void tearDown() throws Exception { + System.out.println("Deleting the test input and output files..."); + deleteFile(TEST_INPUT_DIR); + deleteFile(LOWERCASE_OUTPUT_DIR); + deleteFile(UPPERCARE_OUTPUT_DIR); + } + + @Test + public final void testMain() throws Exception { + App.main(null); + + String inputFileContent = readFileContent(SAMPLE_INPUT_DIR+FILE_NAME); + String outputUpperCase = readFileContent(UPPERCARE_OUTPUT_DIR+FILE_NAME); + String outputLowerCase = readFileContent(LOWERCASE_OUTPUT_DIR+FILE_NAME); + + System.out.println("Input File content = ["+inputFileContent+"]"); + System.out.println("UpperCaseOutput file content = ["+outputUpperCase+"]"); + System.out.println("LowerCaseOtput file content = ["+outputLowerCase+"]"); + + assertEquals(inputFileContent.toUpperCase(), outputUpperCase); + assertEquals(inputFileContent.toLowerCase(), outputLowerCase); + } + + private String readFileContent(String path) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded); + } + + private void deleteFile(String path) { + try { + FileUtil.removeDir(new File(path)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Copy sample input file to input directory. + */ + private void copySampleFileToInputDirectory() { + File sourceFile = new File(SAMPLE_INPUT_DIR + FILE_NAME); + File destFile = new File(TEST_INPUT_DIR + FILE_NAME); + + if (!sourceFile.exists()) { + System.out.println("Sample input file not found at location = [" + SAMPLE_INPUT_DIR + FILE_NAME + "]. Please provide this file."); + } + + if (!destFile.exists()) { + try { + System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]"); + + File destDir = new File(TEST_INPUT_DIR); + if(!destDir.exists()) { + destDir.mkdir(); + } + destFile.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + FileChannel source = null; + FileChannel destination = null; + + try { + source = new FileInputStream(sourceFile).getChannel(); + destination = new FileOutputStream(destFile).getChannel(); + if (destination != null && source != null) { + destination.transferFrom(source, 0, source.size()); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + source.close(); + } catch (Exception e) { + e.printStackTrace(); + } + try { + destination.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +}