Skip to content

Commit

Permalink
spring condition profile 的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed May 13, 2018
1 parent 02f1389 commit b9e766e
Showing 1 changed file with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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;
Expand All @@ -12,46 +11,48 @@
import java.util.Set;

/**
* 实现 Condition Profile 的功能
*
* @author Jason Song([email protected])
*/
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());
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Set<String> activeProfiles = Sets.newHashSet(context.getEnvironment().getActiveProfiles());

return Sets.difference(requiredActiveProfiles, activeProfiles).isEmpty()
&& Sets.intersection(requiredInactiveProfiles, activeProfiles).isEmpty();
}
Set<String> requiredActiveProfiles = retrieveAnnotatedProfiles(metadata, ConditionalOnProfile.class.getName());
Set<String> requiredInactiveProfiles = retrieveAnnotatedProfiles(metadata, ConditionalOnMissingProfile.class.getName());

private Set<String> retrieveAnnotatedProfiles(AnnotatedTypeMetadata metadata, String annotationType) {
if (!metadata.isAnnotated(annotationType)) {
return Collections.emptySet();
return Sets.difference(requiredActiveProfiles, activeProfiles).isEmpty()
&& Sets.intersection(requiredInactiveProfiles, activeProfiles).isEmpty();
}

MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationType);

if (attributes == null) {
return Collections.emptySet();
}
private Set<String> retrieveAnnotatedProfiles(AnnotatedTypeMetadata metadata, String annotationType) {
if (!metadata.isAnnotated(annotationType)) {
return Collections.emptySet();
}

Set<String> profiles = Sets.newHashSet();
List<?> values = attributes.get("value");
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationType);

if (values != null) {
for (Object value : values) {
if (value instanceof String[]) {
Collections.addAll(profiles, (String[]) value);
if (attributes == null) {
return Collections.emptySet();
}
else {
profiles.add((String) value);

Set<String> profiles = Sets.newHashSet();
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;
}

return profiles;
}
}

0 comments on commit b9e766e

Please sign in to comment.