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