20 package uk.ac.cam.eng.extraction.hadoop.util;
22 import java.lang.reflect.Field;
24 import org.apache.hadoop.conf.Configuration;
26 import com.beust.jcommander.JCommander;
27 import com.beust.jcommander.Parameter;
28 import com.beust.jcommander.ParameterException;
29 import com.beust.jcommander.ParametersDelegate;
53 private static void setProps(Object params, Configuration conf)
54 throws IllegalArgumentException, IllegalAccessException {
55 for (Field field : params.getClass().getDeclaredFields()) {
56 Parameter paramAnnotation = field.getAnnotation(Parameter.class);
57 if (paramAnnotation != null) {
59 String name = paramAnnotation.names()[0];
60 Object val = field.get(params);
62 throw new RuntimeException(
"Null value for " + name);
64 Class<?> clazz = val.getClass();
65 if (Integer.class == clazz) {
66 conf.setInt(name, (Integer) val);
67 }
else if (Double.class == clazz) {
68 conf.set(name, val.toString());
69 }
else if (Boolean.class == clazz) {
70 conf.setBoolean(name, (Boolean) val);
71 }
else if (String.class == clazz) {
72 conf.set(name, (String) val);
74 }
else if (field.getAnnotation(ParametersDelegate.class) != null) {
75 setProps(field.get(params), conf);
80 public static void ApplyConf(Object params, Configuration conf)
81 throws IllegalArgumentException, IllegalAccessException {
82 setProps(params, conf);
86 JCommander cmd =
new JCommander();
87 cmd.setAcceptUnknownOptions(
true);
88 cmd.addObject(params);
91 }
catch (ParameterException e) {
92 System.err.println(e.getMessage());