Project

General

Profile

1
package eu.dnetlib.utils;
2

    
3
import javax.servlet.ServletContextEvent;
4
import javax.servlet.ServletContextListener;
5

    
6
import org.apache.log4j.Logger;
7

    
8
/**
9
 * A simple context listener that reads the servlet context and sets the context
10
 * path as a system property. 
11
 * 
12
 * @author antleb
13
 *
14
 */
15
public class ContextPathReader implements ServletContextListener {
16
	private static Logger logger = Logger.getLogger(ContextPathReader.class);
17

    
18
	public void contextDestroyed(ServletContextEvent arg0) {
19
		// doing nothing
20
	}
21

    
22
	public void contextInitialized(ServletContextEvent event) {
23
		try {
24
			String propertyName = event.getServletContext().getInitParameter("applicationContextProperty");
25
			String path = event.getServletContext().getContextPath();
26
			
27
			if (propertyName == null)
28
				propertyName = "driver.service.context";
29
			
30
			logger.debug("Web application path: " + path + " (in property '" + propertyName + "')");
31
			
32
			if (path.startsWith("/"))
33
				path = path.substring(1);
34
			
35
			System.setProperty("driver.service.context", path);
36
		} catch (Exception e) {
37
			logger.error("Error reading application context path", e);
38
		}
39
	}
40
}
(2-2/7)