use of org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty in project cas by apereo.
the class AddPropertiesToConfigurationCommand method add.
/**
* Add properties to configuration.
*
* @param file the file
* @param group the group
* @throws Exception the exception
*/
@CliCommand(value = "add-properties", help = "Add properties associated with a CAS group/module to a Properties/Yaml configuration file.")
public void add(@CliOption(key = { "file" }, help = "Path to the CAS configuration file", unspecifiedDefaultValue = "/etc/cas/config/cas.properties", specifiedDefaultValue = "/etc/cas/config/cas.properties", optionContext = "Path to the CAS configuration file") final String file, @CliOption(key = { "group" }, specifiedDefaultValue = "", unspecifiedDefaultValue = "", help = "Group/module whose associated settings should be added to the CAS configuration file", optionContext = "Group/module whose associated settings should be added to the CAS configuration file", mandatory = true) final String group) throws Exception {
if (StringUtils.isBlank(file)) {
LOGGER.warn("Configuration file must be specified");
return;
}
final File filePath = new File(file);
if (filePath.exists() && (filePath.isDirectory() || !filePath.canRead() || !filePath.canWrite())) {
LOGGER.warn("Configuration file [{}] is not readable/writable or is not a path to a file", filePath.getCanonicalPath());
return;
}
final Map<String, ConfigurationMetadataProperty> results = findProperties(group);
LOGGER.info("Located [{}] properties matching [{}]", results.size(), group);
switch(FilenameUtils.getExtension(filePath.getName()).toLowerCase()) {
case "properties":
createConfigurationFileIfNeeded(filePath);
final Properties props = loadPropertiesFromConfigurationFile(filePath);
writeConfigurationPropertiesToFile(filePath, results, props);
break;
case "yml":
createConfigurationFileIfNeeded(filePath);
final Properties yamlProps = loadYamlPropertiesFromConfigurationFile(filePath);
writeYamlConfigurationPropertiesToFile(filePath, results, yamlProps);
break;
default:
LOGGER.warn("Configuration file format [{}] is not recognized", filePath.getCanonicalPath());
}
}
use of org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty in project cas by apereo.
the class ConfigurationMetadataGenerator method processHints.
private Set<ConfigurationMetadataHint> processHints(final Collection<ConfigurationMetadataProperty> props, final Collection<ConfigurationMetadataProperty> groups) {
final Set<ConfigurationMetadataHint> hints = new LinkedHashSet<>();
for (final ConfigurationMetadataProperty entry : props) {
try {
final String propName = StringUtils.substringAfterLast(entry.getName(), ".");
final String groupName = StringUtils.substringBeforeLast(entry.getName(), ".");
final ConfigurationMetadataProperty grp = groups.stream().filter(g -> g.getName().equalsIgnoreCase(groupName)).findFirst().orElseThrow(() -> new IllegalArgumentException("Cant locate group " + groupName));
final Matcher matcher = PATTERN_GENERICS.matcher(grp.getType());
final String className = matcher.find() ? matcher.group(1) : grp.getType();
final Class clazz = ClassUtils.getClass(className);
final ConfigurationMetadataHint hint = new ConfigurationMetadataHint();
hint.setName(entry.getName());
if (clazz.isAnnotationPresent(RequiresModule.class)) {
final RequiresModule annotation = Arrays.stream(clazz.getAnnotations()).filter(a -> a.annotationType().equals(RequiresModule.class)).findFirst().map(RequiresModule.class::cast).get();
final ValueHint valueHint = new ValueHint();
valueHint.setValue(Stream.of(RequiresModule.class.getName(), annotation.automated()).collect(Collectors.toList()));
valueHint.setDescription(annotation.name());
hint.getValues().add(valueHint);
}
final boolean foundRequiredProperty = StreamSupport.stream(RelaxedNames.forCamelCase(propName).spliterator(), false).map(n -> ReflectionUtils.findField(clazz, n)).anyMatch(f -> f != null && f.isAnnotationPresent(RequiredProperty.class));
if (foundRequiredProperty) {
final ValueHint valueHint = new ValueHint();
valueHint.setValue(RequiredProperty.class.getName());
hint.getValues().add(valueHint);
}
if (!hint.getValues().isEmpty()) {
hints.add(hint);
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
return hints;
}
use of org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty in project cas by apereo.
the class ConfigurationMetadataGenerator method execute.
/**
* Execute.
*
* @throws Exception the exception
*/
public void execute() throws Exception {
final File jsonFile = new File(buildDir, "classes/java/main/META-INF/spring-configuration-metadata.json");
final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final TypeReference<Map<String, Set<ConfigurationMetadataProperty>>> values = new TypeReference<Map<String, Set<ConfigurationMetadataProperty>>>() {
};
final Map<String, Set> jsonMap = mapper.readValue(jsonFile, values);
final Set<ConfigurationMetadataProperty> properties = jsonMap.get("properties");
final Set<ConfigurationMetadataProperty> groups = jsonMap.get("groups");
final Set<ConfigurationMetadataProperty> collectedProps = new HashSet<>();
final Set<ConfigurationMetadataProperty> collectedGroups = new HashSet<>();
properties.stream().filter(p -> NESTED_TYPE_PATTERN.matcher(p.getType()).matches()).forEach(Unchecked.consumer(p -> {
final Matcher matcher = NESTED_TYPE_PATTERN.matcher(p.getType());
final boolean indexBrackets = matcher.matches();
final String typeName = matcher.group(1);
final String typePath = buildTypeSourcePath(typeName);
parseCompilationUnit(collectedProps, collectedGroups, p, typePath, typeName, indexBrackets);
}));
properties.addAll(collectedProps);
groups.addAll(collectedGroups);
final Set<ConfigurationMetadataHint> hints = processHints(properties, groups);
jsonMap.put("properties", properties);
jsonMap.put("groups", groups);
jsonMap.put("hints", hints);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
final PrettyPrinter pp = new DefaultPrettyPrinter();
mapper.writer(pp).writeValue(jsonFile, jsonMap);
}
use of org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty in project cas by apereo.
the class CasConfigurationMetadataServerController method search.
/**
* Search for property.
*
* @param name the name
* @return the response entity
*/
@GetMapping(path = "/search")
public ResponseEntity<List<ConfigurationMetadataSearchResult>> search(@RequestParam(value = "name", required = false) final String name) {
List results = new ArrayList<>();
final Map<String, ConfigurationMetadataProperty> allProps = repository.getRepository().getAllProperties();
if (StringUtils.isNotBlank(name) && RegexUtils.isValidRegex(name)) {
final String names = StreamSupport.stream(RelaxedNames.forCamelCase(name).spliterator(), false).map(Object::toString).collect(Collectors.joining("|"));
final Pattern pattern = RegexUtils.createPattern(names);
results = allProps.entrySet().stream().filter(propEntry -> RegexUtils.find(pattern, propEntry.getKey())).map(propEntry -> new ConfigurationMetadataSearchResult(propEntry.getValue(), repository)).collect(Collectors.toList());
Collections.sort(results);
}
return ResponseEntity.ok(results);
}
use of org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty in project cas by apereo.
the class FindPropertiesCommand method find.
/**
* Find.
*
* @param strict the strict
* @param propertyPattern the property pattern
* @return the map
*/
public Map<String, ConfigurationMetadataProperty> find(final boolean strict, final Pattern propertyPattern) {
final Map<String, ConfigurationMetadataProperty> results = new LinkedHashMap<>();
final CasConfigurationMetadataRepository repository = new CasConfigurationMetadataRepository();
final Map<String, ConfigurationMetadataProperty> props = repository.getRepository().getAllProperties();
props.forEach((k, v) -> {
final boolean matched = StreamSupport.stream(RelaxedNames.forCamelCase(k).spliterator(), false).map(Object::toString).anyMatch(name -> strict ? RegexUtils.matches(propertyPattern, name) : RegexUtils.find(propertyPattern, name));
if (matched) {
results.put(k, v);
}
});
return results;
}
Aggregations