1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33 public final class SettingsManager {
34
35
36 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SettingsManager.class);
37
38
39
40
41
42 public static final String DEFAULT_XML_CONFIGURATION_FILE = "settings4j.xml";
43
44
45
46
47
48 public static final String DEFAULT_FALLBACK_CONFIGURATION_FILE = "org/settings4j/config/defaultsettings4j.xml";
49
50
51
52
53 private static Settings4jRepository settingsRepository = new DefaultSettingsRepository();
54 static {
55 initializeRepository(SettingsManager.DEFAULT_XML_CONFIGURATION_FILE);
56 initializeRepositoryIfNecessary();
57 }
58
59
60 private SettingsManager() {
61 super();
62 }
63
64
65
66
67
68
69 public static Settings4jRepository getSettingsRepository() {
70 return settingsRepository;
71 }
72
73
74
75
76
77
78 public static Settings4jInstance getSettings() {
79 initializeRepositoryIfNecessary();
80
81 return getSettingsRepository().getSettings();
82 }
83
84
85
86
87 private static void initializeRepositoryIfNecessary() {
88 if (getSettingsRepository().getConnectorCount() == 0) {
89
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
100
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 }