1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
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 | package net.sourceforge.hiveutils.web.util; |
16 | |
17 | import java.util.Enumeration; |
18 | |
19 | import javax.servlet.ServletContext; |
20 | import javax.servlet.ServletContextEvent; |
21 | import javax.servlet.ServletContextListener; |
22 | |
23 | /** |
24 | * ServletContextListener that will initialize System properties with values |
25 | * that are provided as parameters to the listener. |
26 | * <p> |
27 | * This allows to use the container mechanisms (like context.xml in Tomcat) to |
28 | * externalize these properties outside of the war file. |
29 | * <p> |
30 | * This listener will put all parameters with a name starting with "init." into |
31 | * the System properties. |
32 | * |
33 | * @author Jean-Francois Poilpret |
34 | */ |
35 | public class SystemPropertyInitListener implements ServletContextListener |
36 | { |
37 | static final private String INIT_PREFIX = "init."; |
38 | |
39 | public void contextInitialized(ServletContextEvent evt) |
40 | { |
41 | // Get information from init parameters |
42 | ServletContext context = evt.getServletContext(); |
43 | Enumeration params = context.getInitParameterNames(); |
44 | while (params.hasMoreElements()) |
45 | { |
46 | String name = (String) params.nextElement(); |
47 | if (name.startsWith(INIT_PREFIX)) |
48 | { |
49 | String value = context.getInitParameter(name); |
50 | name = name.substring(INIT_PREFIX.length()); |
51 | System.setProperty(name, value); |
52 | } |
53 | } |
54 | } |
55 | |
56 | public void contextDestroyed(ServletContextEvent evt) |
57 | { |
58 | } |
59 | } |