FileDocCategorySizeDatePackage
DomainXmlVerifier.javaAPI DocGlassfish v2 API13905Fri May 04 22:24:36 BST 2007com.sun.enterprise.config.serverbeans.validation

DomainXmlVerifier

public class DomainXmlVerifier extends Object
Class which invokes the config validator from Command Line
author
Srinivas Krishnan
version
2.0

Fields Summary
public com.sun.enterprise.config.ConfigContext
configContext
public DomainMgr
domainMgr
public String
domainFile
public boolean
error
public boolean
debug
public static int
count
PrintStream
_out
static Logger
_logger
static com.sun.enterprise.util.LocalStringManagerImpl
smh
Constructors Summary
public DomainXmlVerifier(String file)

         
        this(file, System.out);
    
public DomainXmlVerifier(String file, PrintStream out)

        _out = out;
        domainFile = file;
        configContext = ConfigFactory.createConfigContext(file);
        configContext.refresh();
        domainMgr = new DomainMgr(configContext, true);
        error = false;
    
Methods Summary
public voidcheck(com.sun.enterprise.config.ConfigBean configBean)

        ConfigContextEvent cce = new ConfigContextEvent(getConfigContext(), ConfigContextEvent.PRE_ADD_CHANGE,configBean.toString(),configBean,"VALIDATE");
        cce.setClassObject(configBean.parent());
        Result result = domainMgr.check(cce);
        if(result != null) 
            output(result);
    
public voidcheckDuplicate(java.lang.String elementName, com.sun.enterprise.config.ConfigBean[] cb, java.lang.String attr)

            Result result = new Result();
            result.setAssertion(elementName);
            for(int i=0;i<cb.length-1;i++) {
                        String name=cb[i].getAttributeValue(attr);
                        for(int j=i+1;j<cb.length;j++) {
                            if(name.equals(cb[j].getAttributeValue(attr))) {
                                result.failed("Duplicate Element : " + cb[j] + "(" + attr + "=" +  name + ")");
                                output(result); 
                            }
                        }
            }
    
public voidcheckUnique(com.sun.enterprise.config.ConfigBean configBean)


        try {
            
            Domain domain = (Domain)configContext.getRootConfigBean();
            
            // Resources 
            Resources resource = domain.getResources();
//            checkDuplicate("admin-object-resource", resource.getAdminObjectResource(), "jndi-name");
//            checkDuplicate("resource-adapter-config", resource.getResourceAdapterConfig(), "resource-adapter-name");
            checkDuplicate("connector-connection-pool", resource.getConnectorConnectionPool(), "name");
            ConnectorConnectionPool[] connpool = resource.getConnectorConnectionPool();
            for(int i=0;i<connpool.length;i++)
                checkDuplicate("security-map", connpool[i].getSecurityMap(), "name");

//            checkDuplicate("connector-resource", resource.getConnectorResource(), "jndi-name");
//            checkDuplicate("custom-resource", resource.getCustomResource(), "jndi-name");
//            checkDuplicate("external-jndi-resource", resource.getExternalJndiResource(), "jndi-name");
//            checkDuplicate("jdbc-connection-pool", resource.getJdbcConnectionPool(), "name");
//            checkDuplicate("jdbc-resource", resource.getJdbcResource(), "jndi-name");
//            checkDuplicate("mail-resource", resource.getMailResource(), "jndi-name");
//            checkDuplicate("persistence-manger-factory-resource", resource.getPersistenceManagerFactoryResource(), "jndi-name");
            
            //Applications
            Applications applications = domain.getApplications();
//            checkDuplicate("appclient-module",applications.getAppclientModule(),"name");
//            checkDuplicate("connector-module",applications.getConnectorModule(),"name");
//            checkDuplicate("ejb-module", applications.getEjbModule(),"name");
//            checkDuplicate("j2ee-application", applications.getJ2eeApplication(),"name");
//            checkDuplicate("lifecycle-module", applications.getLifecycleModule(),"name");
//            checkDuplicate("web-module", applications.getWebModule(),"name");
            
            // Configs
            Configs configs = domain.getConfigs();
            checkDuplicate("config", configs.getConfig(), "name");
            Config[] config = configs.getConfig();
            for(int i=0;i<config.length;i++) {
                
                HttpService httpservice = config[i].getHttpService();
                //checkDuplicate("acl",httpservice.getAcl(), "name");
                checkDuplicate("http-listener",httpservice.getHttpListener(), "id");
                //checkDuplicate("mime",httpservice.getMime(), "name");
                checkDuplicate("virtual-server", httpservice.getVirtualServer(), "id");
                
                IiopService iiopservice = config[i].getIiopService();
                checkDuplicate("iiop-listener",iiopservice.getIiopListener(), "id");
                AdminService adminservice = config[i].getAdminService();
                checkDuplicate("jmx-connector",adminservice.getJmxConnector(), "name");
                
                JmsService jmsservice = config[i].getJmsService();
                checkDuplicate("jms-host",jmsservice.getJmsHost(), "name");
                
                SecurityService securityservice = config[i].getSecurityService();
                checkDuplicate("audit-module", securityservice.getAuditModule(), "name");
                checkDuplicate("auth-realm", securityservice.getAuthRealm(), "name");
                checkDuplicate("jacc-provider", securityservice.getJaccProvider(), "name");
            }

            Servers servers = domain.getServers();
            checkDuplicate("server", servers.getServer(), "name");
            
        } catch(Exception e) {
            e.printStackTrace();
        }

    
