Skip to content
Permalink
Browse files
BATCHEE-29 rework proxy creation.
* interfaces must only be listed once -> move to Set
  • Loading branch information
struberg committed Apr 24, 2014
1 parent 5b4f45c commit 914cd649382dd457a7ceffbd65236514b36d4bd5
Showing 1 changed file with 34 additions and 32 deletions.
@@ -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 static InjectionReferences getInjectionReferences() {
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 static CheckpointAlgorithmProxy createCheckpointAlgorithmProxy(final Batc
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 static PartitionCollectorProxy createPartitionCollectorProxy(final BatchA
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()]);
}
}
}

0 comments on commit 914cd64

Please sign in to comment.