17
17
package org .apache .logging .log4j .plugins .validation .validators ;
18
18
19
19
import org .apache .logging .log4j .Logger ;
20
- import org .apache .logging .log4j .plugins .Inject ;
21
- import org .apache .logging .log4j .plugins .convert .TypeConverter ;
22
- import org .apache .logging .log4j .plugins .convert .TypeConverterFactory ;
23
20
import org .apache .logging .log4j .plugins .validation .ConstraintValidator ;
24
21
import org .apache .logging .log4j .plugins .validation .constraints .ValidPort ;
25
22
import org .apache .logging .log4j .status .StatusLogger ;
@@ -33,29 +30,29 @@ public class ValidPortValidator implements ConstraintValidator<ValidPort> {
33
30
34
31
private static final Logger LOGGER = StatusLogger .getLogger ();
35
32
36
- private final TypeConverter <Integer > converter ;
37
33
private ValidPort annotation ;
38
34
39
- @ Inject
40
- public ValidPortValidator (final TypeConverterFactory factory ) {
41
- converter = factory .getTypeConverter (Integer .class );
42
- }
43
-
44
35
@ Override
45
36
public void initialize (final ValidPort annotation ) {
46
37
this .annotation = annotation ;
47
38
}
48
39
49
40
@ Override
50
41
public boolean isValid (final String name , final Object value ) {
51
- if (value instanceof CharSequence ) {
52
- return isValid (name , converter .convert (value .toString (), -1 ));
53
- }
54
- if (!(value instanceof Integer )) {
42
+ final int port ;
43
+ if (value instanceof Integer i ) {
44
+ port = i ;
45
+ } else if (value instanceof CharSequence cs ) {
46
+ try {
47
+ port = Integer .parseInt (cs , 0 , cs .length (), 10 );
48
+ } catch (final NumberFormatException ignored ) {
49
+ LOGGER .error (annotation .message ());
50
+ return false ;
51
+ }
52
+ } else {
55
53
LOGGER .error (annotation .message ());
56
54
return false ;
57
55
}
58
- final int port = (int ) value ;
59
56
if (port < 0 || port > 65535 ) {
60
57
LOGGER .error (annotation .message ());
61
58
return false ;
0 commit comments