Search in sources :

Example 1 with Conditional

use of org.springframework.context.annotation.Conditional in project zipkin by openzipkin.

the class ZipkinElasticsearchStorageConfiguration method dynamicCredentialsScheduledExecutorService.

@Bean(destroyMethod = "shutdown")
@Qualifier(QUALIFIER)
@Conditional(DynamicRefreshRequired.class)
ScheduledExecutorService dynamicCredentialsScheduledExecutorService(@Value("${" + CREDENTIALS_FILE + "}") String credentialsFile, @Value("${" + CREDENTIALS_REFRESH_INTERVAL + "}") Integer credentialsRefreshInterval, @Qualifier(QUALIFIER) BasicCredentials basicCredentials) throws IOException {
    ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("zipkin-load-es-credentials"));
    DynamicCredentialsFileLoader credentialsFileLoader = new DynamicCredentialsFileLoader(basicCredentials, credentialsFile);
    credentialsFileLoader.updateCredentialsFromProperties();
    ScheduledFuture<?> future = ses.scheduleAtFixedRate(credentialsFileLoader, 0, credentialsRefreshInterval, TimeUnit.SECONDS);
    if (future.isDone())
        throw new RuntimeException("credential refresh thread didn't start");
    return ses;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamedThreadFactory(io.micrometer.core.instrument.util.NamedThreadFactory) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with Conditional

use of org.springframework.context.annotation.Conditional in project vboard by voyages-sncf-technologies.

the class WebSecurityConfig method adapterDeploymentContext.

// Also injected in `KeycloakPreAuthActionsFilter` as the `deploymentContext` property through the `initFilterBean` method.
// !BEWARE! if this `deploymentContext` property ends up null, it will lead to a NullPointerException in `org.keycloak.adapters.PreAuthActionsHandler.preflightCors:107`
@Bean
@Conditional(KeycloakEnabledInEnv.class)
public static AdapterDeploymentContext adapterDeploymentContext() throws Exception {
    AdapterDeploymentContextFactoryBean factoryBean = new AdapterDeploymentContextFactoryBean(new KeycloakSpringBootConfigResolver());
    // creates the AdapterDeploymentContext
    factoryBean.afterPropertiesSet();
    return factoryBean.getObject();
}
Also used : KeycloakSpringBootConfigResolver(org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver) AdapterDeploymentContextFactoryBean(org.keycloak.adapters.springsecurity.AdapterDeploymentContextFactoryBean) Conditional(org.springframework.context.annotation.Conditional) AdapterDeploymentContextFactoryBean(org.keycloak.adapters.springsecurity.AdapterDeploymentContextFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with Conditional

use of org.springframework.context.annotation.Conditional in project flow by vaadin.

the class SpringBootAutoConfiguration method dispatcherServletRegistration.

/**
 * Creates a {@link ServletRegistrationBean} instance for a dispatcher
 * servlet in case Vaadin servlet is mapped to the root.
 * <p>
 * This is needed for correct servlet path (and path info) values available
 * in Vaadin servlet because it works via forwarding controller which is not
 * properly mapped without this registration.
 *
 * @return a custom ServletRegistrationBean instance for dispatcher servlet
 */
@Bean
@Conditional(RootMappedCondition.class)
public ServletRegistrationBean<DispatcherServlet> dispatcherServletRegistration() {
    DispatcherServlet servlet = context.getBean(DispatcherServlet.class);
    ServletRegistrationBean<DispatcherServlet> registration = new ServletRegistrationBean<>(servlet, "/*");
    registration.setName("dispatcher");
    return registration;
}
Also used : ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) Conditional(org.springframework.context.annotation.Conditional) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with Conditional

use of org.springframework.context.annotation.Conditional in project java-chassis by ServiceComb.

the class ConfigurationSpringInitializer method addMicroserviceYAMLToSpring.

/**
 * make springboot have a change to add microservice.yaml source earlier<br>
 * to affect {@link Conditional}
 * @param environment environment
 */
private static void addMicroserviceYAMLToSpring(Environment environment) {
    if (!(environment instanceof ConfigurableEnvironment)) {
        return;
    }
    MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
    if (propertySources.contains(MICROSERVICE_PROPERTY_SOURCE_NAME)) {
        return;
    }
    propertySources.addLast(new EnumerablePropertySource<MicroserviceConfigLoader>(MICROSERVICE_PROPERTY_SOURCE_NAME) {

        private final Map<String, Object> values = new HashMap<>();

        private final String[] propertyNames;

        {
            MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
            loader.loadAndSort();
            loader.getConfigModels().forEach(configModel -> values.putAll(YAMLUtil.retrieveItems("", configModel.getConfig())));
            propertyNames = values.keySet().toArray(new String[values.size()]);
        }

        @Override
        public String[] getPropertyNames() {
            return propertyNames;
        }

        @SuppressWarnings("unchecked")
        @Override
        public Object getProperty(String name) {
            Object value = this.values.get(name);
            // spring will not resolve nested placeholder of list, so try to fix the problem
            if (value instanceof List) {
                value = ((List<Object>) value).stream().filter(item -> item instanceof String).map(item -> environment.resolvePlaceholders((String) item)).collect(Collectors.toList());
            }
            return value;
        }
    });
}
Also used : Ordered(org.springframework.core.Ordered) DynamicConfigurationChangedEvent(org.apache.servicecomb.config.event.DynamicConfigurationChangedEvent) PropertySource(org.springframework.core.env.PropertySource) RefreshGovernanceConfigurationEvent(org.apache.servicecomb.config.event.RefreshGovernanceConfigurationEvent) BootStrapService(org.apache.servicecomb.foundation.bootstrap.BootStrapService) LoggerFactory(org.slf4j.LoggerFactory) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ConfigCenterConfigurationSource(org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource) YAMLUtil(org.apache.servicecomb.config.YAMLUtil) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Map(java.util.Map) PropertySourcesPlaceholderConfigurer(org.springframework.context.support.PropertySourcesPlaceholderConfigurer) Subscribe(com.google.common.eventbus.Subscribe) Properties(java.util.Properties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigurationManager(com.netflix.config.ConfigurationManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) CompositePropertySource(org.springframework.core.env.CompositePropertySource) List(java.util.List) EnvironmentAware(org.springframework.context.EnvironmentAware) Environment(org.springframework.core.env.Environment) MapPropertySource(org.springframework.core.env.MapPropertySource) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Conditional(org.springframework.context.annotation.Conditional) ConfigMapping(org.apache.servicecomb.config.ConfigMapping) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) List(java.util.List) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Aggregations

Conditional (org.springframework.context.annotation.Conditional)4 Bean (org.springframework.context.annotation.Bean)3 Subscribe (com.google.common.eventbus.Subscribe)1 ConfigurationManager (com.netflix.config.ConfigurationManager)1 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)1 WatchedUpdateResult (com.netflix.config.WatchedUpdateResult)1 NamedThreadFactory (io.micrometer.core.instrument.util.NamedThreadFactory)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Collectors (java.util.stream.Collectors)1 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ConfigMapping (org.apache.servicecomb.config.ConfigMapping)1 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)1