1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import java .util .List ;
25
25
import java .util .ListIterator ;
26
26
import java .util .Properties ;
27
+ import java .util .stream .Collectors ;
27
28
28
29
import org .apache .commons .logging .Log ;
29
30
import org .apache .commons .logging .LogFactory ;
35
36
import org .springframework .integration .support .MutableMessage ;
36
37
import org .springframework .integration .support .MutableMessageBuilderFactory ;
37
38
import org .springframework .integration .support .context .NamedComponent ;
39
+ import org .springframework .lang .Nullable ;
38
40
import org .springframework .messaging .Message ;
39
41
import org .springframework .messaging .support .ErrorMessage ;
40
42
import org .springframework .messaging .support .GenericMessage ;
41
43
import org .springframework .util .Assert ;
42
- import org .springframework .util .StringUtils ;
43
44
44
45
/**
45
46
* @author Mark Fisher
46
47
* @author Artem Bilan
47
48
* @author Gary Russell
49
+ *
48
50
* @since 2.0
49
51
*/
50
52
@ SuppressWarnings ("serial" )
51
53
public final class MessageHistory implements List <Properties >, Serializable {
52
54
53
- private static final Log logger = LogFactory .getLog (MessageHistory .class );
55
+ private static final Log LOGGER = LogFactory .getLog (MessageHistory .class );
54
56
55
57
private static final UnsupportedOperationException UNSUPPORTED_OPERATION_EXCEPTION_IMMUTABLE =
56
58
new UnsupportedOperationException ("MessageHistory is immutable." );
@@ -68,9 +70,11 @@ public final class MessageHistory implements List<Properties>, Serializable {
68
70
69
71
private final List <Properties > components ;
70
72
71
-
72
- public static MessageHistory read (Message <?> message ) {
73
- return message != null ? message .getHeaders ().get (HEADER_NAME , MessageHistory .class ) : null ;
73
+ @ Nullable
74
+ public static MessageHistory read (@ Nullable Message <?> message ) {
75
+ return message != null
76
+ ? message .getHeaders ().get (HEADER_NAME , MessageHistory .class )
77
+ : null ;
74
78
}
75
79
76
80
public static <T > Message <T > write (Message <T > message , NamedComponent component ) {
@@ -87,8 +91,10 @@ public static <T> Message<T> write(Message<T> messageArg, NamedComponent compone
87
91
Properties metadata = extractMetadata (component );
88
92
if (!metadata .isEmpty ()) {
89
93
MessageHistory previousHistory = message .getHeaders ().get (HEADER_NAME , MessageHistory .class );
90
- List <Properties > components = (previousHistory != null ) ?
91
- new ArrayList <Properties >(previousHistory ) : new ArrayList <Properties >();
94
+ List <Properties > components =
95
+ previousHistory != null
96
+ ? new ArrayList <>(previousHistory )
97
+ : new ArrayList <>();
92
98
components .add (metadata );
93
99
MessageHistory history = new MessageHistory (components );
94
100
@@ -111,13 +117,13 @@ else if (message instanceof AdviceMessage) {
111
117
else {
112
118
if (!(message instanceof GenericMessage ) &&
113
119
(messageBuilderFactory instanceof DefaultMessageBuilderFactory ||
114
- messageBuilderFactory instanceof MutableMessageBuilderFactory )) {
115
- if ( logger .isWarnEnabled ()) {
116
- logger . warn ( "MessageHistory rebuilds the message and produces the result of the [" +
117
- messageBuilderFactory + "], not an instance of the provided type [" +
118
- message . getClass () + "]. Consider to supply a custom MessageBuilderFactory " +
119
- "to retain custom messages during MessageHistory tracking." );
120
- }
120
+ messageBuilderFactory instanceof MutableMessageBuilderFactory )
121
+ && LOGGER .isWarnEnabled ()) {
122
+
123
+ LOGGER . warn ( "MessageHistory rebuilds the message and produces the result of the [" +
124
+ messageBuilderFactory + "], not an instance of the provided type [ " +
125
+ message . getClass () + "]. Consider to supply a custom MessageBuilderFactory " +
126
+ "to retain custom messages during MessageHistory tracking." );
121
127
}
122
128
message = messageBuilderFactory .fromMessage (message )
123
129
.setHeader (HEADER_NAME , history )
@@ -201,14 +207,10 @@ public int lastIndexOf(Object o) {
201
207
202
208
@ Override
203
209
public String toString () {
204
- List <String > names = new ArrayList <String >();
205
- for (Properties p : this .components ) {
206
- String name = p .getProperty (NAME_PROPERTY );
207
- if (name != null ) {
208
- names .add (name );
209
- }
210
- }
211
- return StringUtils .collectionToCommaDelimitedString (names );
210
+ return this .components
211
+ .stream ()
212
+ .map ((props ) -> props .getProperty (NAME_PROPERTY ))
213
+ .collect (Collectors .joining ("," ));
212
214
}
213
215
214
216
0 commit comments