1
|
|
|
package com.github.netkorp.telegram.framework.condition; |
|
|
|
|
2
|
|
|
|
3
|
|
|
import com.github.netkorp.telegram.framework.annotations.TelegramCommand; |
4
|
|
|
import org.springframework.context.annotation.Condition; |
5
|
|
|
import org.springframework.context.annotation.ConditionContext; |
6
|
|
|
import org.springframework.core.type.AnnotatedTypeMetadata; |
7
|
|
|
import org.springframework.core.type.AnnotationMetadata; |
8
|
|
|
import org.springframework.core.type.MethodMetadata; |
9
|
|
|
import org.springframework.stereotype.Component; |
10
|
|
|
|
11
|
|
|
import java.util.List; |
12
|
|
|
import java.util.Map; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Searches for the name of every available command in the excluded command list and the command that appears |
16
|
|
|
* in the list will be excluded from the available command list. |
17
|
|
|
*/ |
18
|
|
|
@Component |
19
|
|
|
public class ExcludeCondition implements Condition { |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Determine if the condition matches. |
23
|
|
|
* |
24
|
|
|
* @param context the condition context |
25
|
|
|
* @param metadata metadata of the {@link AnnotationMetadata class} |
26
|
|
|
* or {@link MethodMetadata method} being checked |
27
|
|
|
* @return {@code true} if the condition matches and the component can be registered, |
28
|
|
|
* or {@code false} to veto the annotated component's registration |
29
|
|
|
*/ |
30
|
|
|
@Override |
31
|
|
|
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
|
|
|
|
32
|
|
|
List excludeValue = context.getEnvironment().getProperty("telegram.commands.exclude", List.class); |
|
|
|
|
33
|
|
|
if (excludeValue != null && context.getBeanFactory() != null) { |
|
|
|
|
34
|
|
|
Map<String, Object> attributes = metadata.getAnnotationAttributes(TelegramCommand.class.getName()); |
|
|
|
|
35
|
|
|
if (attributes != null) { |
|
|
|
|
36
|
|
|
String[] commandNames = (String[]) attributes.get("name"); |
|
|
|
|
37
|
|
|
for (String commandName : commandNames) { |
|
|
|
|
38
|
|
|
if (excludeValue.contains(commandName)) { |
|
|
|
|
39
|
|
|
return false; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|