File vhostFileFile = new File(vhostFile);
//we're creating a new file here, so we must ensure that Augeas does have this file
//on its load path, otherwise it will refuse to create it.
AugeasConfigurationApache config = (AugeasConfigurationApache) comp.getConfiguration();
AugeasModuleConfig moduleConfig = config.getModuleByName(config.getAugeasModuleName());
boolean willPersist = false;
for (String glob : moduleConfig.getIncludedGlobs()) {
if (Glob.matches(getServerRoot(), glob, vhostFileFile)) {
willPersist = true;
break;
}
}
if (!willPersist) {
//the file wouldn't be loaded by augeas
moduleConfig.addIncludedGlob(vhostFile);
//this also means that there was no include
//that would load the file, so we have to
//add the include directive to the main conf.
List<AugeasNode> includes = tree.matchRelative(tree.getRootNode(), "Include");
AugeasNode include = tree.createNode(tree.getRootNode(), "Include", null,
includes.size() + 1);
tree.createNode(include, "param", vhostFile, 0);
tree.save();
}
try {
vhostFileFile.createNewFile();
} catch (IOException e) {
LOG.error("Failed to create a new vhost file: " + vhostFile, e);
}
comp.close();
comp = getAugeas();
tree = comp.getAugeasTree(moduleConfig.getModuletName());
vhost = tree.createNode(AugeasTree.AUGEAS_DATA_PATH + vhostFile + "/<VirtualHost");
((ApacheAugeasNode) vhost).setParentNode(tree.getRootNode());
}