Skip to content

Instantly share code, notes, and snippets.

@zapl
Created June 30, 2016 22:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zapl/1262d21b8866ebc61e48ece505277f69 to your computer and use it in GitHub Desktop.
Save zapl/1262d21b8866ebc61e48ece505277f69 to your computer and use it in GitHub Desktop.
@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