}
public static TaskInputOutputContext getTaskIOContext(
final Broadcast<Configuration> conf,
final Accumulator<Map<String, Long>> counters) {
ProxyFactory factory = new ProxyFactory();
Class<TaskInputOutputContext> superType = TaskInputOutputContext.class;
Class[] types = new Class[0];
Object[] args = new Object[0];
final TaskAttemptID taskAttemptId = new TaskAttemptID();
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.value(), taskAttemptId, null, null, null };
factory.setSuperclass(superType);
}
final Set<String> handledMethods = ImmutableSet.of("getConfiguration", "getCounter",
"progress", "getTaskAttemptID");
factory.setFilter(new MethodFilter() {
@Override
public boolean isHandled(Method m) {
return handledMethods.contains(m.getName());
}
});
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.value();
} else if ("progress".equals(name)) {
// no-op
return null;
} else if ("getTaskAttemptID".equals(name)) {
return taskAttemptId;
} else if ("getCounter".equals(name)){ // getCounter
if (args.length == 1) {
return getCounter(counters, args[0].getClass().getName(), ((Enum) args[0]).name());
} else {
return getCounter(counters, (String) args[0], (String) args[1]);
}
} else {
throw new IllegalStateException("Unhandled method " + name);
}
}
};
try {
Object newInstance = factory.create(types, args, handler);
return (TaskInputOutputContext<?, ?, ?, ?>) newInstance;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}