View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Implementation which loads Default Properties from web.xml Init-Paramters and adds a Property connector to
35   * Settings4j.
36   * <p>
37   * See Example {@link DefaultPropertiesLoaderListener}.
38   * 
39   * @author brabenetz
40   */
41  public class DefaultPropertiesLoader {
42  
43      /** General Logger for this Class. */
44      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(DefaultPropertiesLoader.class);
45  
46      /**
47       * The Name of the Connector which will be added to the Settings4j Config.
48       */
49      public static final String CONNECTOR_NAME = "DefaultPropertiesFromWebXml";
50  
51      /**
52       * The Init-Parameter Name from the web.xml from where the default properties can be used.
53       */
54      public static final String DEFAULT_PROPERTIES = "settings4jDefaultProperties";
55  
56      /**
57       * If the InitParameter "settings4jDefaultProperties" exists in the given {@link ServletContext}, then a Connector
58       * will be added to Settings4j.
59       * 
60       * @param servletContext The ServletContext where the InitParameters are configured.
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             // every JDK must have the ISO-8859-1 Charset...
110             throw new IllegalStateException(e.getMessage());
111         } catch (final IOException e) {
112             // IOException never happens in ByteArrayInputStream implementation...
113             throw new IllegalStateException(e.getMessage());
114         }
115         return property;
116     }
117 }