Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
9721ba37
Commit
9721ba37
authored
Sep 05, 2017
by
张乐
Committed by
GitHub
Sep 05, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #733 from nobodyiam/conditional-on-profile
Add ConditionalOnProfile and ConditionalOnMissingProfile annotations
parents
5f05ddb0
25b43175
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
198 additions
and
1 deletion
+198
-1
ConditionalOnMissingProfile.java
.../apollo/common/condition/ConditionalOnMissingProfile.java
+26
-0
ConditionalOnProfile.java
...amework/apollo/common/condition/ConditionalOnProfile.java
+27
-0
OnProfileCondition.java
...framework/apollo/common/condition/OnProfileCondition.java
+52
-0
AllTests.java
...test/java/com/ctrip/framework/apollo/common/AllTests.java
+2
-1
ConditionalOnProfileTest.java
...k/apollo/common/conditional/ConditionalOnProfileTest.java
+91
-0
No files found.
apollo-common/src/main/java/com/ctrip/framework/apollo/common/condition/ConditionalOnMissingProfile.java
0 → 100644
View file @
9721ba37
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
{};
}
apollo-common/src/main/java/com/ctrip/framework/apollo/common/condition/ConditionalOnProfile.java
0 → 100644
View file @
9721ba37
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
{};
}
apollo-common/src/main/java/com/ctrip/framework/apollo/common/condition/OnProfileCondition.java
0 → 100644
View file @
9721ba37
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
;
}
}
apollo-common/src/test/java/com/ctrip/framework/apollo/common/AllTests.java
View file @
9721ba37
package
com
.
ctrip
.
framework
.
apollo
.
common
;
import
com.ctrip.framework.apollo.common.conditional.ConditionalOnProfileTest
;
import
com.ctrip.framework.apollo.common.utils.InputValidatorTest
;
import
org.junit.runner.RunWith
;
...
...
@@ -8,7 +9,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith
(
Suite
.
class
)
@SuiteClasses
({
InputValidatorTest
.
class
InputValidatorTest
.
class
,
ConditionalOnProfileTest
.
class
})
public
class
AllTests
{
...
...
apollo-common/src/test/java/com/ctrip/framework/apollo/common/conditional/ConditionalOnProfileTest.java
0 → 100644
View file @
9721ba37
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
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment