Search in sources :

Example 6 with Configuration

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);
    }
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Gson(com.google.gson.Gson) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with Configuration

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));
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Example 8 with Configuration

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);
        }
    }));
}
Also used : Arrays(java.util.Arrays) IOCase(org.apache.commons.io.IOCase) PropertySource(org.springframework.core.env.PropertySource) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) Map(java.util.Map) CasConfigurationPropertiesEnvironmentManager(org.apereo.cas.configuration.CasConfigurationPropertiesEnvironmentManager) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) RegexFileFilter(org.apache.commons.io.filefilter.RegexFileFilter) Resource(org.springframework.core.io.Resource) Unchecked(org.jooq.lambda.Unchecked) Logger(org.slf4j.Logger) ResourceLoader(org.springframework.core.io.ResourceLoader) Properties(java.util.Properties) Collection(java.util.Collection) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) FileSystemResource(org.springframework.core.io.FileSystemResource) FileUtils(org.apache.commons.io.FileUtils) Profile(org.springframework.context.annotation.Profile) Collectors(java.util.stream.Collectors) File(java.io.File) YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Environment(org.springframework.core.env.Environment) Bean(org.springframework.context.annotation.Bean) FileReader(java.io.FileReader) Comparator(java.util.Comparator) YamlProcessor(org.springframework.beans.factory.config.YamlProcessor) PropertySourceLocator(org.springframework.cloud.bootstrap.config.PropertySourceLocator) ArrayList(java.util.ArrayList) RegexFileFilter(org.apache.commons.io.filefilter.RegexFileFilter) FileReader(java.io.FileReader) FileSystemResource(org.springframework.core.io.FileSystemResource) Properties(java.util.Properties) File(java.io.File) Map(java.util.Map)

Aggregations

Bean (org.springframework.context.annotation.Bean)8 Configuration (org.springframework.context.annotation.Configuration)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 FileUtils (org.apache.commons.io.FileUtils)6 FileInputStream (java.io.FileInputStream)5 FileReader (java.io.FileReader)5 InputStream (java.io.InputStream)5 Collections (java.util.Collections)5 Locale (java.util.Locale)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 BufferedReader (java.io.BufferedReader)4 FileNotFoundException (java.io.FileNotFoundException)4 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4 IOException (java.io.IOException)3 Modifier (java.lang.reflect.Modifier)3