report.setErrorMessage(ApacheServerComponent.CONFIGURATION_NOT_SUPPORTED_ERROR_MESSAGE);
return report;
}
ResourceType resourceType = report.getResourceType();
AugeasComponent comp = null;
try {
comp = getAugeas();
if (resourceType.equals(getDirectoryResourceType())) {
Configuration resourceConfiguration = report.getResourceConfiguration();
Configuration pluginConfiguration = report.getPluginConfiguration();
String directoryName = report.getUserSpecifiedResourceName();
//fill in the plugin configuration
//get the directive index
AugeasTree tree = comp.getAugeasTree(ApacheServerComponent.AUGEAS_HTTP_MODULE_NAME);
AugeasNode myNode = getNode(tree);
List<AugeasNode> directories = myNode.getChildByLabel("<Directory");
int seq = 1;
/*
* myNode will be parent node of the new Directory node.
* We need to create a new node for directory node which will contain child nodes.
* To create a node we can call method from AugeasTree which will create a node. In this method is
* parameter sequence, if we will leave this parameter empty and there will be more nodes with
* the same label, new node will be created but the method createNode will return node with index 0 resp 1.
* If that will happen we can not update the node anymore because we are updating wrong node.
* To avoid this situation we need to know what is the last sequence nr. of virtual host's child (directory) nodes.
* We can not just count child nodes with the same label because some of the child nodes
* could be stored in another file. So that in httpd configurationstructure they are child nodes of virtual host,
* but in augeas configuration structure they can be child nodes of node Include[];.
*/
for (AugeasNode n : directories) {
String param = n.getFullPath();
int end = param.lastIndexOf(File.separatorChar);
if (end != -1)
if (myNode.getFullPath().equals(param.substring(0, end)))
seq++;
}
//pluginConfiguration.put(new PropertySimple(ApacheDirectoryComponent.DIRECTIVE_INDEX_PROP, seq));
//we don't support this yet... need to figure out how...
pluginConfiguration.put(new PropertySimple(ApacheDirectoryComponent.REGEXP_PROP, false));
String dirNameToSet = AugeasNodeValueUtil.escape(directoryName);
//now actually create the data in augeas
try {
ApacheAugeasMapping mapping = new ApacheAugeasMapping(tree);
AugeasNode directoryNode = tree.createNode(myNode, "<Directory", null, seq);
String myNodeKey = AugeasNodeSearch.getNodeKey(myNode, tree.getRootNode());
tree.createNode(directoryNode, "param", dirNameToSet, 0);
mapping.updateAugeas(directoryNode, resourceConfiguration,
resourceType.getResourceConfigurationDefinition());
tree.save();
comp.close();
tree = comp.getAugeasTree(ApacheServerComponent.AUGEAS_HTTP_MODULE_NAME);
AugeasNode parentNode;
if (myNodeKey.equals("")) {
parentNode = tree.getRootNode();
} else
parentNode = AugeasNodeSearch.findNodeById(tree.getRootNode(), myNodeKey);
List<AugeasNode> nodes = parentNode.getChildByLabel("<Directory");
if (nodes.size() < seq) {
report.setStatus(CreateResourceStatus.FAILURE);
report.setErrorMessage("Could not create directory node.");
}
AugeasNode nd = nodes.get(seq - 1);
String key = AugeasNodeSearch.getNodeKey(nd, parentNode);
report.setResourceKey(key);
report.setResourceName(directoryName);
report.setStatus(CreateResourceStatus.SUCCESS);
resourceContext.getParentResourceComponent().finishChildResourceCreate(report);
} catch (Exception e) {
LOG.error("Could not create httpd virtual host child resource.", e);
report.setException(e);
report.setStatus(CreateResourceStatus.FAILURE);
}
} else {
report.setErrorMessage("Unable to create resources of type " + resourceType.getName());
report.setStatus(CreateResourceStatus.FAILURE);
}
} finally {
if (comp != null)
comp.close();
}
return report;
}