17
17
package org .springframework .boot .autoconfigure .interfaceclients .http ;
18
18
19
19
import java .lang .annotation .Annotation ;
20
+ import java .text .Normalizer ;
20
21
import java .util .Set ;
21
22
22
23
import org .apache .commons .logging .Log ;
23
24
import org .apache .commons .logging .LogFactory ;
25
+ import org .apache .commons .text .CaseUtils ;
24
26
25
27
import org .springframework .beans .factory .ListableBeanFactory ;
26
28
import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
33
35
import org .springframework .core .type .AnnotationMetadata ;
34
36
import org .springframework .util .Assert ;
35
37
import org .springframework .util .ObjectUtils ;
36
- import org .springframework .util .StringUtils ;
37
38
38
39
/**
39
40
* @author Olga Maciaszek-Sharma
@@ -43,19 +44,24 @@ public class HttpInterfaceClientsImportRegistrar extends AbstractInterfaceClient
43
44
44
45
private static final Log logger = LogFactory .getLog (HttpInterfaceClientsImportRegistrar .class );
45
46
47
+ private static final String INTERFACE_CLIENT_SUFFIX = "InterfaceClient" ;
48
+
49
+ private static final String BEAN_NAME_ATTRIBUTE_NAME = "beanName" ;
50
+
46
51
@ Override
47
52
public void registerBeanDefinitions (AnnotationMetadata metadata , BeanDefinitionRegistry registry ) {
48
53
Assert .isInstanceOf (ListableBeanFactory .class , registry ,
49
54
"Registry must be an instance of " + ListableBeanFactory .class .getSimpleName ());
50
- Set <BeanDefinition > candidateComponents = discoverCandidateComponents (metadata );
55
+ ListableBeanFactory beanFactory = (ListableBeanFactory ) registry ;
56
+ Set <BeanDefinition > candidateComponents = discoverCandidateComponents (beanFactory );
51
57
for (BeanDefinition candidateComponent : candidateComponents ) {
52
58
if (candidateComponent instanceof AnnotatedBeanDefinition beanDefinition ) {
53
- AnnotationMetadata annotationMetadata = beanDefinition .getMetadata ();
54
- Assert .isTrue (annotationMetadata .isInterface (),
59
+ AnnotationMetadata annotatedBeanMetadata = beanDefinition .getMetadata ();
60
+ Assert .isTrue (annotatedBeanMetadata .isInterface (),
55
61
getAnnotation ().getSimpleName () + "can only be placed on an interface." );
56
- MergedAnnotation <? extends Annotation > annotation = annotationMetadata .getAnnotations ()
62
+ MergedAnnotation <? extends Annotation > annotation = annotatedBeanMetadata .getAnnotations ()
57
63
.get (getAnnotation ());
58
- String beanClassName = annotationMetadata .getClassName ();
64
+ String beanClassName = annotatedBeanMetadata .getClassName ();
59
65
Class <?> beanClass ;
60
66
try {
61
67
beanClass = Class .forName (beanClassName );
@@ -66,20 +72,31 @@ public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionR
66
72
}
67
73
throw new RuntimeException (e );
68
74
}
69
- String clientName = !ObjectUtils .isEmpty (annotation .getString (MergedAnnotation .VALUE ))
70
- ? annotation .getString (MergedAnnotation .VALUE ) : StringUtils .uncapitalize (beanClassName );
71
- ListableBeanFactory beanFactory = (ListableBeanFactory ) registry ;
75
+ // TODO: consider naming conventions: value of the annotation is the
76
+ // qualifier to look for related beans
77
+ // TODO: while the actual beanName corresponds to the simple class name
78
+ // suffixed with InterfaceClient
79
+ String clientQualifier = annotation .getString (MergedAnnotation .VALUE );
80
+ String beanName = !ObjectUtils .isEmpty (annotation .getString (BEAN_NAME_ATTRIBUTE_NAME ))
81
+ ? annotation .getString (BEAN_NAME_ATTRIBUTE_NAME ) : buildBeanName (clientQualifier );
72
82
HttpInterfaceClientAdapter adapter = beanFactory .getBean (HttpInterfaceClientAdapter .class );
73
83
BeanDefinition definition = BeanDefinitionBuilder
74
84
.rootBeanDefinition (ResolvableType .forClass (beanClass ),
75
- () -> adapter .createClient (beanFactory , clientName , beanClass ))
85
+ () -> adapter .createClient (beanFactory , clientQualifier , beanClass ))
76
86
.getBeanDefinition ();
77
- registry .registerBeanDefinition (clientName , definition );
87
+ registry .registerBeanDefinition (beanName , definition );
78
88
79
89
}
80
90
}
81
91
}
82
92
93
+ private String buildBeanName (String clientQualifier ) {
94
+ // TODO: research Normalizer form types
95
+ String normalised = Normalizer .normalize (clientQualifier , Normalizer .Form .NFD );
96
+ String camelCased = CaseUtils .toCamelCase (normalised , false , '-' , '_' );
97
+ return camelCased + INTERFACE_CLIENT_SUFFIX ;
98
+ }
99
+
83
100
@ Override
84
101
protected Class <? extends Annotation > getAnnotation () {
85
102
return HttpClient .class ;
0 commit comments