/
AlfrescoEventsAutoConfiguration.java
133 lines (120 loc) · 6.07 KB
/
AlfrescoEventsAutoConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright 2021-2021 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.alfresco.event.sdk.autoconfigure;
import org.alfresco.event.sdk.handling.EventHandlingExecutor;
import org.alfresco.event.sdk.handling.EventHandlingRegistry;
import org.alfresco.event.sdk.handling.SimpleEventHandlingExecutor;
import org.alfresco.event.sdk.handling.handler.EventHandler;
import org.alfresco.event.sdk.integration.EventChannels;
import org.alfresco.event.sdk.integration.transformer.EventGenericTransformer;
import org.alfresco.repo.event.v1.model.DataAttributes;
import org.alfresco.repo.event.v1.model.RepoEvent;
import org.alfresco.repo.event.v1.model.Resource;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.core.GenericTransformer;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.integration.jms.dsl.Jms;
import jakarta.jms.Topic;
import java.util.List;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} for Alfresco Event Java API.
* <p>
* This auto-configuration defines the beans required to read the Alfresco JSON events from an ActiveMQ topic, transform that event from JSON to {@link
* RepoEvent} making use of the {@link EventGenericTransformer} and do up to two things:
* <ul>
* <li>Forward a Spring Integration {@link org.springframework.messaging.Message} to a publish-subscribe channel</li>
* <li>Invoke the list of {@link EventHandler} registered to handle the specific event type consumed from the topic</li>
* </ul>
* <p>
* This way the integrator can configure the Alfresco Java Event API to consume the events using Spring Integration or plain Java event handlers.
*/
@AutoConfiguration(after = { IntegrationAutoConfiguration.class, ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class })
@ConditionalOnClass(RepoEvent.class)
@EnableConfigurationProperties(AlfrescoEventsProperties.class)
public class AlfrescoEventsAutoConfiguration {
@Autowired
private AlfrescoEventsProperties alfrescoEventsProperties;
// CORE INTEGRATION WITH BROKER CONFIGURATION
@Bean
public IntegrationFlow acsEventsListeningFlow(
final ActiveMQConnectionFactory activeMQConnectionFactory,
@Qualifier(EventChannels.ERROR) final DirectChannel acsEventErrorChannel,
final Topic acsEventsTopic,
final GenericTransformer<String, RepoEvent<DataAttributes<Resource>>> acsEventTransformer
) {
return IntegrationFlow.from(Jms.messageDrivenChannelAdapter(activeMQConnectionFactory)
.destination(acsEventsTopic)
.errorChannel(acsEventErrorChannel))
.transform(acsEventTransformer)
.routeToRecipients(route -> route
.recipient(EventChannels.SPRING_INTEGRATION, s -> alfrescoEventsProperties.isEnableSpringIntegration())
.recipient(EventChannels.HANDLERS, s -> alfrescoEventsProperties.isEnableHandlers()))
.get();
}
@Bean
public Topic acsEventsTopic() {
return new ActiveMQTopic(alfrescoEventsProperties.getTopicName());
}
@Bean(name = EventChannels.ERROR)
public DirectChannel acsEventErrorChannel() {
return new DirectChannel();
}
@Bean
public GenericTransformer<String, RepoEvent<DataAttributes<Resource>>> acsEventTransformer() {
return new EventGenericTransformer();
}
// SPRING INTEGRATION OPTION CONFIGURATION
@Bean
public IntegrationFlow acsEventsSpringIntegrationFlow(@Qualifier(EventChannels.MAIN) PublishSubscribeChannel acsEventChannel) {
return IntegrationFlow.from(EventChannels.SPRING_INTEGRATION)
.log(LoggingHandler.Level.DEBUG)
.channel(acsEventChannel)
.get();
}
@Bean(name = EventChannels.MAIN)
public PublishSubscribeChannel acsEventChannel() {
return new PublishSubscribeChannel();
}
// PLAIN HANDLERS OPTION CONFIGURATION
@Bean
public IntegrationFlow acsEventsHandlersFlow(EventHandlingExecutor eventHandlingExecutor) {
return IntegrationFlow.from(EventChannels.HANDLERS)
.log(LoggingHandler.Level.DEBUG)
.handle(s -> eventHandlingExecutor.executeEventHandlers((RepoEvent<DataAttributes<Resource>>) s.getPayload()))
.get();
}
@Bean
public EventHandlingExecutor eventHandlingExecutor(EventHandlingRegistry eventHandlingRegistry) {
return new SimpleEventHandlingExecutor(eventHandlingRegistry);
}
@Bean
public EventHandlingRegistry eventHandlingRegistry(List<EventHandler> eventHandlers) {
return new EventHandlingRegistry(eventHandlers);
}
}