1 package org.sapia.regis.ant; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import java.util.Properties ; 10 11 import org.apache.tools.ant.BuildException; 12 import org.apache.tools.ant.Task; 13 import org.sapia.regis.Node; 14 import org.sapia.regis.Path; 15 import org.sapia.regis.Property; 16 import org.sapia.regis.Registry; 17 import org.sapia.regis.RegistryContext; 18 19 public class CheckConfigTask extends Task{ 20 21 private File file; 22 private List checks = new ArrayList (); 23 24 public void setProperties(File f){ 25 file = f; 26 } 27 28 public void execute() throws BuildException { 29 if(file == null){ 30 throw new BuildException("Regis property file not set"); 31 } 32 InputStream is = null; 33 Properties props = new Properties (); 34 try{ 35 is = new FileInputStream (file); 36 props.load(is); 37 }catch(IOException e){ 38 try{ 39 is.close(); 40 }catch(Exception e2){} 41 throw new BuildException("Could not load registry properties", e); 42 } 43 RegistryContext ctx = new RegistryContext(props); 44 Registry reg = null; 45 try{ 46 reg = ctx.connect(); 47 }catch(Exception e){ 48 throw new BuildException("Could not instantiate registry", e); 49 } 50 performChecks(reg); 51 } 52 53 private void performChecks(Registry reg){ 54 for(int i = 0; i < checks.size(); i++){ 55 Check ch = (Check)checks.get(i); 56 ch.execute(reg); 57 } 58 } 59 60 public CheckProperty createCheckProperty(){ 61 CheckProperty check = new CheckProperty(); 62 checks.add(check); 63 return check; 64 } 65 66 public CheckNode createCheckNode(){ 67 CheckNode check = new CheckNode(); 68 checks.add(check); 69 return check; 70 } 71 72 public static interface Check{ 73 public void execute(Registry reg) throws BuildException; 74 public void execute(Node node) throws BuildException; 75 } 76 77 public static final class CheckProperty implements Check{ 78 79 private Path path; 80 private String name, value; 81 82 public void setName(String name) { 83 this.name = name; 84 } 85 86 public void setValue(String value){ 87 this.value = value; 88 } 89 90 public void setPath(String pathStr) { 91 this.path = Path.parse(pathStr); 92 } 93 94 public void execute(Registry reg) throws BuildException { 95 if(path == null){ 96 throw new BuildException("Path not set on checkProperty"); 97 } 98 99 Node node = reg.getRoot().getChild(path); 100 if(node == null){ 101 throw new BuildException("No node for path: " + path); 102 } 103 104 execute(node); 105 } 106 107 public void execute(Node node) throws BuildException{ 108 if(name == null){ 109 throw new BuildException("Name not set on checkProperty"); 110 } 111 112 Property prop = node.renderProperty(name); 113 if(prop.isNull()){ 114 throw new BuildException("No property found for: " + name + " on node: " + node.getAbsolutePath()); 115 } 116 117 if(value != null){ 118 if(!prop.asString().equals(value)){ 119 throw new BuildException("Expected " + value + 120 " for property " + name + " on node: " + node.getAbsolutePath() + "; got: " + prop.asString()); 121 } 122 } 123 } 124 } 125 126 public static final class CheckNode implements Check{ 127 128 private Path path; 129 private int childCount = -1; 130 private List checks = new ArrayList (); 131 132 public void setPath(String pathStr){ 133 path = Path.parse(pathStr); 134 } 135 136 public CheckProperty createCheckProperty(){ 137 CheckProperty prop = new CheckProperty(); 138 checks.add(prop); 139 return prop; 140 } 141 142 public CheckNode createCheckNode(){ 143 CheckNode check = new CheckNode(); 144 checks.add(check); 145 return check; 146 } 147 148 public void execute(Registry reg) throws BuildException { 149 if(path == null){ 150 throw new BuildException("Path not set on checkNode"); 151 } 152 153 Node node = reg.getRoot().getChild(path); 154 if(node == null){ 155 throw new BuildException("No node for path: " + path); 156 } 157 158 for(int i = 0; i < checks.size(); i++){ 159 Check prop = (Check)checks.get(i); 160 if(prop instanceof CheckNode){ 161 Path childPath = ((CheckNode)prop).path; 162 if(childPath == null){ 163 throw new BuildException("Path not set on child checkNode of " + path); 164 } 165 Node child = node.getChild(childPath); 166 if(child == null){ 167 throw new BuildException("No child node found for " + childPath + " under node: " + path); 168 } 169 prop.execute(child); 170 } 171 else{ 172 prop.execute(node); 173 } 174 } 175 } 176 177 public void execute(Node node) throws BuildException{ 178 if(childCount > -1){ 179 int count = node.getChildren().size(); 180 if(count != childCount){ 181 throw new BuildException("Expected " + childCount + 182 " child nodes for " + node.getAbsolutePath() + "; got: " + count); 183 } 184 } 185 } 186 187 public void setChildCount(int childCount) { 188 this.childCount = childCount; 189 } 190 191 public void setPath(Path path) { 192 this.path = path; 193 } 194 } 195 196 } 197 | Popular Tags |