1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import com.sun.enterprise.config.ConfigFactory; 28 import com.sun.enterprise.config.ConfigContext; 29 import com.sun.enterprise.config.ConfigException; 30 import com.sun.enterprise.config.ConfigBean; 31 import com.sun.enterprise.config.ConfigContextEvent; 32 33 34 import com.sun.enterprise.config.serverbeans.Config; 35 import com.sun.enterprise.config.serverbeans.Domain; 36 import com.sun.enterprise.config.serverbeans.Resources; 37 import com.sun.enterprise.config.serverbeans.Applications; 38 import com.sun.enterprise.config.serverbeans.Configs; 39 import com.sun.enterprise.config.serverbeans.HttpService; 40 import com.sun.enterprise.config.serverbeans.IiopService; 41 import com.sun.enterprise.config.serverbeans.JmsService; 42 import com.sun.enterprise.config.serverbeans.Servers; 43 import com.sun.enterprise.config.serverbeans.Applications; 44 import com.sun.enterprise.config.serverbeans.AdminService; 45 import com.sun.enterprise.config.serverbeans.SecurityService; 46 48 import java.net.URL ; 49 50 import com.sun.enterprise.util.LocalStringManagerImpl; 52 import java.util.logging.Logger ; 53 import java.util.logging.Level ; 54 import com.sun.logging.LogDomains; 55 56 import javax.xml.transform.Transformer ; 57 import javax.xml.transform.TransformerConfigurationException ; 58 import javax.xml.transform.TransformerException ; 59 import javax.xml.transform.TransformerFactory ; 60 import javax.xml.transform.stream.StreamResult ; 61 import javax.xml.transform.stream.StreamSource ; 62 63 import com.sun.enterprise.config.serverbeans.ConnectorConnectionPool; 65 66 import java.util.Vector ; 67 import java.io.*; 68 69 75 76 public class DomainXmlVerifier { 77 78 public ConfigContext configContext; 79 public DomainMgr domainMgr; 80 public String domainFile; 81 public boolean error; 82 public boolean debug = false; 83 public static int count = 0; 84 PrintStream _out; 85 86 static Logger _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER); 87 static { 88 StringManagerHelper.setLocalStringsManager(DomainXmlVerifier.class); 89 } 90 static LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager(); 91 public DomainXmlVerifier(String file) throws Exception { 92 this(file, System.out); 93 } 94 public DomainXmlVerifier(String file, PrintStream out) throws Exception { 95 _out = out; 96 domainFile = file; 97 configContext = ConfigFactory.createConfigContext(file); 98 configContext.refresh(); 99 domainMgr = new DomainMgr(configContext, true); 100 error = false; 101 } 102 103 public DomainMgr getDomainMgr() { 104 return domainMgr; 105 } 106 107 public ConfigContext getConfigContext() { 108 return configContext; 109 } 110 111 public void setDebug(boolean flag) { 112 debug = flag; 113 } 114 115 public void output(Result result) { 116 String element = result.getAssertion(); 117 String key = result.getTestName(); 118 if(!result.getErrorDetails().toString().equals("[]")) { 119 error = true; 120 out("Element : " + element); 121 if(key != null) 122 out("Key : " + key); 123 Vector error = result.getErrorDetails(); 124 for(int i=0;i<error.size();i++) { 125 if(i==0) 126 out("Error : " + error.get(i)); 127 else if(i>0) 128 out(" " + error.get(i)); 129 } 130 out(""); 131 count++; } 133 } 134 135 public void check(ConfigBean configBean) { 136 ConfigContextEvent cce = new ConfigContextEvent(getConfigContext(), ConfigContextEvent.PRE_ADD_CHANGE,configBean.toString(),configBean,"VALIDATE"); 137 cce.setClassObject(configBean.parent()); 138 Result result = domainMgr.check(cce); 139 if(result != null) 140 output(result); 141 } 142 143 public void checkUnique(ConfigBean configBean) { 144 145 try { 146 147 Domain domain = (Domain)configContext.getRootConfigBean(); 148 149 Resources resource = domain.getResources(); 151 checkDuplicate("connector-connection-pool", resource.getConnectorConnectionPool(), "name"); 154 ConnectorConnectionPool[] connpool = resource.getConnectorConnectionPool(); 155 for(int i=0;i<connpool.length;i++) 156 checkDuplicate("security-map", connpool[i].getSecurityMap(), "name"); 157 158 166 Applications applications = domain.getApplications(); 168 175 Configs configs = domain.getConfigs(); 177 checkDuplicate("config", configs.getConfig(), "name"); 178 Config[] config = configs.getConfig(); 179 for(int i=0;i<config.length;i++) { 180 181 HttpService httpservice = config[i].getHttpService(); 182 checkDuplicate("http-listener",httpservice.getHttpListener(), "id"); 184 checkDuplicate("virtual-server", httpservice.getVirtualServer(), "id"); 186 187 IiopService iiopservice = config[i].getIiopService(); 188 checkDuplicate("iiop-listener",iiopservice.getIiopListener(), "id"); 189 AdminService adminservice = config[i].getAdminService(); 190 checkDuplicate("jmx-connector",adminservice.getJmxConnector(), "name"); 191 192 JmsService jmsservice = config[i].getJmsService(); 193 checkDuplicate("jms-host",jmsservice.getJmsHost(), "name"); 194 195 SecurityService securityservice = config[i].getSecurityService(); 196 checkDuplicate("audit-module", securityservice.getAuditModule(), "name"); 197 checkDuplicate("auth-realm", securityservice.getAuthRealm(), "name"); 198 checkDuplicate("jacc-provider", securityservice.getJaccProvider(), "name"); 199 } 200 201 Servers servers = domain.getServers(); 202 checkDuplicate("server", servers.getServer(), "name"); 203 204 } catch(Exception e) { 205 e.printStackTrace(); 206 } 207 208 } 209 210 public void checkDuplicate(String elementName, ConfigBean[] cb, String attr) { 211 Result result = new Result(); 212 result.setAssertion(elementName); 213 for(int i=0;i<cb.length-1;i++) { 214 String name=cb[i].getAttributeValue(attr); 215 for(int j=i+1;j<cb.length;j++) { 216 if(name.equals(cb[j].getAttributeValue(attr))) { 217 result.failed("Duplicate Element : " + cb[j] + "(" + attr + "=" + name + ")"); 218 output(result); 219 } 220 } 221 } 222 } 223 224 225 public void preOrder(ConfigBean configBean) { 226 if(configBean != null) { 227 check(configBean); 228 ConfigBean configBean1[] = configBean.getAllChildBeans(); 229 if(configBean1 != null) { 230 for(int j=0;j<configBean1.length;j++) { 231 if(configBean1[j] != null) 232 preOrder(configBean1[j]); 233 } 234 } 235 } 236 } 237 238 public void out(String out) { 239 if(count < 50) 240 _out.println(out); 241 else if(count >= 50 && debug) 242 _out.println(out); 243 } 244 245 public boolean validate() { 246 try { 247 checkUnique(configContext.getRootConfigBean()); 248 preOrder(configContext.getRootConfigBean()); 249 if(!error) 250 _out.println("All Tests Passed, domain.xml is valid"); 251 } catch(Exception e) { 252 e.printStackTrace(); 253 } 254 return error; 255 } 256 257 public boolean invokeConfigValidator() { 258 boolean failed = false; 259 try { 260 failed = validate(); 261 } catch(Exception e) { 262 e.printStackTrace(); 263 } 264 return failed; 265 } 266 267 public static void main(String args[]) throws ConfigException { 268 269 int i=0; 271 if(args.length < 1) { 272 System.out.println("Usage : verify-domain-xml [--verbose] domain_xml_file"); 273 return; 274 } 275 276 boolean isDebug = false; 277 if(args[i].startsWith("--verbose")) { 279 if(args.length == 1) { 280 System.out.println("Usage : verify-domain-xml [--verbose] domain_xml_file"); 281 return; 282 } 283 isDebug = true; 284 i++; 285 } 286 287 File f = new File(args[i]); 289 if(!f.exists()) { 290 System.out.println("Error Config file: " + args[i] + " is not found"); 292 return; 293 } 294 String file = args[i]; 295 boolean fileCheck = false; 296 boolean classCheck = false; 297 298 try { 299 DomainXmlVerifier validator = new DomainXmlVerifier(file); 300 if(isDebug) 301 validator.debug = true; 302 validator.invokeConfigValidator(); 312 } catch (ConfigException ce) { 313 Throwable cause = ce.getCause(); 314 while(cause!=null && !(cause instanceof org.xml.sax.SAXParseException )) 315 cause = cause.getCause(); 316 if(cause!=null) 317 System.out.println("XML: "+cause.getMessage()); 318 else 319 ce.printStackTrace(); 320 } catch (Exception e) { 321 e.printStackTrace(); 322 } 323 } 324 } 325 | Popular Tags |