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.processing;
17  
18  import org.rribbit.execution.ListenerObjectExecutor;
19  import org.rribbit.retrieval.ListenerObjectRetriever;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springframework.context.ApplicationContext;
23  import org.springframework.web.context.WebApplicationContext;
24  import org.springframework.web.context.support.WebApplicationContextUtils;
25  
26  /**
27   * This implementation of {@link HttpRequestProcessorServlet} tries to retrieve a {@link ListenerObjectRetriever} and a {@link ListenerObjectExecutor} from the
28   * Spring {@link WebApplicationContext}, if one is available. Use this class if you use Spring in combination with RRiBbit HTTP remoting. Then, you only have to wire up a
29   * {@link ListenerObjectRetriever} and a {@link ListenerObjectExecutor} in your {@link ApplicationContext} and declare this class in your web.xml and the server side is done.
30   *
31   * @author G.J. Schouten
32   *
33   */
34  public class SpringHttpRequestProcessorServlet extends HttpRequestProcessorServlet {
35  
36  	private static final Logger log = LoggerFactory.getLogger(SpringHttpRequestProcessorServlet.class);
37  
38  	protected ListenerObjectRetriever listenerObjectRetriever;
39  	protected ListenerObjectExecutor listenerObjectExecutor;
40  
41  	@Override
42  	protected ListenerObjectRetriever createListenerObjectRetriever() {
43  		return listenerObjectRetriever;
44  	}
45  
46  	@Override
47  	protected ListenerObjectExecutor createListenerObjectExecutor() {
48  		return listenerObjectExecutor;
49  	}
50  
51  	@Override
52  	public void init() {
53  
54  		log.info("Retrieving WebApplicationContext from ServletContext and getting ListenerObjectRetriever and ListenerObjectExecutor");
55  
56  		//First, retrieve the WebApplicationContext via the Servlet Container and get the ListenerObjectRetriever and ListenerObjectExecutor
57  		WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
58  		listenerObjectRetriever = ctx.getBean(ListenerObjectRetriever.class);
59  		listenerObjectExecutor = ctx.getBean(ListenerObjectExecutor.class);
60  
61  		//Then call super, which in turn calls the createListenerObjectRetriever() and createListenerObjectExecutor() methods
62  		super.init();
63  	}
64  }