1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
48 |
|
@Component |
49 |
|
@Named("database") |
50 |
|
@Singleton |
|
|
| 77.3% |
Uncovered Elements: 10 (44) |
Complexity: 11 |
Complexity Density: 0.34 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
63 |
2 |
@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 |
|
|
|
|
| 68.8% |
Uncovered Elements: 5 (16) |
Complexity: 5 |
Complexity Density: 0.36 |
|
77 |
2 |
@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 |
|
|
86 |
|
|
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 |
|
|
96 |
|
|
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 |
|
|
103 |
|
|
104 |
0 |
logWarning("Failed to compute the database product version", e); |
105 |
|
} |
106 |
|
} |
107 |
2 |
return jsonMap; |
108 |
|
} |
109 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
110 |
0 |
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 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
116 |
2 |
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 |
|
} |