1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.settings4j.helper.web;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.UnsupportedEncodingException;
22 import java.util.List;
23 import java.util.Map.Entry;
24 import java.util.Properties;
25 import java.util.Set;
26
27 import javax.servlet.ServletContext;
28
29 import org.settings4j.Connector;
30 import org.settings4j.Settings4j;
31 import org.settings4j.connector.PropertyFileConnector;
32
33
34
35
36
37
38
39
40
41 public class DefaultPropertiesLoader {
42
43
44 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(DefaultPropertiesLoader.class);
45
46
47
48
49 public static final String CONNECTOR_NAME = "DefaultPropertiesFromWebXml";
50
51
52
53
54 public static final String DEFAULT_PROPERTIES = "settings4jDefaultProperties";
55
56
57
58
59
60
61
62 public void initDefaultProperties(final ServletContext servletContext) {
63 synchronized (Settings4j.getSettingsRepository().getSettings()) {
64 if (Settings4j.getSettingsRepository().getSettings().getConnector(CONNECTOR_NAME) == null) {
65 addPropertyConnector(servletContext);
66 } else {
67 LOG.info("{} Connector already exists in Settings4j", CONNECTOR_NAME);
68 }
69 }
70 }
71
72 private void addPropertyConnector(final ServletContext servletContext) {
73 if (servletContext.getInitParameter(DEFAULT_PROPERTIES) != null) {
74 final Properties property = getDefaultProperties(servletContext);
75 if (LOG.isDebugEnabled()) {
76 LOG.debug("Add Property Connector '{}' to the Settings4j Repository.", CONNECTOR_NAME);
77 final Set<Entry<Object, Object>> entries = property.entrySet();
78 for (Entry<Object, Object> entry : entries) {
79 LOG.debug("{} = {}", entry.getKey(), entry.getValue());
80 }
81 }
82 addPropertyConnector(property);
83 } else {
84 LOG.debug("No InitParameter 'settings4jDefaultProperties' found.");
85 }
86 }
87
88 private void addPropertyConnector(final Properties property) {
89 final PropertyFileConnector propertyFileConnector = new PropertyFileConnector();
90 propertyFileConnector.setName(CONNECTOR_NAME);
91 propertyFileConnector.setProperty(property);
92 Settings4j.getSettingsRepository().getSettings().addConnector(propertyFileConnector);
93 if (LOG.isDebugEnabled()) {
94 final List<Connector> connectors = Settings4j.getConnectors();
95 LOG.debug("Current Connectors are {}", connectors.size());
96 for (Connector connector : connectors) {
97 LOG.debug("Connector: {}", connector.getName());
98 }
99
100 }
101 }
102
103 private Properties getDefaultProperties(final ServletContext servletContext) {
104 final String defaultProperties = servletContext.getInitParameter(DEFAULT_PROPERTIES);
105 final Properties property = new Properties();
106 try {
107 property.load(new ByteArrayInputStream(defaultProperties.getBytes("ISO-8859-1")));
108 } catch (final UnsupportedEncodingException e) {
109
110 throw new IllegalStateException(e.getMessage());
111 } catch (final IOException e) {
112
113 throw new IllegalStateException(e.getMessage());
114 }
115 return property;
116 }
117 }