View Javadoc

1   /*
2    * Copyright 2009-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.sourceforge.statelessfilter.spring;
17  
18  import javax.servlet.ServletContext;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springframework.context.ApplicationContext;
23  import org.springframework.web.context.support.WebApplicationContextUtils;
24  
25  /**
26   * Checks for Spring.
27   * 
28   * @author Nicolas Richeton
29   * 
30   */
31  public class SpringContextChecker {
32  	private static Logger log = LoggerFactory
33  			.getLogger(SpringContextChecker.class);
34  
35  	/**
36  	 * Check is Spring is available and configured in the servlet context.
37  	 * 
38  	 * @param context
39  	 * @return
40  	 */
41  	public static boolean checkForSpring(ServletContext context) {
42  		try {
43  			Class<?> contextUtils = Class
44  					.forName("org.springframework.web.context.support.WebApplicationContextUtils");
45  
46  			if (contextUtils != null) {
47  				ApplicationContext appContext = WebApplicationContextUtils
48  						.getRequiredWebApplicationContext(context);
49  				if (appContext != null) {
50  					if (log.isInfoEnabled()) {
51  						log
52  								.info("Spring is available AND an application context has been found.");
53  					}
54  					return true;
55  				}
56  			}
57  
58  			return true;
59  		} catch (Exception e) {
60  			if (log.isInfoEnabled()) {
61  				log.info("Spring is not available.");
62  			}
63  		}
64  		return false;
65  	}
66  
67  }