1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.store.hibernate; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.InputStream; |
24 |
|
import java.net.URL; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.dom4j.Attribute; |
31 |
|
import org.dom4j.Element; |
32 |
|
import org.hibernate.HibernateException; |
33 |
|
import org.hibernate.SessionFactory; |
34 |
|
import org.hibernate.cfg.Configuration; |
35 |
|
import org.hibernate.cfg.Environment; |
36 |
|
import org.hibernate.connection.ConnectionProvider; |
37 |
|
import org.hibernate.engine.SessionFactoryImplementor; |
38 |
|
import org.hibernate.util.xml.XmlDocument; |
39 |
|
import org.slf4j.Logger; |
40 |
|
import org.xwiki.component.annotation.Component; |
41 |
|
import org.xwiki.component.annotation.DisposePriority; |
42 |
|
import org.xwiki.component.manager.ComponentLifecycleException; |
43 |
|
import org.xwiki.component.phase.Disposable; |
44 |
|
|
45 |
|
import com.xpn.xwiki.util.Util; |
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
@version |
51 |
|
@since |
52 |
|
|
53 |
|
|
54 |
|
@Component |
55 |
|
@Singleton |
56 |
|
@DisposePriority(10000) |
|
|
| 62.6% |
Uncovered Elements: 40 (107) |
Complexity: 28 |
Complexity Density: 0.42 |
|
57 |
|
public class DefaultHibernateSessionFactory implements HibernateSessionFactory, Disposable |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
private Logger logger; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
private org.xwiki.environment.Environment environment; |
70 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
71 |
184 |
@Override... |
72 |
|
public void dispose() throws ComponentLifecycleException |
73 |
|
{ |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
184 |
SessionFactory sessionFactory = getSessionFactory(); |
78 |
184 |
if (sessionFactory != null) { |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
60 |
ConnectionProvider provider = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider(); |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
60 |
if (provider != null) { |
87 |
60 |
provider.close(); |
88 |
|
} |
89 |
|
} |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
private Configuration configuration = new Configuration() |
96 |
|
{ |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
private static final String PROPERTY_PERMANENTDIRECTORY = "environment.permanentDirectory"; |
101 |
|
|
102 |
|
private static final long serialVersionUID = 1L; |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@link |
107 |
|
@link |
108 |
|
|
109 |
|
private boolean isConfigurationInitialized; |
110 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
111 |
0 |
@Override... |
112 |
|
public Configuration configure() throws HibernateException |
113 |
|
{ |
114 |
0 |
Configuration configuration; |
115 |
0 |
if (this.isConfigurationInitialized) { |
116 |
0 |
configuration = this; |
117 |
|
} else { |
118 |
0 |
configuration = super.configure(); |
119 |
0 |
this.isConfigurationInitialized = true; |
120 |
|
} |
121 |
0 |
replaceVariables(configuration); |
122 |
0 |
return configuration; |
123 |
|
} |
124 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
125 |
60 |
@Override... |
126 |
|
public Configuration configure(String resource) throws HibernateException |
127 |
|
{ |
128 |
60 |
Configuration configuration; |
129 |
60 |
if (this.isConfigurationInitialized) { |
130 |
0 |
configuration = this; |
131 |
|
} else { |
132 |
60 |
configuration = super.configure(resource); |
133 |
60 |
this.isConfigurationInitialized = true; |
134 |
|
} |
135 |
60 |
replaceVariables(configuration); |
136 |
60 |
return configuration; |
137 |
|
} |
138 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
139 |
0 |
@Override... |
140 |
|
public Configuration configure(URL url) throws HibernateException |
141 |
|
{ |
142 |
0 |
Configuration configuration; |
143 |
0 |
if (this.isConfigurationInitialized) { |
144 |
0 |
configuration = this; |
145 |
|
} else { |
146 |
0 |
configuration = super.configure(url); |
147 |
0 |
this.isConfigurationInitialized = true; |
148 |
|
} |
149 |
0 |
replaceVariables(configuration); |
150 |
0 |
return configuration; |
151 |
|
} |
152 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
153 |
0 |
@Override... |
154 |
|
public Configuration configure(File configFile) throws HibernateException |
155 |
|
{ |
156 |
0 |
Configuration configuration; |
157 |
0 |
if (this.isConfigurationInitialized) { |
158 |
0 |
configuration = this; |
159 |
|
} else { |
160 |
0 |
configuration = super.configure(configFile); |
161 |
0 |
this.isConfigurationInitialized = true; |
162 |
|
} |
163 |
0 |
replaceVariables(configuration); |
164 |
0 |
return configuration; |
165 |
|
} |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
167 |
346 |
@Override... |
168 |
|
public void add(XmlDocument metadataXml) |
169 |
|
{ |
170 |
346 |
Element basePropertyElement = selectChildClassMappingElement(metadataXml.getDocumentTree().getRootElement(), |
171 |
|
"class", "com.xpn.xwiki.objects.BaseProperty"); |
172 |
346 |
if (basePropertyElement != null) { |
173 |
60 |
decorateDBStringListMapping(basePropertyElement); |
174 |
|
} |
175 |
|
|
176 |
346 |
super.add(metadataXml); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
@param |
183 |
|
@param |
184 |
|
@param |
185 |
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
186 |
406 |
private Element selectChildClassMappingElement(Element parentElement, String elementName, String className)... |
187 |
|
{ |
188 |
406 |
for (Object elementObj : parentElement.elements(elementName)) { |
189 |
1510 |
if (elementObj instanceof Element) { |
190 |
1510 |
Element element = (Element) elementObj; |
191 |
1510 |
Attribute attribute = element.attribute("name"); |
192 |
1510 |
if (attribute != null && className.equals(attribute.getValue())) { |
193 |
120 |
return element; |
194 |
|
} |
195 |
|
} |
196 |
|
} |
197 |
286 |
return null; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
205 |
60 |
private void decorateDBStringListMapping(Element basePropertyElement)... |
206 |
|
{ |
207 |
60 |
final String className = "com.xpn.xwiki.objects.DBStringListProperty"; |
208 |
60 |
final String collectionType = "com.xpn.xwiki.internal.objects.ListPropertyCollectionType"; |
209 |
|
|
210 |
60 |
Element listClassElement = selectChildClassMappingElement(basePropertyElement, "joined-subclass", |
211 |
|
className); |
212 |
|
|
213 |
60 |
if (listClassElement != null) { |
214 |
60 |
Element listElement = listClassElement.element("list"); |
215 |
60 |
if (listElement != null) { |
216 |
60 |
listElement.addAttribute("collection-type", |
217 |
|
collectionType); |
218 |
60 |
DefaultHibernateSessionFactory.this.logger.debug( |
219 |
|
"Added collection-type attribute [{}] to hibernate mapping for [{}].", collectionType, |
220 |
|
className); |
221 |
|
} |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
226 |
60 |
@Override... |
227 |
|
protected InputStream getConfigurationInputStream(String resource) throws HibernateException |
228 |
|
{ |
229 |
60 |
InputStream stream = Util.getResourceAsStream(resource); |
230 |
60 |
if (stream == null) { |
231 |
0 |
throw new HibernateException(String.format("Can't find [%s] for hibernate configuration", resource)); |
232 |
|
} |
233 |
60 |
return stream; |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
@link |
239 |
|
|
240 |
|
|
241 |
|
@param |
242 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
243 |
60 |
private void replaceVariables(Configuration hibernateConfiguration)... |
244 |
|
{ |
245 |
60 |
String url = hibernateConfiguration.getProperty(Environment.URL); |
246 |
60 |
if (StringUtils.isEmpty(url)) { |
247 |
0 |
return; |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
60 |
if (url.matches(".*\\$\\{.*\\}.*")) { |
252 |
55 |
String newURL = StringUtils.replace(url, String.format("${%s}", PROPERTY_PERMANENTDIRECTORY), |
253 |
|
DefaultHibernateSessionFactory.this.environment.getPermanentDirectory().getAbsolutePath()); |
254 |
|
|
255 |
|
|
256 |
55 |
hibernateConfiguration.setProperty(Environment.URL, newURL); |
257 |
55 |
DefaultHibernateSessionFactory.this.logger.debug("Resolved Hibernate URL [{}] to [{}]", url, newURL); |
258 |
|
} |
259 |
|
} |
260 |
|
}; |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
private SessionFactory sessionFactory; |
266 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
267 |
712 |
@Override... |
268 |
|
public Configuration getConfiguration() |
269 |
|
{ |
270 |
712 |
return this.configuration; |
271 |
|
} |
272 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
273 |
227412 |
@Override... |
274 |
|
public SessionFactory getSessionFactory() |
275 |
|
{ |
276 |
227406 |
return this.sessionFactory; |
277 |
|
} |
278 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
279 |
60 |
@Override... |
280 |
|
public void setSessionFactory(SessionFactory sessionFactory) |
281 |
|
{ |
282 |
60 |
this.sessionFactory = sessionFactory; |
283 |
|
} |
284 |
|
} |