1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 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.
16
16
17
17
package org .springframework .integration .mongodb .store ;
18
18
19
+ import java .lang .reflect .Constructor ;
19
20
import java .util .ArrayList ;
20
21
import java .util .Arrays ;
21
22
import java .util .Collection ;
25
26
import java .util .List ;
26
27
import java .util .Map ;
27
28
import java .util .Map .Entry ;
28
- import java .util .Properties ;
29
29
import java .util .UUID ;
30
30
import java .util .stream .Collectors ;
31
31
import java .util .stream .Stream ;
36
36
import org .bson .conversions .Bson ;
37
37
import org .bson .types .Binary ;
38
38
39
+ import org .springframework .beans .BeanUtils ;
39
40
import org .springframework .beans .BeansException ;
40
41
import org .springframework .beans .DirectFieldAccessor ;
41
42
import org .springframework .beans .factory .BeanClassLoaderAware ;
@@ -342,7 +343,7 @@ public void removeMessagesFromGroup(Object groupId, Collection<Message<?>> messa
342
343
ids .clear ();
343
344
}
344
345
}
345
- if (ids .size () > 0 ) {
346
+ if (! ids .isEmpty () ) {
346
347
bulkRemove (groupId , ids );
347
348
}
348
349
updateGroup (groupId , lastModifiedUpdate ());
@@ -573,6 +574,7 @@ void setCustomConverters(Object... customConverters) {
573
574
public void afterPropertiesSet () {
574
575
List <Object > converters = new ArrayList <>();
575
576
converters .add (new MessageHistoryToDocumentConverter ());
577
+ converters .add (new DocumentToMessageHistoryConverter ());
576
578
converters .add (new DocumentToGenericMessageConverter ());
577
579
converters .add (new DocumentToMutableMessageConverter ());
578
580
DocumentToErrorMessageConverter docToErrorMessageConverter = new DocumentToErrorMessageConverter ();
@@ -720,30 +722,55 @@ private Object extractPayload(Bson source) {
720
722
721
723
722
724
@ WritingConverter
723
- private static class MessageHistoryToDocumentConverter implements Converter <MessageHistory , Document > {
725
+ private static final class MessageHistoryToDocumentConverter implements Converter <MessageHistory , Document > {
724
726
725
727
MessageHistoryToDocumentConverter () {
726
728
}
727
729
728
730
@ Override
729
731
public Document convert (MessageHistory source ) {
730
- BasicDBList dbList = new BasicDBList ();
731
- for (Properties properties : source ) {
732
- Document historyProperty = new Document ()
733
- .append (MessageHistory .NAME_PROPERTY , properties .getProperty (MessageHistory .NAME_PROPERTY ))
734
- .append (MessageHistory .TYPE_PROPERTY , properties .getProperty (MessageHistory .TYPE_PROPERTY ))
735
- .append (MessageHistory .TIMESTAMP_PROPERTY ,
736
- properties .getProperty (MessageHistory .TIMESTAMP_PROPERTY ));
737
- dbList .add (historyProperty );
738
- }
739
- return new Document ("components" , dbList )
732
+ return new Document ("components" , source )
740
733
.append ("_class" , MessageHistory .class .getName ());
741
734
}
742
735
743
736
}
744
737
745
738
@ ReadingConverter
746
- private class DocumentToGenericMessageConverter implements Converter <Document , GenericMessage <?>> {
739
+ private static final class DocumentToMessageHistoryConverter implements Converter <Document , MessageHistory > {
740
+
741
+ private static final Constructor <MessageHistory > MESSAGE_HISTORY_CONSTRUCTOR ;
742
+
743
+ static {
744
+ try {
745
+ MESSAGE_HISTORY_CONSTRUCTOR = MessageHistory .class .getDeclaredConstructor (List .class );
746
+ }
747
+ catch (NoSuchMethodException ex ) {
748
+ throw new IllegalStateException (ex );
749
+ }
750
+ }
751
+
752
+ DocumentToMessageHistoryConverter () {
753
+ }
754
+
755
+ @ Override
756
+ @ SuppressWarnings ("unchecked" )
757
+ public MessageHistory convert (Document source ) {
758
+ List <Document > components = (List <Document >) source .get ("components" );
759
+ List <MessageHistory .Entry > historyEntries = new ArrayList <>(components .size ());
760
+ for (Document component : components ) {
761
+ MessageHistory .Entry entry = new MessageHistory .Entry ();
762
+ for (Entry <String , Object > componentEntry : component .entrySet ()) {
763
+ entry .setProperty (componentEntry .getKey (), componentEntry .getValue ().toString ());
764
+ }
765
+ historyEntries .add (entry );
766
+ }
767
+ return BeanUtils .instantiateClass (MESSAGE_HISTORY_CONSTRUCTOR , historyEntries );
768
+ }
769
+
770
+ }
771
+
772
+ @ ReadingConverter
773
+ private final class DocumentToGenericMessageConverter implements Converter <Document , GenericMessage <?>> {
747
774
748
775
DocumentToGenericMessageConverter () {
749
776
}
@@ -783,7 +810,7 @@ public MutableMessage<?> convert(Document source) {
783
810
}
784
811
785
812
@ ReadingConverter
786
- private class DocumentToAdviceMessageConverter implements Converter <Document , AdviceMessage <?>> {
813
+ private final class DocumentToAdviceMessageConverter implements Converter <Document , AdviceMessage <?>> {
787
814
788
815
DocumentToAdviceMessageConverter () {
789
816
}
@@ -820,7 +847,7 @@ public AdviceMessage<?> convert(Document source) {
820
847
}
821
848
822
849
@ ReadingConverter
823
- private class DocumentToErrorMessageConverter implements Converter <Document , ErrorMessage > {
850
+ private final class DocumentToErrorMessageConverter implements Converter <Document , ErrorMessage > {
824
851
825
852
private final AllowListDeserializingConverter deserializingConverter = new AllowListDeserializingConverter ();
826
853
@@ -843,7 +870,7 @@ public ErrorMessage convert(Document source) {
843
870
}
844
871
845
872
@ WritingConverter
846
- private static class ThrowableToBytesConverter implements Converter <Throwable , byte []> {
873
+ private static final class ThrowableToBytesConverter implements Converter <Throwable , byte []> {
847
874
848
875
private final Converter <Object , byte []> serializingConverter = new SerializingConverter ();
849
876
0 commit comments