This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
EntityManagerFactoryHolder.java
88 lines (79 loc) · 3.4 KB
/
EntityManagerFactoryHolder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.zanata.util;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class EntityManagerFactoryHolder {
private static final EntityManagerFactoryHolder HOLDER =
new EntityManagerFactoryHolder();
private static EntityManagerFactory emFactory;
private final Properties overrideProps;
private EntityManagerFactoryHolder() {
copyPersistenceXML();
overrideProps = new Properties();
overrideProps.put("hibernate.hbm2ddl.auto", "");
overrideProps
.put("hibernate.connection.provider_class",
"org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl");
overrideProps.put("hibernate.connection.driver_class",
"com.mysql.jdbc.Driver");
overrideProps.put("hibernate.connection.username",
PropertiesHolder.getProperty("hibernate.connection.username"));
overrideProps.put("hibernate.connection.password",
PropertiesHolder.getProperty("hibernate.connection.password"));
overrideProps.put("hibernate.dialect",
"org.zanata.util.ZanataMySQL5InnoDBDialect");
overrideProps.put("hibernate.connection.url",
PropertiesHolder.getProperty("hibernate.connection.url"));
overrideProps.put("hibernate.search.default.indexBase",
PropertiesHolder
.getProperty("hibernate.search.default.indexBase"));
}
/**
* This is a temporary workaround to reduce another copy of persistence.xml.
* It rely on zanata-war being built (so that the persistenc.xml appears in
* target/test-classes/META-INF)
*/
private static void copyPersistenceXML() {
URL resource =
Thread.currentThread().getContextClassLoader().getResource(".");
File currentDir = new File(resource.getPath());
String current = currentDir.getAbsolutePath();
String testPersistenceXML =
current.replace("functional-test/target/test-classes",
"zanata-war/target/test-classes/META-INF/persistence.xml");
File xml = new File(testPersistenceXML);
Preconditions.checkState(
xml.exists() && xml.getName().equals("persistence.xml"),
"%s does not exists. You will need to build zanata-war first.",
xml.getAbsolutePath());
File destDir = new File(currentDir, "META-INF");
destDir.mkdirs();
try {
Files.copy(xml, new File(destDir, "persistence.xml"));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
public static EntityManagerFactoryHolder holder() {
return HOLDER;
}
public EntityManagerFactory getEmFactory() {
if (emFactory == null) {
synchronized (HOLDER) {
if (emFactory == null) {
emFactory =
Persistence.createEntityManagerFactory(
"zanataTestDatasourcePU", overrideProps);
}
}
}
return emFactory;
}
}