View Javadoc
1   /*
2    * #%L
3    * settings4j
4    * ===============================================================
5    * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.settings4j.helper.web;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.io.UnsupportedEncodingException;
25  import java.util.List;
26  import java.util.Map.Entry;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import javax.servlet.ServletContext;
31  
32  import org.settings4j.Connector;
33  import org.settings4j.Settings4j;
34  import org.settings4j.connector.PropertyFileConnector;
35  
36  /**
37   * Implementation which loads Default Properties from web.xml Init-Paramters and adds a Property connector to Settings4j.
38   * <p>
39   * See Example {@link DefaultPropertiesLoaderListener}.
40   * </p>
41   *
42   * @author brabenetz
43   */
44  public class DefaultPropertiesLoader {
45  
46      /** General Logger for this Class. */
47      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(DefaultPropertiesLoader.class);
48  
49      /**
50       * The Name of the Connector which will be added to the Settings4j Config.
51       */
52      public static final String CONNECTOR_NAME = "DefaultPropertiesFromWebXml";
53  
54      /**
55       * The Init-Parameter Name from the web.xml from where the default properties can be used.
56       */
57      public static final String DEFAULT_PROPERTIES = "settings4jDefaultProperties";
58  
59      /**
60       * If the InitParameter "settings4jDefaultProperties" exists in the given {@link ServletContext}, then a Connector
61       * will be added to Settings4j.
62       *
63       * @param servletContext The ServletContext where the InitParameters are configured.
64       */
65      public void initDefaultProperties(final ServletContext servletContext) {
66          synchronized (Settings4j.getSettingsRepository().getSettings()) {
67              if (Settings4j.getSettingsRepository().getSettings().getConnector(CONNECTOR_NAME) == null) {
68                  addPropertyConnector(servletContext);
69              } else {
70                  LOG.info("{} Connector already exists in Settings4j", CONNECTOR_NAME);
71              }
72          }
73      }
74  
75      private void addPropertyConnector(final ServletContext servletContext) {
76          if (servletContext.getInitParameter(DEFAULT_PROPERTIES) != null) {
77              final Properties property = getDefaultProperties(servletContext);
78              if (LOG.isDebugEnabled()) {
79                  LOG.debug("Add Property Connector '{}' to the Settings4j Repository.", CONNECTOR_NAME);
80                  final Set<Entry<Object, Object>> entries = property.entrySet();
81                  for (final Entry<Object, Object> entry : entries) {
82                      LOG.debug("{} = {}", entry.getKey(), entry.getValue());
83                  }
84              }
85              addPropertyConnector(property);
86          } else {
87              LOG.debug("No InitParameter 'settings4jDefaultProperties' found.");
88          }
89      }
90  
91      private void addPropertyConnector(final Properties property) {
92          final PropertyFileConnector propertyFileConnector = new PropertyFileConnector();
93          propertyFileConnector.setName(CONNECTOR_NAME);
94          propertyFileConnector.setProperty(property);
95          Settings4j.getSettingsRepository().getSettings().addConnector(propertyFileConnector);
96          if (LOG.isDebugEnabled()) {
97              final List<Connector> connectors = Settings4j.getConnectors();
98              LOG.debug("Current Connectors are {}", connectors.size());
99              for (final Connector connector : connectors) {
100                 LOG.debug("Connector: {}", connector.getName());
101             }
102 
103         }
104     }
105 
106     private Properties getDefaultProperties(final ServletContext servletContext) {
107         final String defaultProperties = servletContext.getInitParameter(DEFAULT_PROPERTIES);
108         final Properties property = new Properties();
109         try {
110             property.load(new ByteArrayInputStream(defaultProperties.getBytes("ISO-8859-1")));
111         } catch (final UnsupportedEncodingException e) {
112             // every JDK must have the ISO-8859-1 Charset...
113             throw new IllegalStateException(e.getMessage(), e);
114         } catch (final IOException e) {
115             // IOException never happens in ByteArrayInputStream implementation...
116             throw new IllegalStateException(e.getMessage(), e);
117         }
118         return property;
119     }
120 }