RRiBbitUtil.java

/*
 * Copyright (C) 2012-2024 RRiBbit.org
 *
 * 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
 *
 * https://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.rribbit.util;

import org.rribbit.DefaultRequestResponseBus;
import org.rribbit.RRB;
import org.rribbit.RequestResponseBus;
import org.rribbit.creation.ListenerObjectCreator;
import org.rribbit.dispatching.LocalRequestDispatcher;
import org.rribbit.dispatching.RequestDispatcher;
import org.rribbit.execution.ListenerObjectExecutor;
import org.rribbit.execution.MultiThreadedListenerObjectExecutor;
import org.rribbit.processing.LocalRequestProcessor;
import org.rribbit.retrieval.CachedListenerObjectRetriever;
import org.rribbit.retrieval.ListenerObjectRetriever;

/**
 * This class simplifies setting up RRiBbit for use.
 *
 * @author G.J. Schouten
 *
 */
public final class RRiBbitUtil {

	/**
	 * This class should not be instantiated.
	 */
	private RRiBbitUtil() {}

	/**
	 * 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
	 * {@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
	 * than the defaults, or if you want to use RRiBbit remotely over the network, you will have to wire them together yourself.
	 * <p />
	 * The classes that are chosen:
	 * <ul>
	 * 	<li>{@link CachedListenerObjectRetriever}</li>
	 * 	<li>{@link MultiThreadedListenerObjectExecutor}</li>
	 * 	<li>{@link LocalRequestProcessor}</li>
	 * 	<li>{@link LocalRequestDispatcher}</li>
	 * 	<li>{@link DefaultRequestResponseBus}</li>
	 * </ul>
	 *
	 * @param listenerObjectCreator		The {@link ListenerObjectCreator} to use
	 * @param setInRRB					Whether or not to set the created {@link RequestResponseBus} in {@link RRB}.
	 * @return							A {@link RequestResponseBus} ready for local use.
	 */
	public static RequestResponseBus createRequestResponseBusForLocalUse(ListenerObjectCreator listenerObjectCreator, boolean setInRRB) {

		ListenerObjectRetriever listenerObjectRetriever = new CachedListenerObjectRetriever(listenerObjectCreator);
		ListenerObjectExecutor listenerObjectExecutor = new MultiThreadedListenerObjectExecutor();
		LocalRequestProcessor localRequestProcessor = new LocalRequestProcessor(listenerObjectRetriever, listenerObjectExecutor);
		RequestDispatcher requestDispatcher = new LocalRequestDispatcher(localRequestProcessor);
		RequestResponseBus requestResponseBus = new DefaultRequestResponseBus(requestDispatcher);
		if(setInRRB) {
			RRB.setRequestResponseBus(requestResponseBus);
		}
		return requestResponseBus;
	}
}