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

DomainMgr

public class DomainMgr extends Object implements com.sun.enterprise.config.ConfigContextEventListener
Class which loads all the validator descriptor information from a xml file into a hash map. Validator uses this Hash Map and invokes the particular test case depending on xml tag
author
Srinivas Krishnan
version
2.0

Fields Summary
static Logger
_logger
com.sun.enterprise.util.LocalStringManagerImpl
smh
HashMap
tests
private transient long
lastModified
public NameListMgr
_nameListMgr
public com.sun.enterprise.admin.meta.MBeanRegistry
_mbeanRegistry
public static final String
TEST_PACKAGE
Constructors Summary
public DomainMgr()

      
        this(MBeanRegistryFactory.getAdminContext().getAdminConfigContext(), false);
    
public DomainMgr(com.sun.enterprise.config.ConfigContext ctx, boolean bStaticContext)

        this(ctx, bStaticContext, null);
    
public DomainMgr(com.sun.enterprise.config.ConfigContext ctx, boolean bStaticContext, com.sun.enterprise.admin.meta.MBeanRegistry registry)

        loadDescriptors();
        _nameListMgr = new NameListMgr(ctx, bStaticContext);
        if(registry==null)
            _mbeanRegistry = MBeanRegistryFactory.getAdminMBeanRegistry();
        else
            _mbeanRegistry = registry;
    
Methods Summary
public Resultcheck(com.sun.enterprise.config.ConfigContextEvent cce)

        String name = cce.getName();
        String beanName = cce.getBeanName();
        Result result = null;


        if(name == null && beanName == null)
                return result;
        
        DomainCheck validator = (DomainCheck) tests.get(name);
        if(validator == null && beanName != null)
            validator = (DomainCheck) tests.get(beanName);
        try {
            if(validator != null)
                result = validator.validate(cce);
        } catch(Exception e) {
//System.out.println("+++++name="+name + " xpath=" + ((ConfigBean)cce.getObject()).getXPath());
            _logger.log(Level.WARNING, "domainxmlverifier.error_on_validation", e);
        }
        return result;
    
