View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * TheseFoolishThings: Miscellaneous utilities
5    * http://tidalwave.it/projects/thesefoolishthings
6    *
7    * Copyright (C) 2009 - 2026 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 jakarta.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   * An implementation of {@link MessageBusHelper.Adapter} that subscribes listener methods to a given {@link MessageBus}.
41   * @author  Fabrizio Giudici
42   **************************************************************************************************************************************************************/
43  @RequiredArgsConstructor @Slf4j
44  public class MessageBusAdapterFactory implements MessageBusHelper.Adapter
45    {
46      /** The message bus. */
47      @Nonnull
48      private final MessageBus messageBus;
49  
50      /***********************************************************************************************************************************************************
51       * A listener that delivers messages to a method of an object, and that can subscribe and unsubscribe itself.
52       * @param  <T>  the static type of the topic
53       **********************************************************************************************************************************************************/
54      @Getter @VisibleForTesting @ToString(of = "method")
55      class MessageBusListenerAdapter<T> implements MethodAdapter<T>, MessageBus.Listener<T>
56        {
57          @Nonnull
58          private final Object owner;
59  
60          @Nonnull
61          private final Method method;
62  
63          @Nonnull
64          private final Class<T> topic;
65  
66          /*******************************************************************************************************************************************************
67           * Creates a new instance.
68           * @param   owner     the object that owns the method
69           * @param   method    the listener method
70           * @param   topic     the topic
71           ******************************************************************************************************************************************************/
72          public MessageBusListenerAdapter (@Nonnull final Object owner,
73                                            @Nonnull final Method method,
74                                            @Nonnull final Class<T> topic)
75            {
76              this.owner  = owner;
77              this.method = method;
78              this.topic  = topic;
79              method.setAccessible(true);
80            }
81  
82          /*******************************************************************************************************************************************************
83           * {@inheritDoc} Exceptions thrown by the listener method are logged.
84           ******************************************************************************************************************************************************/
85          @Override
86          public void notify (@Nonnull final T message)
87            {
88              log.trace("notify({})", message);
89  
90              try
91                {
92                  method.invoke(owner, message);
93                }
94              catch (Throwable t)
95                {
96                  log.error("Error calling {} with {}", method, message.getClass());
97                  log.error("", t);
98                }
99            }
100 
101         /*******************************************************************************************************************************************************
102          * {@inheritDoc}
103          ******************************************************************************************************************************************************/
104         @Override
105         public void subscribe()
106           {
107             messageBus.subscribe(topic, this);
108           }
109 
110         /*******************************************************************************************************************************************************
111          * {@inheritDoc}
112          ******************************************************************************************************************************************************/
113         @Override
114         public void unsubscribe()
115           {
116             messageBus.unsubscribe(this);
117           }
118       }
119 
120     /***********************************************************************************************************************************************************
121      * {@inheritDoc}
122      **********************************************************************************************************************************************************/
123     @Override @Nonnull
124     public <T> MethodAdapter<T> createMethodAdapter (@Nonnull final Object owner,
125                                                      @Nonnull final Method method,
126                                                      @Nonnull final Class<T> topic)
127       {
128         return new MessageBusListenerAdapter<>(owner, method, topic);
129       }
130 
131     /***********************************************************************************************************************************************************
132      * {@inheritDoc}
133      **********************************************************************************************************************************************************/
134     @Override
135     public void publish (@Nonnull final Object message)
136       {
137         messageBus.publish(message);
138       }
139 
140     /***********************************************************************************************************************************************************
141      * {@inheritDoc}
142      **********************************************************************************************************************************************************/
143     @Override
144     public <T> void publish (@Nonnull final Class<T> topic, @Nonnull final T message)
145       {
146         messageBus.publish(topic, message);
147       }
148   }