LocalRequestDispatcher.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.dispatching;

import org.rribbit.Request;
import org.rribbit.Response;
import org.rribbit.processing.LocalRequestProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This {@link RequestDispatcher} dispatches a {@link Request} on a local machine to a {@link LocalRequestProcessor} via a simple Java method call.
 *
 * @author G.J. Schouten
 *
 */
public class LocalRequestDispatcher implements RequestDispatcher {

	private static final Logger log = LoggerFactory.getLogger(LocalRequestDispatcher.class);

	protected LocalRequestProcessor localRequestProcessor;

	/**
	 * Whenever you use this constructor, be sure to set the {@link LocalRequestProcessor} with the setter provided by this class.
	 * If you don't, runtime {@link NullPointerException}s will occur.
	 */
	public LocalRequestDispatcher() {}

	/**
	 * This constructor is recommended, since it forces you to specify the {@link LocalRequestProcessor}. Passing a null value for either
	 * of these will result in a runtime {@link NullPointerException} whenever the {@link LocalRequestDispatcher} is used.
	 *
	 * @param localRequestProcessor
	 */
	public LocalRequestDispatcher(LocalRequestProcessor localRequestProcessor) {
		this.localRequestProcessor = localRequestProcessor;
	}

	@Override
	public <T> Response<T> dispatchRequest(Request request) {

		log.info("Dispatching Request");
		Response<T> response = localRequestProcessor.processRequest(request);
		log.info("Returning Response");
		return response;
	}

	public LocalRequestProcessor getLocalRequestProcessor() {
		return localRequestProcessor;
	}

	public void setLocalRequestProcessor(LocalRequestProcessor localRequestProcessor) {
		this.localRequestProcessor = localRequestProcessor;
	}
}