Java Code Examples for org.springframework.core.env.ConfigurableEnvironment#getActiveProfiles()

The following examples show how to use org.springframework.core.env.ConfigurableEnvironment#getActiveProfiles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringApplicationContextInitializer.java    From spring-music with Apache License 2.0 6 votes vote down vote up
private String[] getActiveProfile(ConfigurableEnvironment appEnvironment) {
    List<String> serviceProfiles = new ArrayList<>();

    for (String profile : appEnvironment.getActiveProfiles()) {
        if (validLocalProfiles.contains(profile)) {
            serviceProfiles.add(profile);
        }
    }

    if (serviceProfiles.size() > 1) {
        throw new IllegalStateException("Only one active Spring profile may be set among the following: " +
                validLocalProfiles.toString() + ". " +
                "These profiles are active: [" +
                StringUtils.collectionToCommaDelimitedString(serviceProfiles) + "]");
    }

    if (serviceProfiles.size() > 0) {
        return createProfileNames(serviceProfiles.get(0), "local");
    }

    return null;
}
 
Example 2
Source File: EnviromentDiscovery.java    From gazpachoquest with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext ctx) {
    ConfigurableEnvironment environment = ctx.getEnvironment();
    String activeProfiles[] = environment.getActiveProfiles();

    if (activeProfiles.length == 0) {
        environment.setActiveProfiles("test,db_hsql");
    }

    logger.info("Application running using profiles: {}", Arrays.toString(environment.getActiveProfiles()));

    String instanceInfoString = environment.getProperty(GAZPACHO_APP_KEY);

    String dbEngine = null;
    for (String profile : activeProfiles) {
        if (profile.startsWith("db_")) {
            dbEngine = profile;
            break;
        }
    }
    try {
        environment.getPropertySources().addLast(
                new ResourcePropertySource(String.format("classpath:/database/%s.properties", dbEngine)));
    } catch (IOException e) {
        throw new IllegalStateException(dbEngine + ".properties not found in classpath", e);
    }

    PropertySourcesPlaceholderConfigurer propertyHolder = new PropertySourcesPlaceholderConfigurer();

    Map<String, String> environmentProperties = parseInstanceInfo(instanceInfoString);
    if (!environmentProperties.isEmpty()) {
        logger.info("Overriding default properties with {}", instanceInfoString);
        Properties properties = new Properties();
        for (String key : environmentProperties.keySet()) {
            String value = environmentProperties.get(key);
            properties.put(key, value);
        }
        environment.getPropertySources().addLast(new PropertiesPropertySource("properties", properties));

        propertyHolder.setEnvironment(environment);
        // ctx.addBeanFactoryPostProcessor(propertyHolder);
        // ctx.refresh();
    }
}
 
Example 3
Source File: EtcdPropertySourceLocator.java    From spring-cloud-etcd with Apache License 2.0 5 votes vote down vote up
@Override
public PropertySource<?> locate(Environment environment) {
	if (environment instanceof ConfigurableEnvironment) {
		final ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
		final String[] profiles = env.getActiveProfiles();
		final List<String> contexts = new ArrayList<>();

		setupContext(contexts, profiles, this.properties.getPrefix(),
				this.properties.getDefaultContext());

		setupContext(contexts, profiles, this.properties.getPrefix(),
				env.getProperty(EtcdConstants.PROPERTY_SPRING_APPLICATION_NAME));

		CompositePropertySource composite = new CompositePropertySource(
				EtcdConstants.NAME);
		Collections.reverse(contexts);

		for (String context : contexts) {
			EtcdPropertySource propertySource = new EtcdPropertySource(context, etcd, properties);
			propertySource.init();

			composite.addPropertySource(propertySource);
		}

		return composite;
	}

	return null;
}
 
Example 4
Source File: ProfileInitializer.java    From the-app with Apache License 2.0 4 votes vote down vote up
private boolean hasActiveProfile(ConfigurableEnvironment environment) {
    return environment.getActiveProfiles().length > 0;
}
 
Example 5
Source File: ProfileInitializer.java    From AppStash with Apache License 2.0 4 votes vote down vote up
private boolean hasActiveProfile(ConfigurableEnvironment environment) {
    return environment.getActiveProfiles().length > 0;
}
 
Example 6
Source File: SpringApplication.java    From spring-javaformat with Apache License 2.0 3 votes vote down vote up
/**
 * Configure which profiles are active (or active by default) for this application
 * environment. Additional profiles may be activated during configuration file
 * processing via the {@code spring.profiles.active} property.
 * @param environment this application's environment
 * @param args arguments passed to the {@code run} method
 * @see #configureEnvironment(ConfigurableEnvironment, String[])
 * @see org.springframework.boot.context.config.ConfigFileApplicationListener
 */
protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {
	environment.getActiveProfiles(); // ensure they are initialized
	// But these ones should go first (last wins in a property key clash)
	Set<String> profiles = new LinkedHashSet<>(this.additionalProfiles);
	profiles.addAll(Arrays.asList(environment.getActiveProfiles()));
	environment.setActiveProfiles(StringUtils.toStringArray(profiles));
}