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

File CommonsConfigurationSource.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
30
8
1
136
89
17
0.57
3.75
8
2.12

Classes

Class Line # Actions
CommonsConfigurationSource 41 30 0% 17 2
0.9615384396.2%
 

Contributing tests

This file is covered by 18 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 org.xwiki.configuration.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Iterator;
24    import java.util.List;
25    import java.util.Properties;
26   
27    import javax.inject.Inject;
28   
29    import org.apache.commons.configuration.Configuration;
30    import org.xwiki.configuration.ConfigurationSource;
31    import org.xwiki.properties.ConverterManager;
32   
33    /**
34    * Wrap a Commons Configuration instance into a XWiki {@link ConfigurationSource}. This allows us to reuse the <a href=
35    * "http://commons.apache.org/configuration/">numerous types of Configuration<a/> provided by Commons Configuration
36    * (properties file, XML files, databases, etc).
37    *
38    * @version $Id: 0d367b78574ab38c54e5ec6e47b11935c63493fb $
39    * @since 1.6M1
40    */
 
41    public class CommonsConfigurationSource implements ConfigurationSource
42    {
43    private Configuration configuration;
44   
45    /**
46    * Component used for performing type conversions.
47    */
48    @Inject
49    private ConverterManager converterManager;
50   
 
51  1688840 toggle protected Configuration getConfiguration()
52    {
53  1688830 return this.configuration;
54    }
55   
 
56  87 toggle protected void setConfiguration(Configuration configuration)
57    {
58  87 this.configuration = configuration;
59    }
60   
 
61  1477009 toggle @Override
62    @SuppressWarnings("unchecked")
63    public <T> T getProperty(String key, T defaultValue)
64    {
65  1477024 T result;
66  1477175 if (containsKey(key)) {
67  97 if (defaultValue != null) {
68  97 return getProperty(key, (Class<T>) defaultValue.getClass());
69    } else {
70  0 return getProperty(key);
71    }
72    } else {
73  1477064 result = defaultValue;
74    }
75   
76  1477061 return result;
77    }
78   
 
79  221 toggle @Override
80    @SuppressWarnings("unchecked")
81    public <T> T getProperty(String key)
82    {
83  221 return (T) getConfiguration().getProperty(key);
84    }
85   
 
86  828 toggle @Override
87    @SuppressWarnings("unchecked")
88    public <T> T getProperty(String key, Class<T> valueClass)
89    {
90  828 T result = null;
91   
92  828 try {
93  828 if (String.class.getName().equals(valueClass.getName())) {
94  626 result = (T) getConfiguration().getString(key);
95  202 } else if (List.class.isAssignableFrom(valueClass)) {
96  27 result = (T) getConfiguration().getList(key);
97  175 } else if (Properties.class.isAssignableFrom(valueClass)) {
98  40 result = (T) getConfiguration().getProperties(key);
99  135 } else if (null != getProperty(key)) {
100  77 result = (T) this.converterManager.convert(valueClass, getProperty(key));
101    }
102    } catch (org.apache.commons.configuration.ConversionException e) {
103  1 throw new org.xwiki.configuration.ConversionException("Key [" + key + "] is not of type ["
104    + valueClass.getName() + "]", e);
105    } catch (org.xwiki.properties.converter.ConversionException e) {
106  1 throw new org.xwiki.configuration.ConversionException("Key [" + key + "] is not of type ["
107    + valueClass.getName() + "]", e);
108    }
109   
110  826 return result;
111    }
112   
 
113  14 toggle @Override
114    public List<String> getKeys()
115    {
116  14 List<String> keysList = new ArrayList<String>();
117  14 Iterator<String> keys = getConfiguration().getKeys();
118  38 while (keys.hasNext()) {
119  24 keysList.add(keys.next());
120    }
121   
122  14 return keysList;
123    }
124   
 
125  1687911 toggle @Override
126    public boolean containsKey(String key)
127    {
128  1687923 return getConfiguration().containsKey(key);
129    }
130   
 
131  4 toggle @Override
132    public boolean isEmpty()
133    {
134  4 return getConfiguration().isEmpty();
135    }
136    }