public com.sun.enterprise.config.ConfigContextgetConfigContext()

        return configContext;
    
public DomainMgrgetDomainMgr()

        return domainMgr;
    
public booleaninvokeConfigValidator()

        boolean failed = false;
        try {
            failed =  validate();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return failed;
    
public static voidmain(java.lang.String[] args)

        
        // if no args supplied print the usage
        int i=0;
        if(args.length < 1) {
            System.out.println("Usage : verify-domain-xml [--verbose] domain_xml_file");
            return;
        }
        
        boolean isDebug = false;
        // if verbose set, set debug to true
        if(args[i].startsWith("--verbose")) {
            if(args.length == 1) {
                System.out.println("Usage : verify-domain-xml [--verbose]  domain_xml_file");
                return;
            }
            isDebug = true;
            i++;
        }
        
        // check if domain.xml path supplied exists
        File f = new File(args[i]); 
        if(!f.exists()) {   
            //TODO i18n
            System.out.println("Error Config file: " + args[i] + " is not found");
            return;
        }
        String file = args[i];
        boolean fileCheck = false;
        boolean classCheck = false;
        
        try {
            DomainXmlVerifier validator = new DomainXmlVerifier(file);
            if(isDebug)
                validator.debug = true;
            // do not check for file and class path
//            for(int j=0;j<args.length;j++) {
//                if(args[j].startsWith("--check-classpath"))
//                    classCheck = true;
//                if(args[j].startsWith("--check-filepath"))
//                    fileCheck = true;
//            }
//            StaticTest.fileCheck = fileCheck;
//            StaticTest.classPathCheck = classCheck;
            validator.invokeConfigValidator();
        } catch (ConfigException ce) {
            Throwable cause = ce.getCause();
            while(cause!=null && !(cause instanceof org.xml.sax.SAXParseException))
                cause = cause.getCause();
            if(cause!=null)
                System.out.println("XML: "+cause.getMessage());
            else
                ce.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
public voidout(java.lang.String out)

        if(count < 50)
            _out.println(out);
        else if(count >= 50 && debug) 
            _out.println(out);
    
public voidoutput(Result result)

        String element = result.getAssertion();
        String key = result.getTestName();
        if(!result.getErrorDetails().toString().equals("[]")) {
            error = true;
            out("Element  : " + element);
            if(key != null) 
                out("Key      : " + key);
            Vector error = result.getErrorDetails();
            for(int i=0;i<error.size();i++) {
                if(i==0) 
                    out("Error    : " + error.get(i));
                else if(i>0)
                    out("           " + error.get(i));
            }  
            out("");
            count++; // Number of test to be printed
        }
    
public voidpreOrder(com.sun.enterprise.config.ConfigBean configBean)

            if(configBean != null) {
                check(configBean);
                ConfigBean configBean1[] = configBean.getAllChildBeans();
                if(configBean1 != null) {
                    for(int j=0;j<configBean1.length;j++)  {
                           if(configBean1[j] != null)
                                preOrder(configBean1[j]);
                    }
                }
            }
    
public voidsetDebug(boolean flag)

        debug = flag;
    
public booleanvalidate()

        try {
            checkUnique(configContext.getRootConfigBean());
            preOrder(configContext.getRootConfigBean());
            if(!error)
                _out.println("All Tests Passed, domain.xml is valid");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return error;