Search in sources :

Example 1 with Validator

use of org.opennms.netmgt.poller.remote.metadata.MetadataField.Validator in project opennms by OpenNMS.

the class MetadataFieldReader method getMetadataFields.

public Set<MetadataField> getMetadataFields() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    final Set<MetadataField> fields = new LinkedHashSet<>();
    if (m_propertyFile.exists() && m_propertyFile.canRead()) {
        final Properties props = new LinkedProperties();
        Reader r = null;
        try {
            r = new FileReader(m_propertyFile);
            props.load(r);
            final Set<String> names = new LinkedHashSet<>();
            for (final Entry<Object, Object> entry : props.entrySet()) {
                LOG.debug("{}={}", entry.getKey(), entry.getValue());
                if (entry.getKey() != null) {
                    final String key = entry.getKey().toString().trim();
                    if (key.endsWith(".description")) {
                        names.add(key.replaceAll("\\.description$", ""));
                    } else if (key.endsWith(".validator")) {
                        names.add(key.replaceAll("\\.validator$", ""));
                    } else if (key.endsWith(".required")) {
                        names.add(key.replaceAll("\\.required$", ""));
                    } else {
                        LOG.debug("Unknown metadata entry: {}", key);
                    }
                }
            }
            for (final String name : names) {
                final String description = props.getProperty(name + ".description");
                final String validatorClass = props.getProperty(name + ".validator");
                final String requiredString = props.getProperty(name + ".required");
                @SuppressWarnings("unchecked") final Class<Validator> validator = validatorClass == null ? null : (Class<Validator>) Class.forName(validatorClass);
                final Boolean required = Boolean.valueOf(requiredString);
                fields.add(new MetadataField(name, description, validator == null ? null : validator.newInstance(), required));
            }
        } catch (final IOException e) {
            LOG.warn("Failed to get metadata fields.", e);
        } finally {
            IOUtils.closeQuietly(r);
        }
    }
    return fields;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Reader(java.io.Reader) FileReader(java.io.FileReader) IOException(java.io.IOException) Properties(java.util.Properties) FileReader(java.io.FileReader) Validator(org.opennms.netmgt.poller.remote.metadata.MetadataField.Validator)

Aggregations

FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 Validator (org.opennms.netmgt.poller.remote.metadata.MetadataField.Validator)1