/
XWikiConfigurationService.java
102 lines (89 loc) · 4.16 KB
/
XWikiConfigurationService.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
/*
* 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 com.xpn.xwiki.internal.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.configuration.ConfigurationSource;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiConfig;
/**
* Provide access to the configuration.
* <p>
* Do not use it if you have access to {@link XWiki#getXWikiPreference(String, com.xpn.xwiki.XWikiContext)}, this is
* just a hack for rare use case where we don't have access to the XWikiContext or ExecutionContext.
*
* @version $Id$
* @since 1.9.1
* @deprecated since 6.1M2, use {@link ConfigurationSource} component with hint <code>xwikicfg</code> instead
*/
@Deprecated
public class XWikiConfigurationService
{
private static final Logger LOGGER = LoggerFactory.getLogger(XWikiConfigurationService.class);
private static XWikiConfig config;
public synchronized static String getProperty(String propertyKey, String defaultValue, ServletContext context)
{
if (config == null) {
// Make XWikiRequestProcessor own configuration loader since XWiki of Configuration component are not
// initialized at this time
InputStream xwikicfgis = null;
String configurationLocation;
try {
configurationLocation = XWiki.getConfigPath();
// First try loading from a file.
File f = new File(configurationLocation);
try {
if (f.exists()) {
xwikicfgis = new FileInputStream(f);
}
} catch (Exception e) {
// Error loading the file. Most likely, the Security Manager prevented it.
// We'll try loading it as a resource below.
LOGGER.debug("Failed to load the file [" + configurationLocation + "] using direct "
+ "file access. The error was [" + e.getMessage() + "]. Trying to load it "
+ "as a resource using the Servlet Context...");
}
// Second, try loading it as a resource using the Servlet Context
if (xwikicfgis == null) {
if (context != null) {
xwikicfgis = context.getResourceAsStream(configurationLocation);
LOGGER.debug("Failed to load the file [" + configurationLocation + "] as a resource "
+ "using the Servlet Context. Trying to load it as classpath resource...");
} else {
LOGGER.debug("No Servlet Context available. Trying to load it as classpath resource...");
}
}
// Third, try loading it from the classloader used to load this current class
if (xwikicfgis == null) {
xwikicfgis = XWiki.class.getClassLoader().getResourceAsStream("xwiki.cfg");
}
config = new XWikiConfig(xwikicfgis);
} catch (Exception e) {
LOGGER.error("Faile to lod configuration", e);
config = new XWikiConfig();
}
}
return config.getProperty(propertyKey, defaultValue);
}
}