forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
May 13, 2018
1 parent
02f1389
commit b9e766e
Showing
1 changed file
with
30 additions
and
29 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
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; | ||
|
@@ -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; | ||
} | ||
} |