LocalRequestProcessor.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.processing;

import java.util.Collection;

import org.rribbit.ListenerObject;
import org.rribbit.Request;
import org.rribbit.Response;
import org.rribbit.dispatching.LocalRequestDispatcher;
import org.rribbit.execution.ListenerObjectExecutor;
import org.rribbit.retrieval.ListenerObjectRetriever;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class processes requests on a local machine and exposes its functionality as a simple Java method call. This class is supposed to be used in conjunction with the
 * {@link LocalRequestDispatcher}.
 *
 * @author G.J. Schouten
 *
 */
public class LocalRequestProcessor implements RequestProcessor {

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

	protected ListenerObjectRetriever listenerObjectRetriever;
	protected ListenerObjectExecutor listenerObjectExecutor;

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

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

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

		log.info("Processing Request");

		log.debug("Getting ListenerObjects");
		Collection<ListenerObject> listenerObjects;
		if(request.getDesiredReturnType() == null) {
			if(request.getHint() == null) {
				listenerObjects = listenerObjectRetriever.getListenerObjects();
			} else {
				listenerObjects = listenerObjectRetriever.getListenerObjects(request.getHint());
			}
		} else {
			if(request.getHint() == null) {
				listenerObjects = listenerObjectRetriever.getListenerObjects(request.getDesiredReturnType());
			} else {
				listenerObjects = listenerObjectRetriever.getListenerObjects(request.getDesiredReturnType(), request.getHint());
			}
		}

		log.debug("Executing ListenerObjects");
		Response<T> response = listenerObjectExecutor.executeListeners(listenerObjects, request.getParameters());
		log.info("Returning Response");
		return response;
	}

	public ListenerObjectRetriever getListenerObjectRetriever() {
		return listenerObjectRetriever;
	}

	public void setListenerObjectRetriever(ListenerObjectRetriever listenerObjectRetriever) {
		this.listenerObjectRetriever = listenerObjectRetriever;
	}

	public ListenerObjectExecutor getListenerObjectExecutor() {
		return listenerObjectExecutor;
	}

	public void setListenerObjectExecutor(ListenerObjectExecutor listenerObjectExecutor) {
		this.listenerObjectExecutor = listenerObjectExecutor;
	}
}