You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@batchee.apache.org by rm...@apache.org on 2013/12/15 19:15:06 UTC
git commit: BATCHEE-7 adding modelmapper module
Updated Branches:
refs/heads/master 21cab1b59 -> 8367b00c3
BATCHEE-7 adding modelmapper module
Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/8367b00c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/8367b00c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/8367b00c
Branch: refs/heads/master
Commit: 8367b00c3a5ff51e9db996f9559a101e754b1126
Parents: 21cab1b
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Sun Dec 15 19:23:42 2013 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Sun Dec 15 19:23:42 2013 +0100
----------------------------------------------------------------------
extensions/modelmapper/pom.xml | 44 +++++
.../modelmapper/ModelMapperItemProcessor.java | 77 +++++++++
.../src/main/resources/META-INF/batchee.xml | 20 +++
.../modelmapper/ModelMapperProcessorTest.java | 162 +++++++++++++++++++
.../batch-jobs/modelmapper-processor.xml | 28 ++++
extensions/pom.xml | 1 +
src/site/markdown/extensions.md | 30 +++-
src/site/markdown/modules.md | 14 ++
8 files changed, 374 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/extensions/modelmapper/pom.xml
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/pom.xml b/extensions/modelmapper/pom.xml
new file mode 100644
index 0000000..6dd977c
--- /dev/null
+++ b/extensions/modelmapper/pom.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>batchee-extensions</artifactId>
+ <groupId>org.apache.batchee</groupId>
+ <version>0.1-incubating-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>batchee-modelmapper</artifactId>
+ <name>BatchEE :: Extensions :: ModelMapper</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.batchee</groupId>
+ <artifactId>batchee-extras</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.modelmapper</groupId>
+ <artifactId>modelmapper</artifactId>
+ <version>0.6.2</version>
+ </dependency>
+ </dependencies>
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java b/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java
new file mode 100644
index 0000000..1d1d9bb
--- /dev/null
+++ b/extensions/modelmapper/src/main/java/org/apache/batchee/modelmapper/ModelMapperItemProcessor.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.modelmapper;
+
+import org.modelmapper.ModelMapper;
+
+import javax.batch.api.BatchProperty;
+import javax.batch.api.chunk.ItemProcessor;
+import javax.inject.Inject;
+
+public class ModelMapperItemProcessor implements ItemProcessor {
+ private volatile ModelMapper mapper = null;
+
+ @Inject
+ @BatchProperty
+ private String destinationType;
+ private volatile Class<?> destinationTypeClass = null;
+
+ @Override
+ public Object processItem(final Object o) throws Exception {
+ loadClass();
+ return ensureMapper().map(o, destinationTypeClass);
+ }
+
+ protected ModelMapper newMapper() {
+ return new ModelMapper();
+ }
+
+ private void loadClass() {
+ if (destinationTypeClass == null) {
+ if (destinationType == null) {
+ throw new IllegalArgumentException("Please set destinationType");
+ }
+ synchronized (this) {
+ if (destinationTypeClass == null) {
+ try {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null) {
+ loader = ModelMapperItemProcessor.class.getClassLoader();
+ }
+
+ destinationTypeClass = loader.loadClass(destinationType);
+ } catch (final ClassNotFoundException e) {
+ throw new IllegalArgumentException("Can't load: '" + destinationType + "'", e);
+ } catch (final NoClassDefFoundError e) {
+ throw new IllegalArgumentException("Can't load: '" + destinationType + "'", e);
+ }
+ }
+ }
+ }
+ }
+
+ private ModelMapper ensureMapper() {
+ if (mapper == null) {
+ synchronized (this) {
+ if (mapper == null) {
+ mapper = newMapper();
+ }
+ }
+ }
+ return mapper;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/extensions/modelmapper/src/main/resources/META-INF/batchee.xml
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/src/main/resources/META-INF/batchee.xml b/extensions/modelmapper/src/main/resources/META-INF/batchee.xml
new file mode 100644
index 0000000..cbf3bbf
--- /dev/null
+++ b/extensions/modelmapper/src/main/resources/META-INF/batchee.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<batch-artifacts xmlns="http://xmlns.jcp.org/xml/ns/javaee">
+ <ref id="modelMapperProcessor" class="org.apache.batchee.modelmapper.ModelMapperItemProcessor" />
+</batch-artifacts>
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/ModelMapperProcessorTest.java
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/ModelMapperProcessorTest.java b/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/ModelMapperProcessorTest.java
new file mode 100644
index 0000000..d97d4f2
--- /dev/null
+++ b/extensions/modelmapper/src/test/java/org/apache/batchee/modelmapper/ModelMapperProcessorTest.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.batchee.modelmapper;
+
+import org.apache.batchee.util.Batches;
+import org.testng.annotations.Test;
+
+import javax.batch.api.chunk.ItemReader;
+import javax.batch.api.chunk.ItemWriter;
+import javax.batch.operations.JobOperator;
+import javax.batch.runtime.BatchRuntime;
+import java.io.Serializable;
+import java.util.List;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+public class ModelMapperProcessorTest {
+ @Test
+ public void map() {
+ final JobOperator operator = BatchRuntime.getJobOperator();
+ Batches.waitForEnd(operator, operator.start("modelmapper-processor", null));
+ assertNotNull(Writer.items);
+ assertEquals(2, Writer.items.size());
+ assertTrue(Writer.items.contains(new B("1", "#1")));
+ assertTrue(Writer.items.contains(new B("2", "#2")));
+ }
+
+ public static class Reader implements ItemReader {
+ private int count = 0;
+
+ @Override
+ public void open(final Serializable serializable) throws Exception {
+ // no-op
+ }
+
+ @Override
+ public void close() throws Exception {
+ // no-op
+ }
+
+ @Override
+ public Object readItem() throws Exception {
+ if (count++ < 2) {
+ return new A("" + count, "#" + count);
+ }
+ return null;
+ }
+
+ @Override
+ public Serializable checkpointInfo() throws Exception {
+ return null;
+ }
+ }
+
+ public static class Writer implements ItemWriter {
+ private static List<B> items;
+
+ @Override
+ public void open(final Serializable serializable) throws Exception {
+ // no-op
+ }
+
+ @Override
+ public void close() throws Exception {
+ // no-op
+ }
+
+ @Override
+ public void writeItems(final List objects) throws Exception {
+ items = objects;
+ }
+
+ @Override
+ public Serializable checkpointInfo() throws Exception {
+ return null;
+ }
+ }
+
+ public static class A {
+ private String a;
+ private String b;
+
+ public A(final String a, final String b) {
+ this.a = a;
+ this.b = b;
+ }
+
+ public String getA() {
+ return a;
+ }
+
+ public String getB() {
+ return b;
+ }
+ }
+
+ public static class B {
+ private String a;
+ private String b;
+
+ public B(final String s, final String s1) {
+ this.a = s;
+ this.b = s1;
+ }
+
+ public B() {
+ // no-op
+ }
+
+ public void setA(final String a) {
+ this.a = a;
+ }
+
+ public void setB(final String b) {
+ this.b = b;
+ }
+
+ public String getA() {
+ return a;
+ }
+
+ public String getB() {
+ return b;
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ final B b1 = (B) o;
+ return a.equals(b1.a) && b.equals(b1.b);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = a.hashCode();
+ result = 31 * result + b.hashCode();
+ return result;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor.xml
----------------------------------------------------------------------
diff --git a/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor.xml b/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor.xml
new file mode 100644
index 0000000..44aba6e
--- /dev/null
+++ b/extensions/modelmapper/src/test/resources/META-INF/batch-jobs/modelmapper-processor.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ See the NOTICE file distributed with this work for additional information
+ regarding copyright ownership. Licensed under the Apache License,
+ Version 2.0 (the "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<job id="modelmapper-processor" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
+ <step id="step1">
+ <chunk>
+ <reader ref="org.apache.batchee.modelmapper.ModelMapperProcessorTest$Reader" />
+ <processor ref="modelMapperProcessor">
+ <properties>
+ <property name="destinationType" value="org.apache.batchee.modelmapper.ModelMapperProcessorTest$B" />
+ </properties>
+ </processor>
+ <writer ref="org.apache.batchee.modelmapper.ModelMapperProcessorTest$Writer" />
+ </chunk>
+ </step>
+</job>
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/extensions/pom.xml
----------------------------------------------------------------------
diff --git a/extensions/pom.xml b/extensions/pom.xml
index fdb1c9f..e1d4d18 100644
--- a/extensions/pom.xml
+++ b/extensions/pom.xml
@@ -40,6 +40,7 @@
<module>jackson</module>
<module>cdi</module>
<module>hazelcast</module>
+ <module>modelmapper</module>
</modules>
<dependencies>
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/src/site/markdown/extensions.md
----------------------------------------------------------------------
diff --git a/src/site/markdown/extensions.md b/src/site/markdown/extensions.md
index 0ef67e0..3f62de1 100644
--- a/src/site/markdown/extensions.md
+++ b/src/site/markdown/extensions.md
@@ -949,7 +949,7 @@ Shortname: `jsefaXmlWriter`
### `org.apache.batchee.jackson.JacksonJsonReader`
-Use JSefa to read a XML file.
+Use Jackson to read a JSon file.
Sample:
@@ -977,7 +977,7 @@ Shortname: `jacksonJSonReader`
### `org.apache.batchee.jackson.JacksonJSonWriter`
-Use JSefa to write a XML file.
+Use Jackson to write a JSon file.
Sample:
@@ -1004,6 +1004,32 @@ Configuration (excepted for file see org.jsefa.flr.config.FlrConfiguration for d
Shortname: `jacksonJSonWriter`
+### `org.apache.batchee.modelmapper.ModelMapperItemProcessor`
+
+Just a simple item processor mapping input to another type based on ModelMapper library.
+To customize the modelMapper just override `newMapper()` method.
+
+Sample:
+
+<pre class="prettyprint linenums"><![CDATA[
+<step id="step1">
+ <chunk>
+ <reader ref="..." />
+ <processor ref="org.apache.batchee.modelmapper.ModelMapperItemProcessor">
+ <properties>
+ <property name="destinationType" value="...." />
+ </properties>
+ </processor>
+ <writer ref="..." />
+ </chunk>
+</step>]]></pre>
+
+Configuration (excepted for file see org.jsefa.flr.config.FlrConfiguration for detail):
+
+* destinationType: the target type for the mapping (qualified name)
+
+Shortname: `modelMapperProcessor`
+
### `org.apache.batchee.hazelcast.HazelcastLockBatchlet`
A batchlet getting a hazelcast lock.
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8367b00c/src/site/markdown/modules.md
----------------------------------------------------------------------
diff --git a/src/site/markdown/modules.md b/src/site/markdown/modules.md
index e367b0d..01a6adc 100644
--- a/src/site/markdown/modules.md
+++ b/src/site/markdown/modules.md
@@ -219,3 +219,17 @@ Allows you to contol your batchees from Maven.
Provides reader/writer based on jackson to read/write json.
+## ModelMapper
+### Dependency
+
+<pre class="prettyprint linenums"><![CDATA[
+<dependency>
+ <groupId>org.apache.batchee</groupId>
+ <artifactId>batchee-modelmapper</artifactId>
+ <version>${batchee.version}</version>
+</dependency>
+]]></pre>
+
+### Goal
+
+Provides an `ItemProcessor` mapping input bean to another one based on `ModelMapper`.