GenericValidatorfindConfigBeanValidator(com.sun.enterprise.config.ConfigBean configBean)

        
        if(configBean!=null)
        {
            String className = configBean.getClass().getName();
            int iLastDot = className.lastIndexOf('.");
            if(iLastDot>0)
               return (GenericValidator)tests.get(className.substring(iLastDot+1));
        }
        return null;
    
ValidationDescriptorfindValidationDescriptor(java.lang.String beanName)

        if(beanName!=null)
        {
            GenericValidator genVal = (GenericValidator)tests.get(beanName);
            if(genVal!=null)
                return genVal.desc;
        }
        return null;
    
private java.lang.StringgetAttr(org.w3c.dom.NamedNodeMap nodeMap, java.lang.String attrName)

        Node node = nodeMap.getNamedItem(attrName);
        if(node != null)
            return node.getNodeValue();
        return null;
    
private java.lang.String[]getAttrAsList(org.w3c.dom.NamedNodeMap nodeMap, java.lang.String attrName)

        String attrValue = getAttr(nodeMap, attrName);
        if(attrValue==null)
            return null;
        return attrValue.split(",");
    
GenericValidatorgetGenericValidator(ValidationDescriptor v)
Get the generic validator to be used for the given validation descriptor

param
v the validation descriptor
return
a GenericValidator instance that is to be used to validate elements which match the ValidationDescription

        final Class c = getValidatorClass(v.getCustomValidatorClass());
        try {
            final Constructor con = c.getConstructor(new Class[]{ValidationDescriptor.class});
            return (GenericValidator) con.newInstance(new Object[]{v});
        }
        catch (NoSuchMethodException e){
            return null;
        }
        
    
com.sun.enterprise.admin.meta.MBeanRegistrygetMBeanRegistry()

        return _mbeanRegistry;
    
private java.lang.StringgetTestFile()

        URL propertiesFile = DomainMgr.class.getClassLoader().getResource(
            "com/sun/enterprise/config/serverbeans/validation/config/ServerTestList.xml");
//TODO: for test only
//  URL propertiesFile = (new File("/ias/admin/validator/descr.xml")).toURL();
        return propertiesFile.toString();
    
java.lang.ClassgetValidatorClass(java.lang.String className)
Find a class that will perform validation for the given class name. The class returned is the first that can be loaded from the following ordered list:
  1. The class as specified by the className param
  2. The class as specified by the className param, prepended with the TEST_PACKAGE package name.
  3. The GenericValidator class
  4. Setting the log level to CONFIG will provide logging information as to which class was actually found and loaded.

    param
    className the name of the class for which a validator class is to be found
    return
    the class which is to be used for validation

            Class c;
            final String cn = TEST_PACKAGE+className+"Test";
                try {
                c= Class.forName(cn);
            }
            catch (ClassNotFoundException cnfe2){
                c = GenericValidator.class;
            }
            _logger.log(Level.CONFIG, "validator using class \""+c.getName()+"\" to validate \""+cn+"\"");
            return c;
        
public booleanloadDescriptors()

        boolean allIsWell = true;
       
        try {
            //tests.clear();
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource is = new InputSource(getTestFile());
            Document doc = db.parse(is);
            NodeList list = doc.getElementsByTagName("element");
            for (int i=0;i<list.getLength();i++) {
                Element e = (Element) list.item(i);
                String elementName  =  e.getAttribute("name");
                String elementXPath =  e.getAttribute("xpath");
                String elementCustomClass = e.getAttribute("custom-class") ;
                String testName = e.getAttribute("test-name");
                if(testName==null || testName.length()==0)
                {
                    testName = XPathHelper.convertName(elementName);
                }
                if (null == elementCustomClass || elementCustomClass.length() == 0){
                    elementCustomClass = testName;
                }
                String[] required_children = null;
                String[] exclusive_list    = null;
                String elemList = e.getAttribute("required-children");
                if(elemList!=null && elemList.length()>0)
                {
                    required_children = elemList.split(",");
                }
                elemList = e.getAttribute("exclusive-list");
                if(elemList!=null && elemList.length()>0)
                {
                    exclusive_list = elemList.split(",");
                }
                String key = e.getAttribute("key");
                if(key!=null && key.length()==0)
                    key = null;
                Vector attributes = new Vector();
                NodeList nl = e.getChildNodes();
                for (int index=0,j=0;j<nl.getLength();j++) {
                    String temp;
                    String nodeName = nl.item(j).getNodeName().trim();
                    String nameValue=null;
                    String typeValue=null;
                    NamedNodeMap nodeMap=null;
                    AttrType attr=null;
                   
                    Node n = nl.item(j);
                    
                    if("attribute".equals(nodeName) || "optional-attribute".equals(nodeName)) {
                        nodeMap = n.getAttributes();

                        nameValue   = getAttr(nodeMap, "name");
                        typeValue   = getAttr(nodeMap, "type");
                        if("string".equals(typeValue)) 
                        {
                            attr = new AttrString(nameValue, typeValue, "optional-attribute".equals(nodeName) );
                            if((temp=getAttr(nodeMap, "max-length"))!=null)
                               ((AttrString)attr).setMaxLength(Integer.parseInt(temp));
                            if((temp=getAttr(nodeMap, "enumeration"))!=null)
                            {
                                Vector ee = new Vector();
                                String[] strs = temp.split(",");
                                for(int k=0; k<strs.length; k++)
                                    ee.add(strs[k]);
                                ((AttrString)attr).setEnumstring(ee);
                            }
                            ((AttrString)attr).setRegExpression(getAttr(nodeMap, "regex"));
                        }
                        else if("file".equals(typeValue)) 
                        {
                            attr = new AttrFile(nameValue, typeValue, "optional-attribute".equals(nodeName));
                            if("true".equals(getAttr(nodeMap, "exists")))
                                ((AttrFile)attr).setCheckExists(true);
                        }
                        else if("integer".equals(typeValue))
                        {
                            attr = new AttrInt(nameValue,typeValue, "optional-attribute".equals(nodeName));
                            if((temp = getAttr(nodeMap, "range")) != null) 
                            {
                                String[] strs = temp.split(",");
                                if(!strs[0].equals("NA"))
                                    ((AttrInt)attr).setLowRange(Integer.parseInt(strs[0]));
                                if(!strs[1].equals("NA"))
                                    ((AttrInt)attr).setHighRange(Integer.parseInt(strs[1]));
                            }
                        }
                        else if("classname".equals(typeValue))
                            attr = new AttrClassName(nameValue, typeValue, "optional-attribute".equals(nodeName));
                        else if("address".equals(typeValue)) 
                            attr = new AttrAddress(nameValue,typeValue, "optional-attribute".equals(nodeName));
                        else if("jndi-unique".equals(typeValue)) 
                            attr = new AttrUniqueJNDI(nameValue,typeValue, "optional-attribute".equals(nodeName));

                        if(attr != null) 
                        {
                            attr.addRuleValue("belongs-to",     getAttrAsList(nodeMap, "belongs-to"));
                            attr.addRuleValue("references-to",  getAttrAsList(nodeMap, "references-to"));
                            attr.addRuleValue("le-than", getAttr(nodeMap, "le-than"));
                            attr.addRuleValue("ls-than", getAttr(nodeMap, "ls-than"));
                            attr.addRuleValue("ge-than", getAttr(nodeMap, "ge-than"));
                            attr.addRuleValue("gt-than", getAttr(nodeMap, "gt-than"));

                            attributes.add(index++,attr);
                        }
                    }
                }
                final ValidationDescriptor desc =
                       new ValidationDescriptor(this, elementName, 
                            elementXPath, elementCustomClass, 
                            key, attributes, required_children, exclusive_list);
                final GenericValidator validator= getGenericValidator(desc);
                if(validator != null) 
                    tests.put(testName, validator);
            }
        } catch (ParserConfigurationException e) {
            _logger.log(Level.WARNING, "parser_error", e);
            allIsWell = false;
        } catch (SAXException e) {
            _logger.log(Level.WARNING, "sax_error", e);
            allIsWell = false;     
        } catch (IOException e) {
            _logger.log(Level.WARNING, "error_loading_xmlfile", e);
            allIsWell = false;
        } catch(Exception e) {
            _logger.log(Level.WARNING, "error", e);
            allIsWell = false;
        }
        return allIsWell;
    
public voidpostAccessNotification(com.sun.enterprise.config.ConfigContextEvent ccce)

    
public voidpostChangeNotification(com.sun.enterprise.config.ConfigContextEvent ccce)

    
public voidpreAccessNotification(com.sun.enterprise.config.ConfigContextEvent ccce)

    
public voidpreChangeNotification(com.sun.enterprise.config.ConfigContextEvent cce)

        Result result = null;
        try{
           result = check(cce);
        } catch(Throwable t)
        {
            _logger.log(Level.WARNING, "Exception during validation ", t);
        }
        
        if(result != null && result.getStatus() == Result.FAILED)
        {
            _logger.log(Level.WARNING, "Validation error: " + result.getErrorDetails().toString());
            throw new AdminValidationException(result.getErrorDetailsAsString());
        }