forked from prospero238/liquibase-runner
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Util.java
executable file
·94 lines (79 loc) · 3.11 KB
/
Util.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
89
90
91
92
93
94
package org.jenkinsci.plugins.liquibase.common;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import hudson.FilePath;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.util.StringUtils;
import org.jenkinsci.plugins.liquibase.exception.LiquibaseRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Util {
private static final Logger LOG = LoggerFactory.getLogger(Util.class);
private Util() {
}
public static String formatChangeset(ChangeSet changeSet) {
String filePath;
DatabaseChangeLog log = changeSet.getChangeLog();
if (log != null) {
filePath = log.getFilePath();
} else {
filePath = "";
}
String changeSetName = changeSet.toString(false);
return filePath + "::" + changeSetName.replace(filePath + "::", "");
}
public static void registerDatabaseDriver(String driverName, ClassLoader liquibaseClassLoader) throws SQLException {
Driver driver;
try {
Driver actualDriver = (Driver) Class.forName(driverName, true, liquibaseClassLoader).getDeclaredConstructor().newInstance();
driver = new DriverShim(actualDriver);
DriverManager.registerDriver(driver);
} catch (ReflectiveOperationException e) {
throw new SQLException("Cannot create driver " + driverName + ": " + e.getMessage(), e);
}
}
public static ClassLoader createClassLoader(boolean isUnix, final FilePath workspace, String classpath) {
String separator;
if (isUnix) {
separator = ":";
} else {
separator = ";";
}
List<URL> classpathUrls = new ArrayList<>();
Iterable<String> classPathElements = Splitter.on(separator).trimResults().split(classpath);
for (String filePath : classPathElements) {
filePath = StringUtils.trimToNull(filePath);
if (filePath == null) {
continue;
}
//jenkins FilePath prefers unix style, even on windows
filePath = filePath.replace("\\", "/");
FilePath file = workspace.child(filePath);
try {
if (file.isDirectory()) {
for (FilePath jarFile : file.list("*.jar")) {
classpathUrls.add(jarFile.toURI().toURL());
}
}
classpathUrls.add(file.toURI().toURL());
} catch (Exception e) {
LOG.warn("Error handling classpath " + file+": "+e.getMessage(), e);
}
}
return new URLClassLoader(Iterables.toArray(classpathUrls, URL.class), Util.class.getClassLoader());
}
}