You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@batchee.apache.org by st...@apache.org on 2014/04/24 14:58:26 UTC

[3/4] git commit: BATCHEE-29 rework proxy creation.

BATCHEE-29 rework proxy creation.

* interfaces must only be listed once -> move to Set


Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/914cd649
Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/914cd649
Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/914cd649

Branch: refs/heads/master
Commit: 914cd649382dd457a7ceffbd65236514b36d4bd5
Parents: 5b4f45c
Author: Mark Struberg <st...@apache.org>
Authored: Thu Apr 24 14:55:47 2014 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Thu Apr 24 14:55:47 2014 +0200

----------------------------------------------------------------------
 .../batchee/container/proxy/ProxyFactory.java   | 66 ++++++++++----------
 1 file changed, 34 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/914cd649/jbatch/src/main/java/org/apache/batchee/container/proxy/ProxyFactory.java
----------------------------------------------------------------------
diff --git a/jbatch/src/main/java/org/apache/batchee/container/proxy/ProxyFactory.java b/jbatch/src/main/java/org/apache/batchee/container/proxy/ProxyFactory.java
index 8267e44..928ccf3 100755
--- a/jbatch/src/main/java/org/apache/batchee/container/proxy/ProxyFactory.java
+++ b/jbatch/src/main/java/org/apache/batchee/container/proxy/ProxyFactory.java
@@ -17,8 +17,8 @@
 package org.apache.batchee.container.proxy;
 
 import java.lang.reflect.Proxy;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.batchee.container.impl.StepContextImpl;
 import org.apache.batchee.container.impl.jobinstance.RuntimeJobExecution;
@@ -68,6 +68,13 @@ public class ProxyFactory {
         return INJECTION_CONTEXT.get();
     }
 
+    public static <T> T createProxy(T delegate, StepContextImpl stepContext, String... nonExceptionHandlingMethods) {
+        return (T) Proxy.newProxyInstance(delegate.getClass().getClassLoader(), getInterfaces(delegate.getClass()),
+                new BatchProxyInvocationHandler(delegate, stepContext, nonExceptionHandlingMethods));
+
+    }
+
+
     /*
      * Decider
      */
@@ -102,45 +109,21 @@ public class ProxyFactory {
     public static ItemReader createItemReaderProxy(final BatchArtifactFactory factory, final String id, final InjectionReferences injectionRefs,
                                                         final StepContextImpl stepContext, final RuntimeJobExecution execution) {
         final ItemReader loadedArtifact = (ItemReader) loadArtifact(factory, id, injectionRefs, execution);
-        return (ItemReader) Proxy.newProxyInstance(loadedArtifact.getClass().getClassLoader(), getInterfaces(loadedArtifact.getClass()),
-                new BatchProxyInvocationHandler(loadedArtifact, stepContext, "readItem"));
+        return createProxy(loadedArtifact, stepContext, "readItem");
     }
 
-    /**
-     * @return all the interfaces fo the given class and it's superclasses
-     */
-    private static Class<?>[] getInterfaces(Class<?> clazz) {
-        if (clazz.getSuperclass() == Object.class) {
-            return clazz.getInterfaces();
-        } else {
-            List<Class<?>> clazzes = new ArrayList<Class<?>>();
-            while (clazz != Object.class) {
-                for (Class<?> interfaceClass : clazz.getInterfaces()) {
-                    clazzes.add(interfaceClass);
-                }
-                clazz = clazz.getSuperclass();
-            }
-
-            return clazzes.toArray(new Class<?>[clazzes.size()]);
-        }
-    }
-
-    public static ItemProcessorProxy createItemProcessorProxy(final BatchArtifactFactory factory, final String id, final InjectionReferences injectionRefs,
+    public static ItemProcessor createItemProcessorProxy(final BatchArtifactFactory factory, final String id, final InjectionReferences injectionRefs,
                                                               final StepContextImpl stepContext, final RuntimeJobExecution execution) {
         final ItemProcessor loadedArtifact = (ItemProcessor) loadArtifact(factory, id, injectionRefs, execution);
-        final ItemProcessorProxy proxy = new ItemProcessorProxy(loadedArtifact);
-        proxy.setStepContext(stepContext);
-        return proxy;
+        return createProxy(loadedArtifact, stepContext, "processItem");
     }
 
-    public static ItemWriterProxy createItemWriterProxy(final BatchArtifactFactory factory, final String id, final InjectionReferences injectionRefs,
+    public static ItemWriter createItemWriterProxy(final BatchArtifactFactory factory, final String id, final InjectionReferences injectionRefs,
                                                         final StepContextImpl stepContext, final RuntimeJobExecution execution) {
         final ItemWriter loadedArtifact = (ItemWriter) loadArtifact(factory, id, injectionRefs, execution);
-        final ItemWriterProxy proxy = new ItemWriterProxy(loadedArtifact);
-        proxy.setStepContext(stepContext);
-        return proxy;
+        return createProxy(loadedArtifact, stepContext, "writeItems");
     }
-        
+
     /*
      * The four partition-related artifacts
      */
@@ -176,4 +159,23 @@ public class ProxyFactory {
         proxy.setStepContext(stepContext);
         return proxy;
     }
+
+    /**
+     * @return all the interfaces fo the given class and it's superclasses
+     */
+    private static Class<?>[] getInterfaces(Class<?> clazz) {
+        if (clazz.getSuperclass() == Object.class) {
+            return clazz.getInterfaces();
+        } else {
+            Set<Class<?>> clazzes = new HashSet<Class<?>>();
+            while (clazz != Object.class) {
+                for (Class<?> interfaceClass : clazz.getInterfaces()) {
+                    clazzes.add(interfaceClass);
+                }
+                clazz = clazz.getSuperclass();
+            }
+
+            return clazzes.toArray(new Class<?>[clazzes.size()]);
+        }
+    }
 }