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;
}
Aggregations