forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix apollo config data loader with profiles (apolloconfig#3870)
* fix apollo config data loader with profiles * CHANGES.md * m_pureApolloConfig default to false * rename field * divide Config to 2 class * add unit test * change getPropertyFromRepository * change getPropertyFromAdditional * use spi to create PureApolloConfig, and remove the pureApolloConfig field * modify unit test * update getPropertyNames * move to apollo-client-config-data * extends DefaultConfig * update unit test * fix getPropertyNames * update DefaultConfig unit test * remove AbstractRepositoryConfig * Update apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java Co-authored-by: Jason Song <[email protected]> Co-authored-by: Jason Song <[email protected]>
- Loading branch information
Showing
14 changed files
with
523 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
...fig/data/extension/webclient/injector/ApolloClientCustomHttpClientInjectorCustomizer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...a/com/ctrip/framework/apollo/config/data/injector/ApolloConfigDataInjectorCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2021 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.config.data.injector; | ||
|
||
import com.ctrip.framework.apollo.core.spi.Ordered; | ||
import com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* @author vdisk <[email protected]> | ||
*/ | ||
public class ApolloConfigDataInjectorCustomizer implements ApolloInjectorCustomizer { | ||
|
||
/** | ||
* the order of the injector customizer | ||
*/ | ||
public static final int ORDER = Ordered.LOWEST_PRECEDENCE - 200; | ||
|
||
private static final Map<Class<?>, Supplier<?>> INSTANCE_SUPPLIERS = new ConcurrentHashMap<>(); | ||
|
||
private static final Map<Class<?>, Object> INSTANCES = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Register a specific type with the registry. If the specified type has already been registered, | ||
* it will be replaced. | ||
* | ||
* @param <T> the instance type | ||
* @param type the instance type | ||
* @param instanceSupplier the instance supplier | ||
*/ | ||
public static <T> void register(Class<T> type, Supplier<T> instanceSupplier) { | ||
INSTANCE_SUPPLIERS.put(type, instanceSupplier); | ||
} | ||
|
||
/** | ||
* Register a specific type with the registry if one is not already present. | ||
* | ||
* @param <T> the instance type | ||
* @param type the instance type | ||
* @param instanceSupplier the instance supplier | ||
*/ | ||
public static <T> void registerIfAbsent(Class<T> type, Supplier<T> instanceSupplier) { | ||
INSTANCE_SUPPLIERS.putIfAbsent(type, instanceSupplier); | ||
} | ||
|
||
/** | ||
* Return if a registration exists for the given type. | ||
* | ||
* @param <T> the instance type | ||
* @param type the instance type | ||
* @return {@code true} if the type has already been registered | ||
*/ | ||
public static <T> boolean isRegistered(Class<T> type) { | ||
return INSTANCE_SUPPLIERS.containsKey(type); | ||
} | ||
|
||
@Override | ||
public <T> T getInstance(Class<T> clazz) { | ||
@SuppressWarnings("unchecked") | ||
Supplier<T> instanceSupplier = (Supplier<T>) INSTANCE_SUPPLIERS.get(clazz); | ||
if (instanceSupplier == null) { | ||
return null; | ||
} | ||
return this.getInstance(clazz, instanceSupplier); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <T> T getInstance(Class<T> type, Supplier<T> instanceSupplier) { | ||
T instance = (T) INSTANCES.get(type); | ||
if (instance != null) { | ||
return instance; | ||
} | ||
// prebuild an newInstance to prevent dead lock when recursive call computeIfAbsent | ||
// https://bugs.openjdk.java.net/browse/JDK-8062841 | ||
T newInstance = instanceSupplier.get(); | ||
return (T) INSTANCES.computeIfAbsent(type, key -> newInstance); | ||
} | ||
|
||
@Override | ||
public <T> T getInstance(Class<T> clazz, String name) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return ORDER; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...data/src/main/java/com/ctrip/framework/apollo/config/data/internals/PureApolloConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.config.data.internals; | ||
|
||
import com.ctrip.framework.apollo.internals.ConfigRepository; | ||
import com.ctrip.framework.apollo.internals.DefaultConfig; | ||
import com.ctrip.framework.apollo.internals.RepositoryChangeListener; | ||
import com.google.common.collect.Sets; | ||
import java.util.Set; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
/** | ||
* @author vdisk <[email protected]> | ||
*/ | ||
public class PureApolloConfig extends DefaultConfig implements RepositoryChangeListener { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param namespace the namespace of this config instance | ||
* @param configRepository the config repository for this config instance | ||
*/ | ||
public PureApolloConfig(String namespace, | ||
ConfigRepository configRepository) { | ||
super(namespace, configRepository); | ||
} | ||
|
||
@Override | ||
public String getProperty(String key, String defaultValue) { | ||
// step 1: check local cached properties file | ||
String value = this.getPropertyFromRepository(key); | ||
|
||
// step 2: check properties file from classpath | ||
if (value == null) { | ||
value = this.getPropertyFromAdditional(key); | ||
} | ||
|
||
this.tryWarnLog(value); | ||
|
||
return value == null ? defaultValue : value; | ||
} | ||
|
||
@Override | ||
public Set<String> getPropertyNames() { | ||
// pure apollo config only contains the property from repository and the property from additional | ||
Set<String> fromRepository = this.getPropertyNamesFromRepository(); | ||
Set<String> fromAdditional = this.getPropertyNamesFromAdditional(); | ||
if (CollectionUtils.isEmpty(fromRepository)) { | ||
return fromAdditional; | ||
} | ||
if (CollectionUtils.isEmpty(fromAdditional)) { | ||
return fromRepository; | ||
} | ||
Set<String> propertyNames = Sets | ||
.newLinkedHashSetWithExpectedSize(fromRepository.size() + fromAdditional.size()); | ||
propertyNames.addAll(fromRepository); | ||
propertyNames.addAll(fromAdditional); | ||
return propertyNames; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...c/main/java/com/ctrip/framework/apollo/config/data/internals/PureApolloConfigFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2021 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.config.data.internals; | ||
|
||
import com.ctrip.framework.apollo.Config; | ||
import com.ctrip.framework.apollo.internals.ConfigRepository; | ||
import com.ctrip.framework.apollo.spi.ConfigFactory; | ||
import com.ctrip.framework.apollo.spi.DefaultConfigFactory; | ||
|
||
/** | ||
* @author vdisk <[email protected]> | ||
*/ | ||
public class PureApolloConfigFactory extends DefaultConfigFactory implements ConfigFactory { | ||
|
||
@Override | ||
protected Config createRepositoryConfig(String namespace, ConfigRepository configRepository) { | ||
return new PureApolloConfig(namespace, configRepository); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../main/resources/META-INF/services/com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
com.ctrip.framework.apollo.config.data.extension.webclient.injector.ApolloClientCustomHttpClientInjectorCustomizer | ||
com.ctrip.framework.apollo.config.data.injector.ApolloConfigDataInjectorCustomizer |
Oops, something went wrong.