* into finalChild. The command will have currentParent as it's parent.
*/
protected Command buildCommand(Config cmdConfig, Command currentParent, Command finalChild) {
Set<Map.Entry<String, Object>> entries = cmdConfig.root().unwrapped().entrySet();
if (entries.size() != 1) {
throw new MorphlineCompilationException("Illegal number of entries: " + entries.size(), cmdConfig);
}
Map.Entry<String, Object> entry = entries.iterator().next();
String cmdName = entry.getKey();
Class cmdClass;
LOG.trace("Building command: {}", cmdName);
if (!cmdName.contains(".") && !cmdName.contains("/")) {
cmdClass = getContext().getCommandBuilder(cmdName);
if (cmdClass == null) {
throw new MorphlineCompilationException("No command builder registered for name: " + cmdName, cmdConfig);
}
} else {
String className = cmdName.replace('/', '.');
try {
cmdClass = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new MorphlineCompilationException("Cannot find command class: " + className, cmdConfig, e);
}
}
Object obj;
try {
obj = cmdClass.newInstance();
} catch (Exception e) {
throw new MorphlineCompilationException("Cannot instantiate command class: " + cmdClass.getName(), cmdConfig, e);
}
if (!(obj instanceof CommandBuilder)) {
throw new MorphlineCompilationException("Type of command " + cmdName + " must be an instance of "
+ CommandBuilder.class.getName() + " but is: " + cmdClass.getName(), cmdConfig);
}
CommandBuilder builder = (CommandBuilder) obj;
Command cmd = builder.build(cmdConfig.getConfig(cmdName), currentParent, finalChild, getContext());
return cmd;