* Note: The intention of this is to provide the bare essentials that are
* required to make the {@linkplain MemPipeline} work. It lacks even the basic
* things that can proved some support for unit testing pipeline.
*/
private static TaskInputOutputContext<?, ?, ?, ?> getInMemoryContext(final Configuration conf) {
ProxyFactory factory = new ProxyFactory();
Class<TaskInputOutputContext> superType = TaskInputOutputContext.class;
Class[] types = new Class[0];
Object[] args = new Object[0];
if (superType.isInterface()) {
factory.setInterfaces(new Class[] { superType });
} else {
types = new Class[] { Configuration.class, TaskAttemptID.class, RecordWriter.class, OutputCommitter.class,
StatusReporter.class };
args = new Object[] { conf, new TaskAttemptID(), null, null, null };
factory.setSuperclass(superType);
}
factory.setFilter(new MethodFilter() {
@Override
public boolean isHandled(Method m) {
String name = m.getName();
return "getConfiguration".equals(name) || "getCounter".equals(name) || "progress".equals(name);
}
});
MethodHandler handler = new MethodHandler() {
@Override
public Object invoke(Object arg0, Method m, Method arg2, Object[] args) throws Throwable {
String name = m.getName();
if ("getConfiguration".equals(name)) {
return conf;
} else if ("progress".equals(name)) {
// no-op
return null;
} else { // getCounter
if (args.length == 1) {
return MemPipeline.getCounters().findCounter((Enum<?>) args[0]);
} else {
return MemPipeline.getCounters().findCounter((String) args[0], (String) args[1]);
}
}
}
};
try {
Object newInstance = factory.create(types, args, handler);
return (TaskInputOutputContext<?, ?, ?, ?>) newInstance;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}