View Javadoc
1   /*
2    * Copyright (C) 2012-2024 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.dispatching;
17  
18  import org.rribbit.Request;
19  import org.rribbit.Response;
20  import org.rribbit.processing.LocalRequestProcessor;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  /**
25   * This {@link RequestDispatcher} dispatches a {@link Request} on a local machine to a {@link LocalRequestProcessor} via a simple Java method call.
26   *
27   * @author G.J. Schouten
28   *
29   */
30  public class LocalRequestDispatcher implements RequestDispatcher {
31  
32  	private static final Logger log = LoggerFactory.getLogger(LocalRequestDispatcher.class);
33  
34  	protected LocalRequestProcessor localRequestProcessor;
35  
36  	/**
37  	 * Whenever you use this constructor, be sure to set the {@link LocalRequestProcessor} with the setter provided by this class.
38  	 * If you don't, runtime {@link NullPointerException}s will occur.
39  	 */
40  	public LocalRequestDispatcher() {}
41  
42  	/**
43  	 * This constructor is recommended, since it forces you to specify the {@link LocalRequestProcessor}. Passing a null value for either
44  	 * of these will result in a runtime {@link NullPointerException} whenever the {@link LocalRequestDispatcher} is used.
45  	 *
46  	 * @param localRequestProcessor
47  	 */
48  	public LocalRequestDispatcher(LocalRequestProcessor localRequestProcessor) {
49  		this.localRequestProcessor = localRequestProcessor;
50  	}
51  
52  	@Override
53  	public <T> Response<T> dispatchRequest(Request request) {
54  
55  		log.info("Dispatching Request");
56  		Response<T> response = localRequestProcessor.processRequest(request);
57  		log.info("Returning Response");
58  		return response;
59  	}
60  
61  	public LocalRequestProcessor getLocalRequestProcessor() {
62  		return localRequestProcessor;
63  	}
64  
65  	public void setLocalRequestProcessor(LocalRequestProcessor localRequestProcessor) {
66  		this.localRequestProcessor = localRequestProcessor;
67  	}
68  }