Skip to content

Package: MessageBusAdapterFactory

MessageBusAdapterFactory

nameinstructionbranchcomplexitylinemethod
createMethodAdapter(Object, Method, Class)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
publish(Class, Object)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
publish(Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * 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
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.messagebus.impl.spring;
27:
28: import java.lang.reflect.Method;
29: import javax.annotation.Nonnull;
30: import it.tidalwave.util.annotation.VisibleForTesting;
31: import it.tidalwave.messagebus.MessageBus;
32: import it.tidalwave.messagebus.MessageBusHelper;
33: import it.tidalwave.messagebus.MessageBusHelper.MethodAdapter;
34: import lombok.Getter;
35: import lombok.RequiredArgsConstructor;
36: import lombok.ToString;
37: import lombok.extern.slf4j.Slf4j;
38:
39: /***************************************************************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: *
43: **************************************************************************************************************************************************************/
44: @RequiredArgsConstructor @Slf4j
45: public class MessageBusAdapterFactory implements MessageBusHelper.Adapter
46: {
47: @Nonnull
48: private final MessageBus messageBus;
49:
50: /***********************************************************************************************************************************************************
51: *
52: **********************************************************************************************************************************************************/
53: @Getter @VisibleForTesting @ToString(of = "method")
54: class MessageBusListenerAdapter<T> implements MethodAdapter<T>, MessageBus.Listener<T>
55: {
56: @Nonnull
57: private final Object owner;
58:
59: @Nonnull
60: private final Method method;
61:
62: @Nonnull
63: private final Class<T> topic;
64:
65: public MessageBusListenerAdapter (@Nonnull final Object owner,
66: @Nonnull final Method method,
67: @Nonnull final Class<T> topic)
68: {
69: this.owner = owner;
70: this.method = method;
71: this.topic = topic;
72: method.setAccessible(true);
73: }
74:
75: @Override
76: public void notify (@Nonnull final T message)
77: {
78: log.trace("notify({})", message);
79:
80: try
81: {
82: method.invoke(owner, message);
83: }
84: catch (Throwable t)
85: {
86: log.error("Error calling {} with {}", method, message.getClass());
87: log.error("", t);
88: }
89: }
90:
91: @Override
92: public void subscribe()
93: {
94: messageBus.subscribe(topic, this);
95: }
96:
97: @Override
98: public void unsubscribe()
99: {
100: messageBus.unsubscribe(this);
101: }
102: }
103:
104: /***********************************************************************************************************************************************************
105: * {@inheritDoc}
106: **********************************************************************************************************************************************************/
107: @Override @Nonnull
108: public <T> MethodAdapter<T> createMethodAdapter (@Nonnull final Object owner,
109: @Nonnull final Method method,
110: @Nonnull final Class<T> topic)
111: {
112: return new MessageBusListenerAdapter<>(owner, method, topic);
113: }
114:
115: /***********************************************************************************************************************************************************
116: * {@inheritDoc}
117: **********************************************************************************************************************************************************/
118: @Override
119: public void publish (@Nonnull final Object message)
120: {
121: messageBus.publish(message);
122: }
123:
124: /***********************************************************************************************************************************************************
125: * {@inheritDoc}
126: **********************************************************************************************************************************************************/
127: @Override
128: public <T> void publish (@Nonnull final Class<T> topic, @Nonnull final T message)
129: {
130: messageBus.publish(topic, message);
131: }
132: }