| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.internal; |
| 21 |
|
|
| 22 |
|
import java.io.File; |
| 23 |
|
import java.io.FileInputStream; |
| 24 |
|
import java.io.InputStream; |
| 25 |
|
import java.util.Enumeration; |
| 26 |
|
import java.util.List; |
| 27 |
|
import java.util.Properties; |
| 28 |
|
|
| 29 |
|
import javax.inject.Inject; |
| 30 |
|
import javax.inject.Named; |
| 31 |
|
import javax.inject.Singleton; |
| 32 |
|
import javax.naming.Context; |
| 33 |
|
import javax.naming.InitialContext; |
| 34 |
|
|
| 35 |
|
import org.apache.commons.collections4.EnumerationUtils; |
| 36 |
|
import org.apache.commons.lang3.StringUtils; |
| 37 |
|
import org.slf4j.Logger; |
| 38 |
|
import org.xwiki.component.annotation.Component; |
| 39 |
|
import org.xwiki.component.phase.Initializable; |
| 40 |
|
import org.xwiki.component.phase.InitializationException; |
| 41 |
|
import org.xwiki.configuration.ConfigurationSource; |
| 42 |
|
import org.xwiki.environment.Environment; |
| 43 |
|
import org.xwiki.properties.ConverterManager; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@version |
| 49 |
|
@since |
| 50 |
|
|
| 51 |
|
@Component |
| 52 |
|
@Named(XWikiCfgConfigurationSource.ROLEHINT) |
| 53 |
|
@Singleton |
| |
|
| 77.3% |
Uncovered Elements: 15 (66) |
Complexity: 23 |
Complexity Density: 0.57 |
|
| 54 |
|
public class XWikiCfgConfigurationSource implements ConfigurationSource, Initializable |
| 55 |
|
{ |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
public static final String CFG_ENV_NAME = "XWikiConfig"; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
public static final String ROLEHINT = "xwikicfg"; |
| 65 |
|
|
| 66 |
|
@Inject |
| 67 |
|
private Environment environment; |
| 68 |
|
|
| 69 |
|
@Inject |
| 70 |
|
private ConverterManager converter; |
| 71 |
|
|
| 72 |
|
@Inject |
| 73 |
|
private Logger logger; |
| 74 |
|
|
| 75 |
|
private Properties properties = new Properties(); |
| 76 |
|
|
| 77 |
|
private String configurationLocation; |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@return |
| 81 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 82 |
75 |
public static String getConfigPath()... |
| 83 |
|
{ |
| 84 |
75 |
String configurationLocation; |
| 85 |
|
|
| 86 |
75 |
try { |
| 87 |
75 |
Context envContext = (Context) new InitialContext().lookup("java:comp/env"); |
| 88 |
32 |
configurationLocation = (String) envContext.lookup(CFG_ENV_NAME); |
| 89 |
|
} catch (Exception e) { |
| 90 |
43 |
configurationLocation = "/WEB-INF/xwiki.cfg"; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
75 |
return configurationLocation; |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 79.2% |
Uncovered Elements: 5 (24) |
Complexity: 7 |
Complexity Density: 0.44 |
|
| 96 |
75 |
@Override... |
| 97 |
|
public void initialize() throws InitializationException |
| 98 |
|
{ |
| 99 |
75 |
this.configurationLocation = getConfigPath(); |
| 100 |
|
|
| 101 |
75 |
InputStream xwikicfgis = null; |
| 102 |
|
|
| 103 |
75 |
try { |
| 104 |
|
|
| 105 |
75 |
File f = new File(this.configurationLocation); |
| 106 |
75 |
try { |
| 107 |
75 |
if (f.exists()) { |
| 108 |
0 |
xwikicfgis = new FileInputStream(f); |
| 109 |
|
} |
| 110 |
|
} catch (Exception e) { |
| 111 |
|
|
| 112 |
|
|
| 113 |
0 |
this.logger.debug("Failed to load the file [{}] using direct file access." |
| 114 |
|
+ " Trying to load it as a resource using the Servlet Context...", this.configurationLocation, e); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| 118 |
75 |
if (xwikicfgis == null) { |
| 119 |
75 |
xwikicfgis = this.environment.getResourceAsStream(this.configurationLocation); |
| 120 |
75 |
this.logger.debug("Failed to load the file [{}] as a resource " |
| 121 |
|
+ "using the Servlet Context. Trying to load it as classpath resource...", |
| 122 |
|
this.configurationLocation); |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
|
| 126 |
75 |
if (xwikicfgis == null) { |
| 127 |
43 |
xwikicfgis = Thread.currentThread().getContextClassLoader().getResourceAsStream("xwiki.cfg"); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
75 |
if (xwikicfgis != null) { |
| 131 |
32 |
this.properties.load(xwikicfgis); |
| 132 |
|
} |
| 133 |
|
} catch (Exception e) { |
| 134 |
0 |
this.logger.error("Failed to load configuration", e); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
@return |
| 140 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 141 |
0 |
public String getConfigurationLocation()... |
| 142 |
|
{ |
| 143 |
0 |
return this.configurationLocation; |
| 144 |
|
} |
| 145 |
|
|
| |
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 146 |
54379 |
private <T> T convert(String value, Class<T> targetClass, T defaultValue)... |
| 147 |
|
{ |
| 148 |
54379 |
try { |
| 149 |
54381 |
if (targetClass == String[].class) { |
| 150 |
|
|
| 151 |
0 |
return (T) StringUtils.split(value, " ,"); |
| 152 |
|
} else { |
| 153 |
54376 |
return this.converter.convert(targetClass, value); |
| 154 |
|
} |
| 155 |
|
} catch (Exception e) { |
| 156 |
102 |
return defaultValue; |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
@return |
| 162 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 163 |
17204 |
public Properties getProperties()... |
| 164 |
|
{ |
| 165 |
17204 |
return this.properties; |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 170 |
1160470 |
@Override... |
| 171 |
|
public <T> T getProperty(String key, T defaultValue) |
| 172 |
|
{ |
| 173 |
1160468 |
String value = getProperty(key); |
| 174 |
|
|
| 175 |
1160552 |
if (value == null) { |
| 176 |
1099062 |
return defaultValue; |
| 177 |
|
} |
| 178 |
|
|
| 179 |
61478 |
return defaultValue == null ? (T) value : convert(value, (Class<T>) defaultValue.getClass(), defaultValue); |
| 180 |
|
} |
| 181 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 182 |
102 |
@Override... |
| 183 |
|
public <T> T getProperty(String key, Class<T> valueClass) |
| 184 |
|
{ |
| 185 |
102 |
String value = getProperty(key); |
| 186 |
|
|
| 187 |
102 |
return convert(value, valueClass, null); |
| 188 |
|
} |
| 189 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 190 |
1200430 |
@Override... |
| 191 |
|
public <T> T getProperty(String key) |
| 192 |
|
{ |
| 193 |
1200427 |
return (T) StringUtils.trim(this.properties.getProperty(key)); |
| 194 |
|
} |
| 195 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 196 |
0 |
@Override... |
| 197 |
|
public List<String> getKeys() |
| 198 |
|
{ |
| 199 |
0 |
return EnumerationUtils.toList((Enumeration<String>) this.properties.propertyNames()); |
| 200 |
|
} |
| 201 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 202 |
0 |
@Override... |
| 203 |
|
public boolean containsKey(String key) |
| 204 |
|
{ |
| 205 |
0 |
return this.properties.containsKey(key); |
| 206 |
|
} |
| 207 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 208 |
0 |
@Override... |
| 209 |
|
public boolean isEmpty() |
| 210 |
|
{ |
| 211 |
0 |
return this.properties.isEmpty(); |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
|
| 215 |
|
@param@link |
| 216 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 217 |
28 |
public void set(Properties properties)... |
| 218 |
|
{ |
| 219 |
28 |
this.properties = properties; |
| 220 |
|
} |
| 221 |
|
} |