1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal

File XWikiCfgConfigurationSource.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

14
40
12
1
221
138
23
0.57
3.33
12
1.92

Classes

Class Line # Actions
XWikiCfgConfigurationSource 54 40 0% 23 15
0.7727272577.3%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Looks for configuration data in {@code /WEB-INF/xwiki.cfg}.
47    *
48    * @version $Id: e023f5b8071a02ad7415e54045e71be5f55d36e0 $
49    * @since 6.1M2
50    */
51    @Component
52    @Named(XWikiCfgConfigurationSource.ROLEHINT)
53    @Singleton
 
54    public class XWikiCfgConfigurationSource implements ConfigurationSource, Initializable
55    {
56    /**
57    * The name of the JDNI variable.
58    */
59    public static final String CFG_ENV_NAME = "XWikiConfig";
60   
61    /**
62    * The component role hint.
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 the location where to find the configuration
81    */
 
82  75 toggle 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   
 
96  75 toggle @Override
97    public void initialize() throws InitializationException
98    {
99  75 this.configurationLocation = getConfigPath();
100   
101  75 InputStream xwikicfgis = null;
102   
103  75 try {
104    // First try loading from a file.
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    // Error loading the file. Most likely, the Security Manager prevented it.
112    // We'll try loading it as a resource below.
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    // Second, try loading it as a resource from the environment
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    // Third, try loading it from the classloader used to load this current class
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 the configuration location
140    */
 
141  0 toggle public String getConfigurationLocation()
142    {
143  0 return this.configurationLocation;
144    }
145   
 
146  54379 toggle private <T> T convert(String value, Class<T> targetClass, T defaultValue)
147    {
148  54379 try {
149  54381 if (targetClass == String[].class) {
150    // Retro compatibility from old XWikiConfig class
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 the properties instance
162    */
 
163  17204 toggle public Properties getProperties()
164    {
165  17204 return this.properties;
166    }
167   
168    // ConfigurationSource
169   
 
170  1160470 toggle @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   
 
182  102 toggle @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   
 
190  1200430 toggle @Override
191    public <T> T getProperty(String key)
192    {
193  1200427 return (T) StringUtils.trim(this.properties.getProperty(key));
194    }
195   
 
196  0 toggle @Override
197    public List<String> getKeys()
198    {
199  0 return EnumerationUtils.toList((Enumeration<String>) this.properties.propertyNames());
200    }
201   
 
202  0 toggle @Override
203    public boolean containsKey(String key)
204    {
205  0 return this.properties.containsKey(key);
206    }
207   
 
208  0 toggle @Override
209    public boolean isEmpty()
210    {
211  0 return this.properties.isEmpty();
212    }
213   
214    /**
215    * @param properties change the internal {@link Properties} instance
216    */
 
217  28 toggle public void set(Properties properties)
218    {
219  28 this.properties = properties;
220    }
221    }