-
-
Save zapl/1262d21b8866ebc61e48ece505277f69 to your computer and use it in GitHub Desktop.
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
@Documented | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.TYPE, ElementType.METHOD}) | |
@Conditional(AllProfilesCondition.class) | |
public @interface AllProfiles { | |
String[] value(); | |
static class AllProfilesCondition implements Condition { | |
@Override | |
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | |
if (context.getEnvironment() != null) { | |
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(AllProfiles.class.getName()); | |
if (attrs != null) { | |
// check that all match | |
for (Object value : attrs.get("value")) { | |
// check that 1 set of profiles matches | |
String[] profiles = (String[]) value; | |
Assert.notEmpty(profiles, "Must specify at least one profile"); | |
for (String profile : profiles) { | |
if (!context.getEnvironment() | |
.acceptsProfiles(profile)) { | |
return false; | |
} | |
} | |
// this set of profiles matches but there could be more | |
} | |
return true; | |
} | |
} | |
// environment or attrs was null, no match | |
return false; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment