}
report.setResourceKey(resourceKey);
report.setResourceName(resourceName);
AugeasComponent comp = getAugeas();
//determine the resource name
AugeasTree tree;
try {
tree = comp.getAugeasTree(AUGEAS_HTTP_MODULE_NAME);
//fill in the plugin config
String url = "http://" + addr.host + ":" + addr.port + "/";
vhostPluginConfig.put(new PropertySimple(ApacheVirtualHostServiceComponent.URL_CONFIG_PROP, url));
//determine the sequence number of the new vhost
List<AugeasNode> existingVhosts = tree.matchRelative(tree.getRootNode(), "<VirtualHost");
int seq = existingVhosts.size() + 1;
Configuration pluginConfig = resourceContext.getPluginConfiguration();
String creationType = pluginConfig.getSimpleValue(PLUGIN_CONFIG_PROP_VHOST_CREATION_POLICY,
PLUGIN_CONFIG_VHOST_PER_FILE_PROP_VALUE);
AugeasNode vhost = null;
String vhostFile = comp.getConfiguration().getModules().get(0).getConfigFiles().get(0);
if (PLUGIN_CONFIG_VHOST_IN_SINGLE_FILE_PROP_VALUE.equals(creationType)) {
vhost = tree.createNode(tree.getRootNode(), "<VirtualHost", null, seq);
} else if (PLUGIN_CONFIG_VHOST_PER_FILE_PROP_VALUE.equals(creationType)) {
String mask = pluginConfig.getSimpleValue(PLUGIN_CONFIG_PROP_VHOST_FILES_MASK, null);
if (mask == null) {
report.setErrorMessage("No virtual host file mask configured.");
} else {
vhostFile = getNewVhostFileName(addr, mask);
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());
}
}
if (vhost == null) {
report.setStatus(CreateResourceStatus.FAILURE);
} else {
try {
for (int i = 0; i < vhostDefs.length; ++i) {
tree.createNode(vhost, "param", vhostDefs[i], i + 1);
}
ApacheAugeasMapping mapping = new ApacheAugeasMapping(tree);
mapping.updateAugeas(vhost, vhostResourceConfig, vhostResourceConfigDef);
tree.save();
report.setStatus(CreateResourceStatus.SUCCESS);
finishChildResourceCreate(report);
} catch (Exception e) {
report.setStatus(CreateResourceStatus.FAILURE);
report.setException(e);
}
}
} finally {
if (comp != null)
comp.close();
}
}
return report;
}