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.settings;
21  
22  import java.net.URL;
23  
24  import javax.xml.parsers.FactoryConfigurationError;
25  
26  import org.settings4j.Settings4jInstance;
27  import org.settings4j.Settings4jRepository;
28  import org.settings4j.config.DOMConfigurator;
29  import org.settings4j.contentresolver.ClasspathContentResolver;
30  
31  /**
32   * manage the {@link Settings4jRepository} which is used to store the configuration from the settings4j.xml.
33   *
34   * @author Harald.Brabenetz
35   */
36  public final class SettingsManager {
37  
38      /** General Logger for this Class. */
39      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SettingsManager.class);
40  
41      /**
42       * The Classpath-Location of the settings4j.xml who i readed by default. <br>
43       * settings4j.xml
44       */
45      public static final String DEFAULT_XML_CONFIGURATION_FILE = "settings4j.xml";
46  
47      /**
48       * The fallback settings4j.xml who is used if not settings4j.xml where found. <br>
49       * org/settings4j/config/defaultsettings4j.xml
50       */
51      public static final String DEFAULT_FALLBACK_CONFIGURATION_FILE = "org/settings4j/config/defaultsettings4j.xml";
52  
53      /**
54       * The internal default Settings4j Repository where all {@link org.settings4j.Settings4j} are stored.
55       */
56      private static Settings4jRepository settingsRepository = new DefaultSettingsRepository();
57      static {
58          initializeRepository(SettingsManager.DEFAULT_XML_CONFIGURATION_FILE);
59          initializeRepositoryIfNecessary(); // default fallback if connectors are empty
60      }
61  
62      /** Hide Constructor, Utility Pattern. */
63      private SettingsManager() {
64          super();
65      }
66  
67      /**
68       * The internal default Settings4j Repository where all {@link org.settings4j.Settings4j} are stored.
69       *
70       * @return The Settings4j Repository, Returns NEVER null.
71       */
72      public static Settings4jRepository getSettingsRepository() {
73          return settingsRepository;
74      }
75  
76      /**
77       * Retrieve the appropriate {@link org.settings4j.Settings4j} instance.
78       *
79       * @return the appropriate {@link org.settings4j.Settings4j} instance.
80       */
81      public static Settings4jInstance getSettings() {
82          initializeRepositoryIfNecessary();
83          // Delegate the actual manufacturing of the settings to the settings repository.
84          return getSettingsRepository().getSettings();
85      }
86  
87      /**
88       * Check if the repository must be configured with the defaul fallback settings4j.xml.
89       */
90      private static void initializeRepositoryIfNecessary() {
91          if (getSettingsRepository().getConnectorCount() == 0) {
92              // No connectors in hierarchy, add default-configuration.
93              initializeRepository(SettingsManager.DEFAULT_FALLBACK_CONFIGURATION_FILE);
94          }
95      }
96  
97      private static void initializeRepository(final String configurationFile) throws FactoryConfigurationError {
98          LOG.debug("Using URL [{}] for automatic settings4j configuration.", configurationFile);
99  
100         final URL url = ClasspathContentResolver.getResource(configurationFile);
101 
102         // If we have a non-null url, then delegate the rest of the
103         // configuration to the DOMConfigurator.configure method.
104         if (url != null) {
105             LOG.info("The settings4j will be configured with the config: {}", url);
106             try {
107                 DOMConfigurator.configure(url, getSettingsRepository());
108             } catch (final NoClassDefFoundError e) {
109                 LOG.warn("Error during initialization {}", configurationFile, e);
110             }
111         } else {
112             if (SettingsManager.DEFAULT_FALLBACK_CONFIGURATION_FILE.equals(configurationFile)) {
113                 LOG.error("Could not find resource: [{}].", configurationFile);
114             } else {
115                 LOG.debug("Could not find resource: [{}]. Use default fallback.", configurationFile);
116             }
117         }
118     }
119 }