use of org.springframework.context.annotation.Configuration in project camel by apache.
the class SpringBootAutoConfigurationMojo method writeAdditionalSpringMetaData.
private void writeAdditionalSpringMetaData(String prefix, String type, String name) throws MojoFailureException {
String fullQualifiedName = prefix + "." + type + "." + name + "." + "enabled";
String fileName = "META-INF/additional-spring-configuration-metadata.json";
File target = new File(SpringBootHelper.starterResourceDir(baseDir, project.getArtifactId()), fileName);
deleteFileOnMainArtifact(target);
try {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, Object> map = null;
List<Map<String, Object>> properties = null;
if (target.exists()) {
BufferedReader br = new BufferedReader(new FileReader(target));
map = gson.fromJson(br, Map.class);
properties = (List<Map<String, Object>>) map.get("properties");
if (properties != null && properties.stream().anyMatch(m -> fullQualifiedName.equals(m.get("name")))) {
getLog().debug("No changes to existing file: " + target);
return;
}
}
Map<String, Object> meta = new HashMap();
meta.put("name", fullQualifiedName);
meta.put("type", "java.lang.Boolean");
meta.put("defaultValue", true);
meta.put("description", "Enable " + name + " " + type);
if (properties == null) {
properties = new ArrayList<>(1);
}
if (map == null) {
map = new HashMap();
}
properties.add(meta);
map.put("properties", properties);
FileUtils.write(target, gson.toJson(map));
} catch (Exception e) {
throw new MojoFailureException("IOError with file " + target, e);
}
}
use of org.springframework.context.annotation.Configuration in project camel by apache.
the class SpringBootAutoConfigurationMojo method createComponentAutoConfigurationSource.
private void createComponentAutoConfigurationSource(String packageName, ComponentModel model, List<String> componentAliases, boolean hasOptions, String overrideComponentName) throws MojoFailureException {
final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
int pos = model.getJavaType().lastIndexOf(".");
String name = model.getJavaType().substring(pos + 1);
name = name.replace("Component", "ComponentAutoConfiguration");
javaClass.setPackage(packageName).setName(name);
String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
javaClass.getJavaDoc().setFullText(doc);
javaClass.addAnnotation(Configuration.class);
javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration");
if (hasOptions) {
AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
ann.setLiteralValue("value", configurationName + ".class");
javaClass.addImport("java.util.HashMap");
javaClass.addImport("java.util.Map");
javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
}
javaClass.addImport(model.getJavaType());
javaClass.addImport("org.apache.camel.CamelContext");
// add method for auto configure
String body = createComponentBody(model.getShortJavaType(), hasOptions);
String methodName = "configure" + model.getShortJavaType();
MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(model.getShortJavaType()).addThrows(Exception.class);
method.addParameter("CamelContext", "camelContext");
if (hasOptions) {
method.addParameter(configurationName, "configuration");
}
// Determine all the aliases
String[] springBeanAliases = componentAliases.stream().map(alias -> alias + "-component").toArray(size -> new String[size]);
method.addAnnotation(Lazy.class);
method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
// Generate Condition
javaClass.addNestedType(createConditionType(javaClass, "camel.component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US)));
sortImports(javaClass);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName);
writeAdditionalSpringMetaData("camel", "component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US));
}
use of org.springframework.context.annotation.Configuration in project cas by apereo.
the class CasCoreBootstrapStandaloneConfiguration method loadSettingsFromConfigurationSources.
private void loadSettingsFromConfigurationSources(final Environment environment, final Properties props, final File config) {
final List<String> profiles = new ArrayList<>();
profiles.add(configurationPropertiesEnvironmentManager().getApplicationName());
profiles.addAll(Arrays.stream(environment.getActiveProfiles()).collect(Collectors.toList()));
final String propertyNames = profiles.stream().collect(Collectors.joining("|"));
final String regex = String.format("(%s|application)\\.(yml|properties)", propertyNames);
LOGGER.debug("Looking for configuration files at [{}] that match the pattern [{}]", config, regex);
final Collection<File> configFiles = FileUtils.listFiles(config, new RegexFileFilter(regex, IOCase.INSENSITIVE), TrueFileFilter.INSTANCE).stream().sorted(Comparator.comparing(File::getName)).collect(Collectors.toList());
LOGGER.info("Configuration files found at [{}] are [{}]", config, configFiles);
configFiles.forEach(Unchecked.consumer(f -> {
LOGGER.debug("Loading configuration file [{}]", f);
if (f.getName().toLowerCase().endsWith("yml")) {
final Map pp = loadYamlProperties(new FileSystemResource(f));
LOGGER.debug("Found settings [{}] in YAML file [{}]", pp.keySet(), f);
props.putAll(pp);
} else {
final Properties pp = new Properties();
pp.load(new FileReader(f));
LOGGER.debug("Found settings [{}] in file [{}]", pp.keySet(), f);
props.putAll(pp);
}
}));
}
Aggregations