public static void addToEnvironment(ConfigurableEnvironment environment) { environment.getPropertySources().addAfter( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new RandomValuePropertySource(RANDOM_PROPERTY_SOURCE_NAME)); logger.trace("RandomValuePropertySource add to Environment"); }
private void initializePropertySources() { if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) { //already initialized return; } CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME); //sort by order asc ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet()); Iterator<Integer> iterator = orders.iterator(); while (iterator.hasNext()) { int order = iterator.next(); for (String namespace : NAMESPACE_NAMES.get(order)) { Config config = ConfigService.getConfig(namespace); composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); } } // clean up NAMESPACE_NAMES.clear(); // add after the bootstrap property source or to the first if (environment.getPropertySources() .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { // ensure ApolloBootstrapPropertySources is still the first ensureBootstrapPropertyPrecedence(environment); environment.getPropertySources() .addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite); } else { environment.getPropertySources().addFirst(composite); } }
private void addLoadedPropertySource(MutablePropertySources destination, String lastAdded, PropertySource<?> source) { if (lastAdded == null) { if (destination.contains(DEFAULT_PROPERTIES)) { destination.addBefore(DEFAULT_PROPERTIES, source); } else { destination.addLast(source); } } else { destination.addAfter(lastAdded, source); } }
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) { Properties properties = new Properties(); JsonParser jsonParser = JsonParserFactory.getJsonParser(); addWithPrefix(properties, getPropertiesFromApplication(environment, jsonParser), "vcap.application."); addWithPrefix(properties, getPropertiesFromServices(environment, jsonParser), "vcap.services."); MutablePropertySources propertySources = environment.getPropertySources(); if (propertySources.contains( CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { propertySources.addAfter( CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource("vcap", properties)); } else { propertySources .addFirst(new PropertiesPropertySource("vcap", properties)); } } }
private void insert(MutablePropertySources propertySources, PropertySource<?> propertySource) { if (propertySources .contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) { if (DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME .equals(propertySource.getName())) { propertySources.addBefore( BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME, propertySource); } else { propertySources.addAfter( BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME, propertySource); } } else { propertySources.addFirst(propertySource); } }
target.addAfter(targetName, source);
.contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) { if (!remoteProperties.isOverrideSystemProperties()) { propertySources.addAfter( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ClassPathResource resource = new ClassPathResource(OH_PROPERTIES_LOCATION); if (resource.exists()) { LOG.info("Loading external property '{}{}'.", CLASSPATH_URL_PREFIX, OH_PROPERTIES_LOCATION); ResourcePropertySource propertySource; try { propertySource = new ResourcePropertySource(OH_PROPERTY_SOURCE_NAME, resource); event.getEnvironment().getPropertySources().addAfter(RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME, propertySource); } catch (IOException e) { throw new IllegalStateException("Unable to parse external property " + CLASSPATH_URL_PREFIX + OH_PROPERTIES_LOCATION, e); } } LOG.debug("External property '{}{}' was not found.", CLASSPATH_URL_PREFIX, OH_PROPERTIES_LOCATION); } }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ClassPathResource resource = new ClassPathResource(OPENHUB_PROPERTIES_LOCATION); if (resource.exists()) { LOG.info("Loading external property '{}{}'.", CLASSPATH_URL_PREFIX, OPENHUB_PROPERTIES_LOCATION); ResourcePropertySource propertySource; try { propertySource = new ResourcePropertySource(OPENHUB_PROPERTY_SOURCE_NAME, resource); event.getEnvironment().getPropertySources() .addAfter(RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME, propertySource); LOG.debug("External property '{}{}' was found.", CLASSPATH_URL_PREFIX, OPENHUB_PROPERTIES_LOCATION); } catch (IOException ex) { throw new IllegalStateException( "Unable to parse external property " + CLASSPATH_URL_PREFIX + OPENHUB_PROPERTIES_LOCATION, ex); } } else { LOG.debug("External property '{}{}' was not found.", CLASSPATH_URL_PREFIX, OPENHUB_PROPERTIES_LOCATION); } } }
@Override public void afterPropertiesSet() throws Exception { ConfigurableEnvironment environment = (ConfigurableEnvironment) this.environment; PropertiesPropertySource[] propertiesPropertySources = databaseConfigFactory.getObject().toPropertiesPropertySources(); for (int i = (propertiesPropertySources.length - 1); i >= 0; i--) { environment.getPropertySources() .addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getPropertySource(propertiesPropertySources[i])); } }
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { log.info("Detecting 'K8S'-Environment ..."); Properties properties = new Properties(); if (specificFilesystemLayoutExists()) { properties.put(K8S_ENABLED_KEY, Boolean.TRUE.toString()); properties.put(K8S_NAMESPACE, readNamespace()); log.info("'K8S'-metadata : {}", properties.toString()); } else { log.info("Ignore 'K8S', no metadata available."); properties.put(K8S_ENABLED_KEY, Boolean.FALSE.toString()); } // MutablePropertySources propertySources = environment.getPropertySources(); if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource(K8S_PREFIX, properties)); } else { propertySources.addFirst(new PropertiesPropertySource(K8S_PREFIX, properties)); } }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { MutablePropertySources propertySources = event.getEnvironment().getPropertySources(); if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { if(log.isDebugEnabled()) { log.debug("Adding afterCommandLineArgs property source after " + CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME); } propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new MapPropertySource("afterCommandLineArgs", propertySourceMap)); } else { if(log.isDebugEnabled()) { log.debug("Adding afterCommandLineArgs property source as first"); } propertySources.addFirst(new MapPropertySource("afterCommandLineArgs", propertySourceMap)); } }
@Override public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) { File file = new File(System.getProperty("user.home"), LOCATION); if (file.exists()) { MutablePropertySources propertySources = configurableEnvironment.getPropertySources(); logger.info("Loading local settings from " + file.getAbsolutePath()); Properties properties = loadProperties(file); if (propertySources.contains( CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { propertySources.addAfter( CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource("hello-local", properties)); } else { propertySources .addFirst(new PropertiesPropertySource("hello-local", properties)); } } }
private void addNacosPropertySource(NacosPropertySource nacosPropertySource) { MutablePropertySources propertySources = environment.getPropertySources(); boolean first = nacosPropertySource.isFirst(); String before = nacosPropertySource.getBefore(); String after = nacosPropertySource.getAfter(); boolean hasBefore = !nullSafeEquals(DEFAULT_STRING_ATTRIBUTE_VALUE, before); boolean hasAfter = !nullSafeEquals(DEFAULT_STRING_ATTRIBUTE_VALUE, after); boolean isRelative = hasBefore || hasAfter; if (first) { // If First propertySources.addFirst(nacosPropertySource); } else if (isRelative) { // If relative if (hasBefore) { propertySources.addBefore(before, nacosPropertySource); } if (hasAfter) { propertySources.addAfter(after, nacosPropertySource); } } else { propertySources.addLast(nacosPropertySource); // default add last } }
private void insert(MutablePropertySources propertySources, PropertySource<?> propertySource) { if (propertySources .contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) { if (DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME .equals(propertySource.getName())) { propertySources.addBefore( BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME, propertySource); } else { propertySources.addAfter( BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME, propertySource); } } else { propertySources.addFirst(propertySource); } }
private void addNacosPropertySource(NacosPropertySource nacosPropertySource) { MutablePropertySources propertySources = environment.getPropertySources(); boolean first = nacosPropertySource.isFirst(); String before = nacosPropertySource.getBefore(); String after = nacosPropertySource.getAfter(); boolean hasBefore = !nullSafeEquals(DEFAULT_STRING_ATTRIBUTE_VALUE, before); boolean hasAfter = !nullSafeEquals(DEFAULT_STRING_ATTRIBUTE_VALUE, after); boolean isRelative = hasBefore || hasAfter; if (first) { // If First propertySources.addFirst(nacosPropertySource); } else if (isRelative) { // If relative if (hasBefore) { propertySources.addBefore(before, nacosPropertySource); } if (hasAfter) { propertySources.addAfter(after, nacosPropertySource); } } else { propertySources.addLast(nacosPropertySource); // default add last } }
@Override public void run() { try { StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addAfter( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new MapPropertySource("appDeployer", properties)); String main = source; if (source==null) { main = SpringApplication.class.getName(); } SpringApplicationBuilder builder = builder(main) .registerShutdownHook(false) .environment(environment); context = builder.run(args); } catch (Throwable ex) { error = ex; } } });
@Override public void run() { try { resetUrlHandler(); StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addAfter( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new MapPropertySource("appDeployer", properties)); if (args != null && args.length > 0) { environment.getPropertySources().addFirst( new SimpleCommandLinePropertySource("args", args)); } running = true; Class<?> sourceClass = ClassUtils.resolveClassName(source, null); SpringApplication builder = builder(sourceClass, environment); context = builder.run(args); } catch (Throwable ex) { error = ex; } } });
public void addKeyVaultPropertySource() { final String vaultUri = getProperty(this.environment, Constants.AZURE_KEYVAULT_VAULT_URI); final Long refreshInterval = Optional.ofNullable( this.environment.getProperty(Constants.AZURE_KEYVAULT_REFRESH_INTERVAL)) .map(Long::valueOf).orElse(Constants.DEFAULT_REFRESH_INTERVAL_MS); final ServiceClientCredentials credentials = getCredentials(); final RestClient restClient = new RestClient.Builder().withBaseUrl(vaultUri) .withCredentials(credentials) .withSerializerAdapter(new AzureJacksonAdapter()) .withResponseBuilderFactory(new AzureResponseBuilder.Factory()) .withUserAgent(UserAgent.getUserAgent(Constants.AZURE_KEYVAULT_USER_AGENT, allowTelemetry(this.environment))) .build(); final KeyVaultClient kvClient = new KeyVaultClient(restClient); this.trackCustomEvent(); try { final MutablePropertySources sources = this.environment.getPropertySources(); final KeyVaultOperation kvOperation = new KeyVaultOperation(kvClient, vaultUri, refreshInterval); if (sources.contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) { sources.addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new KeyVaultPropertySource(kvOperation)); } else { sources.addFirst(new KeyVaultPropertySource(kvOperation)); } } catch (final Exception ex) { throw new IllegalStateException("Failed to configure KeyVault property source", ex); } }