1 |
| package photospace.web.spring; |
2 |
| |
3 |
| import java.io.*; |
4 |
| import java.util.*; |
5 |
| import org.apache.commons.logging.*; |
6 |
| import org.springframework.beans.factory.config.*; |
7 |
| import org.springframework.core.io.*; |
8 |
| import org.springframework.web.context.support.*; |
9 |
| |
10 |
| public class ConfigurableXmlWebApplicationContext |
11 |
| extends XmlWebApplicationContext |
12 |
| { |
13 |
| private static final Log log = LogFactory.getLog(ConfigurableXmlWebApplicationContext.class); |
14 |
| |
15 |
| private static final String PROPERTIES_PARAM = "photospace.properties"; |
16 |
| |
17 |
0
| protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
18 |
| { |
19 |
0
| super.postProcessBeanFactory(beanFactory);
|
20 |
| |
21 |
0
| if (getServletContext().getInitParameter(PROPERTIES_PARAM) != null)
|
22 |
| { |
23 |
0
| String location = getServletContext().getInitParameter(PROPERTIES_PARAM);
|
24 |
0
| setPropertiesLocation(beanFactory, location);
|
25 |
| } |
26 |
0
| addPropertiesToServletContext(beanFactory);
|
27 |
| } |
28 |
| |
29 |
0
| private void setPropertiesLocation(ConfigurableListableBeanFactory beanFactory, String location)
|
30 |
| { |
31 |
0
| log.info("Configuring application using " + location);
|
32 |
0
| BeanDefinition configurer = beanFactory.getBeanDefinition("propertyConfigurer");
|
33 |
0
| configurer.getPropertyValues().addPropertyValue("location", new FileSystemResource(location));
|
34 |
| } |
35 |
| |
36 |
0
| private void addPropertiesToServletContext(ConfigurableListableBeanFactory beanFactory)
|
37 |
| { |
38 |
0
| Object location = beanFactory.getBeanDefinition("propertyConfigurer").getPropertyValues().getPropertyValue("location").getValue();
|
39 |
0
| try
|
40 |
| { |
41 |
0
| Properties properties = new Properties();
|
42 |
0
| if (location instanceof FileSystemResource)
|
43 |
| { |
44 |
0
| properties.load(new FileInputStream(((FileSystemResource) location).getFile()));
|
45 |
| } |
46 |
0
| else if (location instanceof String)
|
47 |
| { |
48 |
0
| properties.load(getServletContext().getResourceAsStream((String) location));
|
49 |
| } |
50 |
| else |
51 |
| { |
52 |
0
| throw new IllegalStateException("Not sure how to load properties from " + location
|
53 |
| + "(" + location.getClass().getName() + ")"); |
54 |
| } |
55 |
0
| for (Iterator i = properties.entrySet().iterator(); i.hasNext();)
|
56 |
| { |
57 |
0
| Map.Entry entry = (Map.Entry) i.next();
|
58 |
0
| getServletContext().setAttribute((String) entry.getKey(), entry.getValue());
|
59 |
| } |
60 |
| } |
61 |
| catch (Exception e) |
62 |
| { |
63 |
0
| log.warn("Unable to load properties from " + location, e);
|
64 |
| } |
65 |
| } |
66 |
| } |