/
XWikiPropertiesConfigurationSource.java
123 lines (112 loc) · 5.28 KB
/
XWikiPropertiesConfigurationSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.configuration.internal;
import java.io.File;
import java.net.URL;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.commons.configuration2.BaseConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.environment.Environment;
/**
* Looks for configuration data in {@code /WEB-INF/xwiki.properties}.
*
* @version $Id$
* @since 2.0M1
*/
@Component
@Named("xwikiproperties")
@Singleton
public class XWikiPropertiesConfigurationSource extends CommonsConfigurationSource implements Initializable
{
private static final String XWIKI_PROPERTIES_FILE = "xwiki.properties";
private static final String XWIKI_PROPERTIES_WARPATH = "/WEB-INF/" + XWIKI_PROPERTIES_FILE;
/**
* the Environment from where to get the XWiki properties file.
*/
@Inject
private Environment environment;
/**
* The logger to log.
*/
@Inject
private Logger logger;
@Override
public void initialize() throws InitializationException
{
setConfiguration(loadConfiguration());
}
private Configuration loadConfiguration()
{
// Looking for /etc/xwiki/xwiki.properties first
File file = new File("/etc/xwiki/" + XWIKI_PROPERTIES_FILE);
if (file.exists()) {
try {
this.logger.info("Loading [{}] from default location [{}]", XWIKI_PROPERTIES_FILE,
file.getCanonicalPath());
return new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
.configure(new Parameters().properties()
.setListDelimiterHandler(new DefaultListDelimiterHandler(',')).setFile(file))
.getConfiguration();
} catch (Exception e) {
// Note: if we cannot read the configuration file for any reason we log a warning but continue since
// XWiki will use default values for all configurable elements.
this.logger.warn("Failed to load configuration file [{}]: {}", file, e.getMessage());
}
}
// Register the Commons Properties Configuration, looking for a xwiki.properties file
// in the XWiki path somewhere.
URL xwikiPropertiesUrl = null;
try {
xwikiPropertiesUrl = this.environment.getResource(XWIKI_PROPERTIES_WARPATH);
if (xwikiPropertiesUrl != null) {
this.logger.info("Loading [{}] from [{}]", XWIKI_PROPERTIES_FILE, xwikiPropertiesUrl.toExternalForm());
FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
.configure(new Parameters().properties()
.setListDelimiterHandler(new DefaultListDelimiterHandler(',')).setURL(xwikiPropertiesUrl));
return builder.getConfiguration();
} else {
// We use a debug logging level here since we consider it's ok that there's no XWIKI_PROPERTIES_FILE
// available, in which case default values are used.
this.logger.debug("No configuration file [{}] found. Using default configuration values.",
XWIKI_PROPERTIES_WARPATH);
}
} catch (Exception e) {
// Note: if we cannot read the configuration file for any reason we log a warning but continue since XWiki
// will use default values for all configurable elements.
this.logger.warn(
"Failed to load configuration file [{}]. Using default configuration values. Internal error [{}]",
XWIKI_PROPERTIES_WARPATH, e.getMessage());
}
// If no Commons Properties Configuration has been set, use a default empty Commons Configuration
// implementation.
return new BaseConfiguration();
}
}