View Javadoc
1   /*
2    * Copyright 2010 Capgemini
3    * Licensed under the Apache License, Version 2.0 (the "License"); 
4    * you may not use this file except in compliance with the License. 
5    * You may obtain a copy of the License at 
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0 
8    * 
9    * Unless required by applicable law or agreed to in writing, software 
10   * distributed under the License is distributed on an "AS IS" BASIS, 
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
12   * See the License for the specific language governing permissions and 
13   * limitations under the License. 
14   * 
15   */
16  package net.sf.appstatus.web;
17  
18  import javax.servlet.ServletContext;
19  
20  import net.sf.appstatus.core.IObjectInstantiationListener;
21  
22  import org.springframework.beans.BeansException;
23  import org.springframework.web.context.WebApplicationContext;
24  import org.springframework.web.context.support.WebApplicationContextUtils;
25  
26  /**
27   * 
28   * Spring-aware instantiation listener for web applications.
29   * 
30   * @author Nicolas Richeton
31   * 
32   */
33  public class SpringObjectInstantiationListener implements
34  		IObjectInstantiationListener {
35  	WebApplicationContext webApplicationContext = null;
36  
37  	/**
38  	 * Constructor.
39  	 * 
40  	 * @param context
41  	 *            Current servlet context.
42  	 */
43  	public SpringObjectInstantiationListener(ServletContext context) {
44  		webApplicationContext = WebApplicationContextUtils
45  				.getRequiredWebApplicationContext(context);
46  	}
47  
48  	public Object getInstance(String className) {
49  		Object obj = null;
50  
51  		try {
52  			obj = webApplicationContext.getBean(className);
53  		} catch (BeansException e) {
54  		}
55  
56  		return obj;
57  	}
58  
59  }