1 /*
2 * Copyright (C) 2012-2025 RRiBbit.org
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.rribbit.util;
17
18 import org.rribbit.DefaultRequestResponseBus;
19 import org.rribbit.RRB;
20 import org.rribbit.RequestResponseBus;
21 import org.rribbit.creation.ListenerObjectCreator;
22 import org.rribbit.dispatching.LocalRequestDispatcher;
23 import org.rribbit.dispatching.RequestDispatcher;
24 import org.rribbit.execution.ListenerObjectExecutor;
25 import org.rribbit.execution.MultiThreadedListenerObjectExecutor;
26 import org.rribbit.processing.LocalRequestProcessor;
27 import org.rribbit.retrieval.CachedListenerObjectRetriever;
28 import org.rribbit.retrieval.ListenerObjectRetriever;
29
30 /**
31 * This class simplifies setting up RRiBbit for use.
32 *
33 * @author G.J. Schouten
34 *
35 */
36 public final class RRiBbitUtil {
37
38 /**
39 * This class should not be instantiated.
40 */
41 private RRiBbitUtil() {}
42
43 /**
44 * Takes care of setting up the various objects needed to use RRiBbit locally. You have to specify the {@link ListenerObjectCreator}, because it's impossible for
45 * {@link RRiBbitUtil} to guess a sensible default here, but all of the other objects will be instantiated by {@link RRiBbitUtil}. If you want different objects
46 * than the defaults, or if you want to use RRiBbit remotely over the network, you will have to wire them together yourself.
47 * <p />
48 * The classes that are chosen:
49 * <ul>
50 * <li>{@link CachedListenerObjectRetriever}</li>
51 * <li>{@link MultiThreadedListenerObjectExecutor}</li>
52 * <li>{@link LocalRequestProcessor}</li>
53 * <li>{@link LocalRequestDispatcher}</li>
54 * <li>{@link DefaultRequestResponseBus}</li>
55 * </ul>
56 *
57 * @param listenerObjectCreator The {@link ListenerObjectCreator} to use
58 * @param setInRRB Whether or not to set the created {@link RequestResponseBus} in {@link RRB}.
59 * @return A {@link RequestResponseBus} ready for local use.
60 */
61 public static RequestResponseBus createRequestResponseBusForLocalUse(ListenerObjectCreator listenerObjectCreator, boolean setInRRB) {
62
63 ListenerObjectRetriever listenerObjectRetriever = new CachedListenerObjectRetriever(listenerObjectCreator);
64 ListenerObjectExecutor listenerObjectExecutor = new MultiThreadedListenerObjectExecutor();
65 LocalRequestProcessor localRequestProcessor = new LocalRequestProcessor(listenerObjectRetriever, listenerObjectExecutor);
66 RequestDispatcher requestDispatcher = new LocalRequestDispatcher(localRequestProcessor);
67 RequestResponseBus requestResponseBus = new DefaultRequestResponseBus(requestDispatcher);
68 if(setInRRB) {
69 RRB.setRequestResponseBus(requestResponseBus);
70 }
71 return requestResponseBus;
72 }
73 }