XmlValidatorManager implements ValidatorManager { @Override public void loadValidators(final Props props, final Logger log) { this.validators = new LinkedHashMap<>(); if (!props.containsKey(ValidatorConfigs.XML_FILE_PARAM)) { logger.warn( "Azkaban properties file does not contain the key " + ValidatorConfigs.XML_FILE_PARAM); return; } final String xmlPath = props.get(ValidatorConfigs.XML_FILE_PARAM); final File file = new File(xmlPath); if (!file.exists()) { logger.error("Azkaban validator configuration file " + xmlPath + " does not exist."); return; } final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = docBuilderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { throw new ValidatorManagerException( "Exception while parsing validator xml. Document builder not created.", e); } Document doc = null; try { doc = builder.parse(file); } catch (final SAXException e) { throw new ValidatorManagerException("Exception while parsing " + xmlPath + ". Invalid XML.", e); } catch (final IOException e) { throw new ValidatorManagerException("Exception while parsing " + xmlPath + ". Error reading file.", e); } final NodeList tagList = doc.getChildNodes(); final Node azkabanValidators = tagList.item(0); final NodeList azkabanValidatorsList = azkabanValidators.getChildNodes(); for (int i = 0; i < azkabanValidatorsList.getLength(); ++i) { final Node node = azkabanValidatorsList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals(VALIDATOR_TAG)) { parseValidatorTag(node, props, log); } } } } XmlValidatorManager(final Props props); @Override void loadValidators(final Props props, final Logger log); @Override String getCacheKey(final Project project, final File projectDir, final Props props); @Override Map<String, ValidationReport> validate(final Project project, final File projectDir, final Props additionalProps); @Override List<String> getValidatorsInfo(); static final String VALIDATOR_TAG; static final String CLASSNAME_ATTR; static final String ITEM_TAG; }
@Test public void testLoadValidators() { final Props props = new Props(this.baseProps); final URL validatorUrl = Resources.getResource("project/testValidators"); final URL configUrl = Resources.getResource("test-conf/azkaban-validators-test2.xml"); props.put(ValidatorConfigs.VALIDATOR_PLUGIN_DIR, validatorUrl.getPath()); props.put(ValidatorConfigs.XML_FILE_PARAM, configUrl.getPath()); final XmlValidatorManager manager = new XmlValidatorManager(props); assertEquals("XmlValidatorManager should contain 1 validator.", manager.getValidatorsInfo().size(), 1); assertEquals( "XmlValidatorManager should contain the validator specified in the xml configuration file.", manager.getValidatorsInfo().get(0), "Test"); }