22// Licensed under the MIT License.
33package com .microsoft .azure .spring .cloud .config ;
44
5- import com .azure .data .appconfiguration .models .ConfigurationSetting ;
6- import com .azure .data .appconfiguration .models .SettingSelector ;
7- import com .microsoft .azure .spring .cloud .config .feature .management .entity .FeatureSet ;
8- import com .microsoft .azure .spring .cloud .config .properties .AppConfigurationProperties ;
9- import com .microsoft .azure .spring .cloud .config .properties .AppConfigurationProviderProperties ;
10- import com .microsoft .azure .spring .cloud .config .properties .AppConfigurationStoreTrigger ;
11- import com .microsoft .azure .spring .cloud .config .properties .ConfigStore ;
12- import com .microsoft .azure .spring .cloud .config .stores .ClientStore ;
135import java .util .ArrayList ;
146import java .util .Arrays ;
157import java .util .Collections ;
1911import java .util .Map ;
2012import java .util .concurrent .ConcurrentHashMap ;
2113import java .util .concurrent .atomic .AtomicBoolean ;
14+
2215import org .apache .commons .lang3 .time .DateUtils ;
2316import org .slf4j .Logger ;
2417import org .slf4j .LoggerFactory ;
3124import org .springframework .util .ReflectionUtils ;
3225import org .springframework .util .StringUtils ;
3326
27+ import com .azure .data .appconfiguration .models .ConfigurationSetting ;
28+ import com .azure .data .appconfiguration .models .SettingSelector ;
29+ import com .microsoft .azure .spring .cloud .config .feature .management .entity .FeatureSet ;
30+ import com .microsoft .azure .spring .cloud .config .properties .AppConfigurationProperties ;
31+ import com .microsoft .azure .spring .cloud .config .properties .AppConfigurationProviderProperties ;
32+ import com .microsoft .azure .spring .cloud .config .properties .AppConfigurationStoreTrigger ;
33+ import com .microsoft .azure .spring .cloud .config .properties .ConfigStore ;
34+ import com .microsoft .azure .spring .cloud .config .stores .ClientStore ;
35+
3436public class AppConfigurationPropertySourceLocator implements PropertySourceLocator {
3537
3638 private static final Logger LOGGER = LoggerFactory .getLogger (AppConfigurationPropertySourceLocator .class );
3739
3840 private static final String SPRING_APP_NAME_PROP = "spring.application.name" ;
3941
4042 private static final String PROPERTY_SOURCE_NAME = "azure-config-store" ;
41-
43+
4244 private static final String PATH_SPLITTER = "/" ;
45+
4346 private final AppConfigurationProperties properties ;
44- private final String profileSeparator ;
47+
4548 private final List <ConfigStore > configStores ;
49+
4650 private final Map <String , List <String >> storeContextsMap = new ConcurrentHashMap <>();
51+
4752 private final AppConfigurationProviderProperties appProperties ;
53+
4854 private final ClientStore clients ;
55+
4956 private final KeyVaultCredentialProvider keyVaultCredentialProvider ;
57+
5058 private final SecretClientBuilderSetup keyVaultClientProvider ;
59+
5160 private static AtomicBoolean startup = new AtomicBoolean (true );
5261
5362 public AppConfigurationPropertySourceLocator (AppConfigurationProperties properties ,
5463 AppConfigurationProviderProperties appProperties , ClientStore clients ,
5564 KeyVaultCredentialProvider keyVaultCredentialProvider , SecretClientBuilderSetup keyVaultClientProvider ) {
5665 this .properties = properties ;
5766 this .appProperties = appProperties ;
58- this .profileSeparator = properties .getProfileSeparator ();
5967 this .configStores = properties .getStores ();
6068 this .clients = clients ;
6169 this .keyVaultCredentialProvider = keyVaultCredentialProvider ;
@@ -102,21 +110,20 @@ public Map<String, List<String>> getStoreContextsMap() {
102110 /**
103111 * Adds a new Property Source
104112 *
105- * @param composite PropertySource being added
106- * @param store Config Store the PropertySource is being generated from
107- * @param applicationName Name of the application
108- * @param profiles Active profiles in the Store
113+ * @param composite PropertySource being added
114+ * @param store Config Store the PropertySource is being generated from
115+ * @param applicationName Name of the application
116+ * @param profiles Active profiles in the Store
109117 * @param storeContextsMap the Map storing the storeName -> List of contexts map
110- * @param initFeatures determines if Feature Management is set in the PropertySource. When generating more than
111- * one it needs to be in the last one.
118+ * @param initFeatures determines if Feature Management is set in the PropertySource. When generating more than one
119+ * it needs to be in the last one.
112120 */
113121 private void addPropertySource (CompositePropertySource composite , ConfigStore store , String applicationName ,
114122 List <String > profiles , Map <String , List <String >> storeContextsMap , boolean initFeatures ) {
115123 /*
116- * Generate which contexts(key prefixes) will be used for key-value items search
117- * If key prefix is empty, default context is: application, current application
118- * name is: foo, active profile is: dev, profileSeparator is: _ Will generate
119- * these contexts: /application/, /application_dev/, /foo/, /foo_dev/
124+ * Generate which contexts(key prefixes) will be used for key-value items search If key prefix is empty, default
125+ * context is: application, current application name is: foo, active profile is: dev, profileSeparator is: _
126+ * Will generate these contexts: /application/, /application_dev/, /foo/, /foo_dev/
120127 */
121128 List <String > contexts = new ArrayList <>();
122129 contexts .addAll (generateContexts (this .properties .getDefaultContext (), profiles , store ));
@@ -132,7 +139,7 @@ private void addPropertySource(CompositePropertySource composite, ConfigStore st
132139 Collections .reverse (contexts );
133140 for (String sourceContext : contexts ) {
134141 try {
135- sourceList .addAll (create (sourceContext , store , storeContextsMap , initFeatures , featureSet ));
142+ sourceList .addAll (create (sourceContext , store , storeContextsMap , profiles , initFeatures , featureSet ));
136143
137144 LOGGER .debug ("PropertySource context [{}] is added." , sourceContext );
138145 } catch (Exception e ) {
@@ -166,7 +173,6 @@ private List<String> generateContexts(String applicationName, List<String> profi
166173
167174 String prefixedContext = propWithAppName (prefix , applicationName );
168175 result .add (prefixedContext + PATH_SPLITTER );
169- profiles .forEach (profile -> result .add (propWithProfile (prefixedContext , profile )));
170176
171177 return result ;
172178 }
@@ -180,26 +186,25 @@ private String propWithAppName(String prefix, String applicationName) {
180186 return PATH_SPLITTER + applicationName ;
181187 }
182188
183- private String propWithProfile (String context , String profile ) {
184- return context + this .profileSeparator + profile + PATH_SPLITTER ;
185- }
186-
187189 /**
188190 * Creates a new set of AppConfigurationProertySources, 1 per Label.
189191 *
190- * @param context Context of the application, part of uniquely define a PropertySource
191- * @param store Config Store the PropertySource is being generated from
192+ * @param context Context of the application, part of uniquely define a PropertySource
193+ * @param store Config Store the PropertySource is being generated from
192194 * @param storeContextsMap the Map storing the storeName -> List of contexts map
193- * @param initFeatures determines if Feature Management is set in the PropertySource. When generating more than
194- * one it needs to be in the last one.
195+ * @param initFeatures determines if Feature Management is set in the PropertySource. When generating more than one
196+ * it needs to be in the last one.
195197 * @return a list of AppConfigurationPropertySources
196198 */
197199 private List <AppConfigurationPropertySource > create (String context , ConfigStore store ,
198- Map <String , List <String >> storeContextsMap , boolean initFeatures , FeatureSet featureSet ) throws Exception {
200+ Map <String , List <String >> storeContextsMap , List <String > profiles , boolean initFeatures , FeatureSet featureSet )
201+ throws Exception {
199202 List <AppConfigurationPropertySource > sourceList = new ArrayList <>();
200203
201204 try {
202- for (String label : store .getLabels ()) {
205+ String [] labels = store .getLabels (profiles );
206+
207+ for (String label : labels ) {
203208 putStoreContext (store .getEndpoint (), context , storeContextsMap );
204209 AppConfigurationPropertySource propertySource = new AppConfigurationPropertySource (context , store ,
205210 label , properties , clients , appProperties , keyVaultCredentialProvider , keyVaultClientProvider );
@@ -234,8 +239,8 @@ private List<AppConfigurationPropertySource> create(String context, ConfigStore
234239 /**
235240 * Put certain context to the store contexts map
236241 *
237- * @param storeName the name of the configuration store
238- * @param context the context text for the PropertySource, e.g., "/application"
242+ * @param storeName the name of the configuration store
243+ * @param context the context text for the PropertySource, e.g., "/application"
239244 * @param storeContextsMap the Map storing the storeName -> List of contexts map
240245 */
241246 private void putStoreContext (String storeName , String context ,
0 commit comments