Commit 9721ba37 by 张乐 Committed by GitHub

Merge pull request #733 from nobodyiam/conditional-on-profile

Add ConditionalOnProfile and ConditionalOnMissingProfile annotations
parents 5f05ddb0 25b43175
package com.ctrip.framework.apollo.common.condition;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* {@link Conditional} that only matches when the specified profiles are inactive.
*
* @author Jason Song(song_s@ctrip.com)
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnProfileCondition.class)
public @interface ConditionalOnMissingProfile {
/**
* The profiles that should be inactive
* @return
*/
String[] value() default {};
}
package com.ctrip.framework.apollo.common.condition;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* {@link Conditional} that only matches when the specified profiles are active.
*
* @author Jason Song(song_s@ctrip.com)
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnProfileCondition.class)
public @interface ConditionalOnProfile {
/**
* The profiles that should be active
* @return
*/
String[] value() default {};
}
package com.ctrip.framework.apollo.common.condition;
import com.google.common.collect.Sets;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class OnProfileCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Set<String> activeProfiles = Sets.newHashSet(context.getEnvironment().getActiveProfiles());
Set<String> requiredActiveProfiles = retrieveAnnotatedProfiles(metadata, ConditionalOnProfile.class.getName());
Set<String> requiredInactiveProfiles = retrieveAnnotatedProfiles(metadata, ConditionalOnMissingProfile.class
.getName());
return Sets.difference(requiredActiveProfiles, activeProfiles).isEmpty()
&& Sets.intersection(requiredInactiveProfiles, activeProfiles).isEmpty();
}
private Set<String> retrieveAnnotatedProfiles(AnnotatedTypeMetadata metadata, String annotationType) {
if (!metadata.isAnnotated(annotationType)) {
return Collections.emptySet();
}
Set<String> profiles = Sets.newHashSet();
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationType);
List<?> values = attributes.get("value");
if (values != null) {
for (Object value : values) {
if (value instanceof String[]) {
Collections.addAll(profiles, (String[]) value);
}
else {
profiles.add((String) value);
}
}
}
return profiles;
}
}
package com.ctrip.framework.apollo.common; package com.ctrip.framework.apollo.common;
import com.ctrip.framework.apollo.common.conditional.ConditionalOnProfileTest;
import com.ctrip.framework.apollo.common.utils.InputValidatorTest; import com.ctrip.framework.apollo.common.utils.InputValidatorTest;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -8,7 +9,7 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -8,7 +9,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ @SuiteClasses({
InputValidatorTest.class InputValidatorTest.class, ConditionalOnProfileTest.class
}) })
public class AllTests { public class AllTests {
......
package com.ctrip.framework.apollo.common.conditional;
import com.ctrip.framework.apollo.common.condition.ConditionalOnMissingProfile;
import com.ctrip.framework.apollo.common.condition.ConditionalOnProfile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static com.ctrip.framework.apollo.common.conditional.ConditionalOnProfileTest.ANOTHER_PROFILE;
import static com.ctrip.framework.apollo.common.conditional.ConditionalOnProfileTest.SOME_PROFILE;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ConditionalOnProfileTest.TestConfiguration.class)
@ActiveProfiles({SOME_PROFILE, ANOTHER_PROFILE})
public class ConditionalOnProfileTest {
static final String SOME_PROFILE = "someProfile";
static final String ANOTHER_PROFILE = "anotherProfile";
static final String YET_ANOTHER_PROFILE = "yetAnotherProfile";
static boolean someConfigurationEnabled = false;
static boolean anotherConfigurationEnabled = false;
static boolean yetAnotherConfigurationEnabled = false;
static boolean combinedConfigurationEnabled = false;
static boolean anotherCombinedConfigurationEnabled = false;
@Test
public void test() throws Exception {
assertTrue(someConfigurationEnabled);
assertFalse(anotherConfigurationEnabled);
assertTrue(yetAnotherConfigurationEnabled);
assertTrue(combinedConfigurationEnabled);
assertFalse(anotherCombinedConfigurationEnabled);
}
@Configuration
static class TestConfiguration {
@Configuration
@ConditionalOnProfile(SOME_PROFILE)
static class SomeConfiguration {
{
someConfigurationEnabled = true;
}
}
@Configuration
@ConditionalOnMissingProfile(ANOTHER_PROFILE)
static class AnotherConfiguration {
{
anotherConfigurationEnabled = true;
}
}
@Configuration
@ConditionalOnMissingProfile(YET_ANOTHER_PROFILE)
static class YetAnotherConfiguration {
{
yetAnotherConfigurationEnabled = true;
}
}
@Configuration
@ConditionalOnProfile({SOME_PROFILE, ANOTHER_PROFILE})
@ConditionalOnMissingProfile(YET_ANOTHER_PROFILE)
static class CombinedConfiguration {
{
combinedConfigurationEnabled = true;
}
}
@Configuration
@ConditionalOnProfile(SOME_PROFILE)
@ConditionalOnMissingProfile({YET_ANOTHER_PROFILE, ANOTHER_PROFILE})
static class AnotherCombinedConfiguration {
{
anotherCombinedConfigurationEnabled = true;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment