modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java
| 69 +++++++---
modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java
| 6
modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasTreeBuilderApache.java
| 25 +--
3 files changed, 62 insertions(+), 38 deletions(-)
New commits:
commit 258be003ddf7bf022611ef183ed60a7e7ee7ef18
Author: Lukas Krejci <lkrejci(a)redhat.com>
Date: Wed May 4 18:40:08 2011 +0200
BZ 700616 - Augeas tree abstraction now replaces the Include nodes with their contents
instead of just appending their contents to their children list.
This seems to be the original intended behavior of the class according to its
description but actually was not the case.
diff --git
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java
index 6cb168e..99cc8cb 100644
---
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java
+++
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java
@@ -24,7 +24,10 @@ package org.rhq.plugins.apache.augeas;
import java.io.File;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.rhq.augeas.node.AugeasNode;
import org.rhq.augeas.node.AugeasNodeLazy;
@@ -42,13 +45,14 @@ import org.rhq.augeas.tree.AugeasTreeException;
* and modifies the get* methods to handle these as well.
*
* @author Filip Drabek
+ * @author Lukas Krejci
*/
public class ApacheAugeasNode extends AugeasNodeLazy implements AugeasNode {
/**
* List of included nodes.
*/
- private List<AugeasNode> includedNodes;
+ private Map<Integer, List<AugeasNode>> includedNodes;
public ApacheAugeasNode(String fullPath, AugeasTree tree) {
super(fullPath, tree);
@@ -72,8 +76,22 @@ public class ApacheAugeasNode extends AugeasNodeLazy implements
AugeasNode {
public List<AugeasNode> getChildNodes() {
List<AugeasNode> nodes = null;
nodes = ag.match(getFullPath() + File.separatorChar + "*");
- if (includedNodes != null)
- nodes.addAll(includedNodes);
+
+ if (includedNodes != null) {
+ //to avoid having to recompute indexes to insert the included nodes into the
+ //list of nodes as seen by augeas, let's include them from the biggest
index
+ //to the lowest.
+ List<Integer> includeNodeIndexes = new
ArrayList<Integer>(includedNodes.keySet());
+ Collections.sort(includeNodeIndexes, Collections.reverseOrder());
+
+ for(Integer idx : includeNodeIndexes) {
+ //remove the include node itself
+ nodes.remove(idx);
+
+ //add the included nodes instead of it
+ nodes.addAll(idx, includedNodes.get(idx));
+ }
+ }
return nodes;
}
@@ -83,26 +101,36 @@ public class ApacheAugeasNode extends AugeasNodeLazy implements
AugeasNode {
*
* @param nodes
*/
- public void addIncludeNodes(List<AugeasNode> nodes) {
+ public void addIncludeNodes(AugeasNode includeNode, List<AugeasNode> nodes) {
if (nodes.isEmpty())
return;
if (includedNodes == null)
- includedNodes = new ArrayList<AugeasNode>();
-
- includedNodes.addAll(nodes);
- }
-
- /**
- * Adds the node to the list of the included child nodes.
- *
- * @param node
- */
- public void addIncludeNode(AugeasNode node) {
- if (includedNodes == null)
- includedNodes = new ArrayList<AugeasNode>();
-
- includedNodes.add(node);
+ includedNodes = new HashMap<Integer, List<AugeasNode>>();
+
+ List<AugeasNode> childNodes = super.getChildNodes();
+ int idx = 0;
+ boolean found = false;
+
+ for(AugeasNode child : childNodes) {
+ if (child.getLabel().equals(includeNode.getLabel()) && child.getSeq()
== includeNode.getSeq()) {
+ found = true;
+ break;
+ }
+
+ ++idx;
+ }
+
+ if (found) {
+ List<AugeasNode> alreadyIncluded = includedNodes.get(idx);
+ if (alreadyIncluded == null) {
+ //copy the nodes over to a new list so that we can modify it later
without modifying the original collection
+ //which might be unexpected on the caller site.
+ includedNodes.put(idx, new ArrayList<AugeasNode>(nodes));
+ } else {
+ alreadyIncluded.addAll(nodes);
+ }
+ }
}
public AugeasNode getParentNode() {
@@ -132,7 +160,8 @@ public class ApacheAugeasNode extends AugeasNodeLazy implements
AugeasNode {
//else if this node is included from another file
//and we would destroy that association here.
}
+
public void setParentNode(AugeasNode node){
this.parentNode = node;
- }
+ }
}
diff --git
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java
index d4f7dd2..a3a715c 100644
---
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java
+++
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java
@@ -109,7 +109,7 @@ public class AugeasConfigurationApache extends
PluginDescriptorBasedAugeasConfig
File check = new File(expression);
File root = new File(check.isAbsolute() ? Glob.rootPortion(expression) :
serverRootPath);
- files.addAll(Glob.match(root, expression));
+ files.addAll(Glob.match(root, expression, Glob.ALPHABETICAL_COMPARATOR));
for (File fl : files){
if (fl.exists() && fl.isFile()) {
@@ -155,7 +155,7 @@ public class AugeasConfigurationApache extends
PluginDescriptorBasedAugeasConfig
File check = new File(incl);
File root = new File(check.isAbsolute() ? Glob.rootPortion(incl) :
serverRootPath);
- files.addAll(Glob.match(root, incl));
+ files.addAll(Glob.match(root, incl, Glob.ALPHABETICAL_COMPARATOR));
}
if (module.getExcludedGlobs() != null) {
@@ -191,7 +191,7 @@ public class AugeasConfigurationApache extends
PluginDescriptorBasedAugeasConfig
for (String path : foundIncludes) {
File check = new File(path);
File root = new File(check.isAbsolute() ? Glob.rootPortion(path) :
serverRoot);
- for (File f :Glob.match(root, path)){
+ for (File f :Glob.match(root, path, Glob.ALPHABETICAL_COMPARATOR)){
ret.add(f);
}
}
diff --git
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasTreeBuilderApache.java
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasTreeBuilderApache.java
index e6454b7..fbde74f 100644
---
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasTreeBuilderApache.java
+++
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasTreeBuilderApache.java
@@ -43,7 +43,6 @@ public class AugeasTreeBuilderApache implements AugeasTreeBuilder {
private Map<String, List<File>> includes;
private Map<AugeasNode, List<String>> incl;
- private static final String[] NESTED_INCLUDE_DIRECTIVES = {
"<VirtualHost", "<Directory" };
private Augeas ag;
public AugeasTreeBuilderApache() {
@@ -78,7 +77,7 @@ public class AugeasTreeBuilderApache implements AugeasTreeBuilder {
File check = new File(inclName);
File root = new File(check.isAbsolute() ? Glob.rootPortion(inclName) :
apacheConfig.getServerRootPath());
- files.addAll(Glob.match(root, inclName));
+ files.addAll(Glob.match(root, inclName, Glob.ALPHABETICAL_COMPARATOR));
if (module.getExcludedGlobs() != null)
Glob.excludeAll(files, module.getExcludedGlobs());
@@ -87,7 +86,7 @@ public class AugeasTreeBuilderApache implements AugeasTreeBuilder {
includes.put(inclName, files);
}
- updateIncludes((ApacheAugeasNode) rootNode, tree, rootPath, false);
+ updateIncludes((ApacheAugeasNode) rootNode, tree, rootPath, null);
//List<String> rootconf = new ArrayList<String>();
// rootconf.add(ApacheAugeasTree.AUGEAS_DATA_PATH + rootPath);
@@ -97,7 +96,7 @@ public class AugeasTreeBuilderApache implements AugeasTreeBuilder {
return tree;
}
- public void updateIncludes(ApacheAugeasNode parentNode, AugeasTree tree, String
fileName, boolean update)
+ public void updateIncludes(ApacheAugeasNode parentNode, AugeasTree tree, String
fileName, AugeasNode includeNode)
throws AugeasRhqException {
List<String> nestedNodes = ag.match(ApacheAugeasTree.AUGEAS_DATA_PATH +
fileName + File.separator + "*");
@@ -111,14 +110,14 @@ public class AugeasTreeBuilderApache implements AugeasTreeBuilder {
createdNodes.add(newNode);
}
- if (update)
- parentNode.addIncludeNodes(createdNodes);
+ if (includeNode != null)
+ parentNode.addIncludeNodes(includeNode, createdNodes);
for (AugeasNode node : createdNodes) {
- if (canContainIncludes(node.getLabel())) {
+ if (canContainNestedNodes(node.getLabel())) {
String labelName = node.getLabel()
+ ((node.getSeq() != 0) ? "[" +
String.valueOf(node.getSeq()) + "]" : "");
- updateIncludes((ApacheAugeasNode) node, tree, fileName + File.separator +
labelName, false);
+ updateIncludes((ApacheAugeasNode) node, tree, fileName + File.separator +
labelName, null);
}
if (node.getLabel().equals("Include")) {
String val = ag.get(node.getFullPath() + File.separator +
"param");
@@ -128,7 +127,7 @@ public class AugeasTreeBuilderApache implements AugeasTreeBuilder {
List<String> names = new ArrayList<String>();
for (File file : files) {
names.add(ApacheAugeasTree.AUGEAS_DATA_PATH +
file.getAbsolutePath());
- updateIncludes((ApacheAugeasNode) node.getParentNode(), tree,
file.getAbsolutePath(), true);
+ updateIncludes((ApacheAugeasNode) node.getParentNode(), tree,
file.getAbsolutePath(), node);
}
if (incl.containsKey(node.getParentNode())) {
List<String> list = incl.get(node.getParentNode());
@@ -140,11 +139,7 @@ public class AugeasTreeBuilderApache implements AugeasTreeBuilder {
}
}
- private boolean canContainIncludes(String name) {
- for (String directive : NESTED_INCLUDE_DIRECTIVES) {
- if (directive.equals(name))
- return true;
- }
- return false;
+ private boolean canContainNestedNodes(String name) {
+ return name.startsWith("<");
}
}