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.support.spring;
17  
18  import net.sf.appstatus.core.IObjectInstantiationListener;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springframework.beans.BeansException;
23  import org.springframework.context.ApplicationContext;
24  import org.springframework.context.ApplicationContextAware;
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, ApplicationContextAware {
35  	private static Logger logger = LoggerFactory
36  			.getLogger(SpringObjectInstantiationListener.class);
37  
38  	ApplicationContext applicationContext;
39  
40  	/**
41  	 * Constructor.
42  	 * 
43  	 * @param context
44  	 *            Current servlet context.
45  	 */
46  	public SpringObjectInstantiationListener() {
47  	}
48  
49  	public Object getInstance(String className) {
50  		Object obj = null;
51  
52  		try {
53  			obj = applicationContext.getBean(className);
54  		} catch (BeansException e) {
55  			logger.info("Unable to get Bean {}", e, className);
56  		}
57  
58  		return obj;
59  	}
60  
61  	public void setApplicationContext(ApplicationContext applicationContext)
62  			throws BeansException {
63  		this.applicationContext = applicationContext;
64  	}
65  
66  }