forked from hibernate/hibernate-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBAllocationHelper.groovy
86 lines (81 loc) · 3.73 KB
/
DBAllocationHelper.groovy
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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package hudson.util
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.hibernate.gradle.testing.matrix.MatrixNode
import org.hibernate.gradle.util.FileUtil
/**
*
* @author Strong Liu
*/
public class DBAllocationHelper {
private static final Logger log = Logging.getLogger(DBAllocationHelper.class);
private static final String DRIVER_PROP = "hibernate.connection.driver_class";
private static final String URL_PROP = "hibernate.connection.url";
private static final String USERNAME_PROP = "hibernate.connection.username";
private static final String PASSWORD_PROP = "hibernate.connection.password";
//DBAllocator supports the following DBs
private static def supportedDB = ["oracle9i", "oracle10g", "oracle11gR1", "oracle11gR2",
"oracle11gR2RAC","oracle11gR1RAC",
"postgresql82", "postgresql83", "postgresql84", "mysql50", "mysql51",
"db2-91", "db2-97", "mssql2005", "mssql2008R1", "mssql2008R2", "sybase155"];
public static Map<String, String> getProperties(MatrixNode node) {
Map<String, String> map = new HashMap<String, String>();
if ( FileUtil.isFile(node.hibernatePropertyFile) ) {
Properties hibernateProperties = new Properties();
hibernateProperties.load(new FileInputStream(node.hibernatePropertyFile));
map.putAll(hibernateProperties);
}
if ( isDBAllocationEnabled(node.name) ) {
log.lifecycle("using DBAllocator to get DB[${node.name}] connection info");
try {
DBAllocation db = node.DBAllocation
db.allocate(node.name, 300);
Properties prop = db.properties
log.lifecycle("DBAllocating finished for DB[${node.name}], uuid is [${prop['uuid']}]")
map[DRIVER_PROP] = prop["db.jdbc_class"]
map[URL_PROP] = prop["db.jdbc_url"]
map[USERNAME_PROP] = prop["db.username"]
map[PASSWORD_PROP] = prop["db.password"]
map["uuid"] = prop["uuid"];
db.clean();
}
catch (RuntimeException e) {
log.debug("DBAllocating error, ignore", e);
}
}
return map;
}
/**
* use -Dhibernate-matrix-dballocation=all to enable DBAllocation for all matrix node
* or
* add systemProp.hibernate-matrix-dballocation=all to ${user.home}/.gradle/gradle.properties
*/
public static boolean isDBAllocationEnabled(String name) {
if ( !supportedDB.contains(name) ) return false;
String value = System.properties["hibernate-matrix-dballocation"]
return value != null && (value.contains(name) || value.equals("all"));
}
}