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 2014/01/07 08:19:37 UTC
git commit: BATCHEE-13 supporting more injection type in jbatch
components
Updated Branches:
refs/heads/master 80805f85a -> 8688a28d8
BATCHEE-13 supporting more injection type in jbatch components
Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/8688a28d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/8688a28d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/8688a28d
Branch: refs/heads/master
Commit: 8688a28d82100fcdf59e020ac520d5a153d22732
Parents: 80805f8
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Tue Jan 7 08:18:33 2014 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Tue Jan 7 08:19:36 2014 +0100
----------------------------------------------------------------------
jbatch/pom.xml | 6 +
.../container/cdi/BatchProducerBean.java | 245 ++++++++++++++++++-
.../container/util/DependencyInjections.java | 16 +-
.../apache/batchee/test/cdi/InjectionsTest.java | 35 +++
.../batchee/test/mock/InjectionsMock.java | 35 +++
.../META-INF/batch-jobs/injections.xml | 24 ++
jbatch/src/test/resources/META-INF/beans.xml | 16 ++
7 files changed, 370 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8688a28d/jbatch/pom.xml
----------------------------------------------------------------------
diff --git a/jbatch/pom.xml b/jbatch/pom.xml
index eef9fcc..c91b0ee 100644
--- a/jbatch/pom.xml
+++ b/jbatch/pom.xml
@@ -62,6 +62,12 @@
</dependency>
<dependency>
+ <groupId>org.apache.xbean</groupId>
+ <artifactId>xbean-reflect</artifactId>
+ <version>3.16</version>
+ </dependency>
+
+ <dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8688a28d/jbatch/src/main/java/org/apache/batchee/container/cdi/BatchProducerBean.java
----------------------------------------------------------------------
diff --git a/jbatch/src/main/java/org/apache/batchee/container/cdi/BatchProducerBean.java b/jbatch/src/main/java/org/apache/batchee/container/cdi/BatchProducerBean.java
index a877c32..07d9748 100755
--- a/jbatch/src/main/java/org/apache/batchee/container/cdi/BatchProducerBean.java
+++ b/jbatch/src/main/java/org/apache/batchee/container/cdi/BatchProducerBean.java
@@ -24,8 +24,38 @@ import javax.batch.runtime.context.JobContext;
import javax.batch.runtime.context.StepContext;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
+import javax.management.ObjectName;
+import java.io.File;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.URI;
+import java.net.URL;
+import java.util.Date;
+import java.util.Properties;
+import java.util.logging.Logger;
+import java.util.regex.Pattern;
public class BatchProducerBean {
+ private static final int[] EMPTY_INTS = new int[0];
+ private static final double[] EMPTY_DOUBLES = new double[0];
+ private static final String[] EMPTY_STRINGS = new String[0];
+
+ @Produces
+ public JobContext getJobContext() {
+ if (ProxyFactory.getInjectionReferences() != null) {
+ return ProxyFactory.getInjectionReferences().getJobContext();
+ }
+ return null;
+ }
+
+ @Produces
+ public StepContext getStepContext() {
+ if (ProxyFactory.getInjectionReferences() != null) {
+ return ProxyFactory.getInjectionReferences().getStepContext();
+ }
+ return null;
+ }
+
@Produces
@BatchProperty
public String produceProperty(final InjectionPoint injectionPoint) {
@@ -41,21 +71,224 @@ public class BatchProducerBean {
return DependencyInjections.getPropertyValue(ProxyFactory.getInjectionReferences().getProps(), batchPropName);
}
return null;
+ }
+ @Produces
+ @BatchProperty
+ public Integer produceIntProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Integer.class, Integer.class);
+ }
+ return 0;
}
@Produces
- public JobContext getJobContext() {
- if (ProxyFactory.getInjectionReferences() != null) {
- return ProxyFactory.getInjectionReferences().getJobContext();
+ @BatchProperty
+ public Double produceDoubleProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Double.class, Double.class);
+ }
+ return 0.;
+ }
+
+ @Produces
+ @BatchProperty
+ public Float produceFloatProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Float.class, Float.class);
+ }
+ return 0.f;
+ }
+
+ @Produces
+ @BatchProperty
+ public Short produceShortProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Short.class, Short.class);
+ }
+ return 0;
+ }
+
+ @Produces
+ @BatchProperty
+ public Boolean produceBooleanProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Boolean.class, Boolean.class);
+ }
+ return false;
+ }
+
+ @Produces
+ @BatchProperty
+ public Long produceLongProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Long.class, Long.class);
+ }
+ return 0L;
+ }
+
+ @Produces
+ @BatchProperty
+ public Byte produceByteProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Byte.class, Byte.class);
+ }
+ return 0;
+ }
+
+ @Produces
+ @BatchProperty
+ public Character produceCharProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Character.class, Character.class);
+ }
+ return 0;
+ }
+
+ @Produces
+ @BatchProperty
+ public int[] produceIntArrayProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, EMPTY_INTS.getClass(), null);
+ }
+ return EMPTY_INTS;
+ }
+
+ @Produces
+ @BatchProperty
+ public double[] produceDoubleArrayProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, EMPTY_DOUBLES.getClass(), null);
+ }
+ return EMPTY_DOUBLES;
+ }
+
+ @Produces
+ @BatchProperty
+ public String[] produceStringArrayProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, EMPTY_STRINGS.getClass(), null);
+ }
+ return EMPTY_STRINGS;
+ }
+
+ @Produces
+ @BatchProperty
+ public Date produceDateProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Date.class, Date.class);
}
return null;
}
@Produces
- public StepContext getStepContext() {
- if (ProxyFactory.getInjectionReferences() != null) {
- return ProxyFactory.getInjectionReferences().getStepContext();
+ @BatchProperty
+ public Inet4Address produceIp4Property(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Inet4Address.class, Inet4Address.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public Inet6Address produceIp6Property(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Inet6Address.class, Inet6Address.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public URI produceUriProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, URI.class, URI.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public URL produceUrlProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, URL.class, URL.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public Logger produceLoggerProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Logger.class, Logger.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public Properties producePropertiesProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Properties.class, Properties.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public Class produceClassProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Class.class, Class.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public Pattern producePatternProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, Pattern.class, Pattern.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public ObjectName produceObjectNameProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, ObjectName.class, ObjectName.class);
+ }
+ return null;
+ }
+
+ @Produces
+ @BatchProperty
+ public File produceFileProperty(final InjectionPoint injectionPoint) {
+ final String v = produceProperty(injectionPoint);
+ if (v != null) {
+ return DependencyInjections.convertTo(v, File.class, File.class);
}
return null;
}
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8688a28d/jbatch/src/main/java/org/apache/batchee/container/util/DependencyInjections.java
----------------------------------------------------------------------
diff --git a/jbatch/src/main/java/org/apache/batchee/container/util/DependencyInjections.java b/jbatch/src/main/java/org/apache/batchee/container/util/DependencyInjections.java
index c2fd7e1..5755c82 100755
--- a/jbatch/src/main/java/org/apache/batchee/container/util/DependencyInjections.java
+++ b/jbatch/src/main/java/org/apache/batchee/container/util/DependencyInjections.java
@@ -20,12 +20,14 @@ import org.apache.batchee.container.exception.BatchContainerRuntimeException;
import org.apache.batchee.container.exception.IllegalBatchPropertyException;
import org.apache.batchee.container.proxy.InjectionReferences;
import org.apache.batchee.jaxb.Property;
+import org.apache.xbean.propertyeditor.PropertyEditors;
import javax.batch.api.BatchProperty;
import javax.batch.runtime.context.JobContext;
import javax.batch.runtime.context.StepContext;
import javax.inject.Inject;
import java.lang.reflect.Field;
+import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
@@ -64,7 +66,10 @@ public class DependencyInjections {
// the field otherwise the default value will remain
try {
if (!(propValue == null)) {
- batchProperty.getValue().set(artifact, propValue);
+ final Field field = batchProperty.getValue();
+ final Type genericType = field.getGenericType();
+ final Class<?> type = field.getType();
+ field.set(artifact, convertTo(propValue, genericType, type));
}
} catch (IllegalArgumentException e) {
throw new IllegalBatchPropertyException("The given property value is not an instance of the declared field.", e);
@@ -76,6 +81,15 @@ public class DependencyInjections {
}
+ public static <T> T convertTo(final String propValue, final Type genericType, final Class<T> type) {
+ if (String.class.equals(type)) {
+ return (T) propValue;
+ } else if (type == null ||PropertyEditors.canConvert(type)) {
+ return (T) PropertyEditors.getValue(genericType, propValue);
+ }
+ throw new IllegalArgumentException("Can't find a converter for type " + type);
+ }
+
/**
* @param props list of properties from job xml
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8688a28d/jbatch/src/test/java/org/apache/batchee/test/cdi/InjectionsTest.java
----------------------------------------------------------------------
diff --git a/jbatch/src/test/java/org/apache/batchee/test/cdi/InjectionsTest.java b/jbatch/src/test/java/org/apache/batchee/test/cdi/InjectionsTest.java
new file mode 100644
index 0000000..ca047ee
--- /dev/null
+++ b/jbatch/src/test/java/org/apache/batchee/test/cdi/InjectionsTest.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2012 International Business Machines Corp.
+ *
+ * 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.
+*/
+package org.apache.batchee.test.cdi;
+
+import org.testng.annotations.Test;
+
+import javax.batch.operations.JobOperator;
+import javax.batch.runtime.BatchRuntime;
+
+import static org.apache.batchee.util.Batches.waitForEnd;
+import static org.testng.Assert.assertEquals;
+
+public class InjectionsTest {
+ @Test
+ public void run() {
+ final JobOperator operator = BatchRuntime.getJobOperator();
+ final long id = operator.start("injections", null);
+ waitForEnd(operator, id);
+ assertEquals(operator.getStepExecutions(id).iterator().next().getExitStatus(), "true");
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8688a28d/jbatch/src/test/java/org/apache/batchee/test/mock/InjectionsMock.java
----------------------------------------------------------------------
diff --git a/jbatch/src/test/java/org/apache/batchee/test/mock/InjectionsMock.java b/jbatch/src/test/java/org/apache/batchee/test/mock/InjectionsMock.java
new file mode 100644
index 0000000..55a98de
--- /dev/null
+++ b/jbatch/src/test/java/org/apache/batchee/test/mock/InjectionsMock.java
@@ -0,0 +1,35 @@
+/*
+ * 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.test.mock;
+
+import javax.batch.api.AbstractBatchlet;
+import javax.batch.api.BatchProperty;
+import javax.inject.Inject;
+import javax.inject.Named;
+import java.net.URL;
+
+@Named("injected")
+public class InjectionsMock extends AbstractBatchlet {
+ @Inject
+ @BatchProperty
+ private URL url;
+
+ @Override
+ public String process() throws Exception {
+ return url.toExternalForm().equals("http://batchee.incubator.org") + "";
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8688a28d/jbatch/src/test/resources/META-INF/batch-jobs/injections.xml
----------------------------------------------------------------------
diff --git a/jbatch/src/test/resources/META-INF/batch-jobs/injections.xml b/jbatch/src/test/resources/META-INF/batch-jobs/injections.xml
new file mode 100644
index 0000000..0076454
--- /dev/null
+++ b/jbatch/src/test/resources/META-INF/batch-jobs/injections.xml
@@ -0,0 +1,24 @@
+<?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="injections" version="1.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
+ <step id="inject-step">
+ <batchlet ref="injected">
+ <properties>
+ <property name="url" value="http://batchee.incubator.org"/>
+ </properties>
+ </batchlet>
+ </step>
+</job>
http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/8688a28d/jbatch/src/test/resources/META-INF/beans.xml
----------------------------------------------------------------------
diff --git a/jbatch/src/test/resources/META-INF/beans.xml b/jbatch/src/test/resources/META-INF/beans.xml
new file mode 100644
index 0000000..c0699dc
--- /dev/null
+++ b/jbatch/src/test/resources/META-INF/beans.xml
@@ -0,0 +1,16 @@
+<?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.
+-->
+<beans />