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

File DatabasePingDataProvider.java

 

Coverage histogram

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

Code metrics

8
32
4
1
133
87
11
0.34
8
4
2.75

Classes

Class Line # Actions
DatabasePingDataProvider 51 32 0% 11 10
0.7727272577.3%
 

Contributing tests

This file is covered by 2 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.activeinstalls.internal.client.data;
21   
22    import java.sql.DatabaseMetaData;
23    import java.sql.SQLException;
24    import java.util.HashMap;
25    import java.util.Map;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.lang.exception.ExceptionUtils;
32    import org.slf4j.Logger;
33    import org.xwiki.activeinstalls.internal.client.PingDataProvider;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.context.Execution;
36   
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.store.XWikiCacheStoreInterface;
39    import com.xpn.xwiki.store.XWikiHibernateBaseStore;
40    import com.xpn.xwiki.store.XWikiStoreInterface;
41   
42    /**
43    * Provide database name and version.
44    *
45    * @version $Id: 1f34ade15fc5c1b8f8e93ca0f022c04ebda37ee7 $
46    * @since 6.1M1
47    */
48    @Component
49    @Named("database")
50    @Singleton
 
51    public class DatabasePingDataProvider implements PingDataProvider
52    {
53    private static final String PROPERTY_DB_NAME = "dbName";
54   
55    private static final String PROPERTY_DB_VERSION = "dbVersion";
56   
57    @Inject
58    private Execution execution;
59   
60    @Inject
61    private Logger logger;
62   
 
63  2 toggle @Override
64    public Map<String, Object> provideMapping()
65    {
66  2 Map<String, Object> map = new HashMap<>();
67  2 map.put("type", "string");
68  2 map.put("index", "not_analyzed");
69   
70  2 Map<String, Object> propertiesMap = new HashMap<>();
71  2 propertiesMap.put(PROPERTY_DB_NAME, map);
72  2 propertiesMap.put(PROPERTY_DB_VERSION, map);
73   
74  2 return propertiesMap;
75    }
76   
 
77  2 toggle @Override
78    public Map<String, Object> provideData()
79    {
80  2 Map<String, Object> jsonMap = new HashMap<>();
81  2 DatabaseMetaData metaData;
82  2 try {
83  2 metaData = getDatabaseMetaData();
84    } catch (Exception e) {
85    // Ignore, we just don't save DB information...
86    // However we log a warning since it's a problem that needs to be seen and looked at.
87  0 logWarning("Failed to retrieve database metadata", e);
88  0 metaData = null;
89    }
90   
91  2 if (metaData != null) {
92  2 try {
93  2 jsonMap.put(PROPERTY_DB_NAME, metaData.getDatabaseProductName());
94    } catch (SQLException e) {
95    // Ignore, we just don't save that information...
96    // However we log a warning since it's a problem that needs to be seen and looked at.
97  0 logWarning("Failed to compute the database product name", e);
98    }
99  2 try {
100  2 jsonMap.put(PROPERTY_DB_VERSION, metaData.getDatabaseProductVersion());
101    } catch (SQLException e) {
102    // Ignore, we just don't save that information...
103    // However we log a warning since it's a problem that needs to be seen and looked at.
104  0 logWarning("Failed to compute the database product version", e);
105    }
106    }
107  2 return jsonMap;
108    }
109   
 
110  0 toggle private void logWarning(String explanation, Throwable e)
111    {
112  0 this.logger.warn("{}. This information has not been added to the Active Installs ping data. Reason [{}]",
113    explanation, ExceptionUtils.getRootCauseMessage(e));
114    }
115   
 
116  2 toggle private DatabaseMetaData getDatabaseMetaData()
117    {
118  2 DatabaseMetaData metaData = null;
119  2 XWikiContext xcontext =
120    (XWikiContext) this.execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
121  2 if (xcontext != null) {
122  2 XWikiStoreInterface storeInterface = xcontext.getWiki().getStore();
123  2 if (storeInterface instanceof XWikiCacheStoreInterface) {
124  2 storeInterface = ((XWikiCacheStoreInterface) storeInterface).getStore();
125    }
126  2 if (XWikiHibernateBaseStore.class.isAssignableFrom(storeInterface.getClass())) {
127  2 XWikiHibernateBaseStore baseStore = (XWikiHibernateBaseStore) storeInterface;
128  2 metaData = baseStore.getDatabaseMetaData();
129    }
130    }
131  2 return metaData;
132    }
133    }