1 | |
|
2 | |
|
3 | |
package org.liquibase.maven.plugins; |
4 | |
|
5 | |
import java.io.File; |
6 | |
import java.net.*; |
7 | |
import java.sql.*; |
8 | |
import java.util.*; |
9 | |
import java.lang.reflect.Field; |
10 | |
import liquibase.exception.LiquibaseException; |
11 | |
import org.apache.maven.artifact.Artifact; |
12 | |
import org.apache.maven.plugin.logging.Log; |
13 | |
import org.apache.maven.project.MavenProject; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | 0 | public class MavenUtils { |
21 | |
|
22 | |
public static final String LOG_SEPARATOR = "------------------------------------------------------------------------"; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public static ClassLoader getArtifactClassloader(MavenProject project, boolean includeArtifact, |
34 | |
boolean includeTestOutputDirectory, Class clazz, Log log, boolean verbose) throws MalformedURLException { |
35 | 0 | if (verbose) { |
36 | 0 | log.info("Loading artfacts into URLClassLoader"); |
37 | |
} |
38 | 0 | Set<URL> urls = new HashSet<URL>(); |
39 | |
|
40 | 0 | Set dependencies = project.getDependencyArtifacts(); |
41 | 0 | if (dependencies != null && !dependencies.isEmpty()) { |
42 | 0 | for (Iterator it = dependencies.iterator(); it.hasNext();) { |
43 | 0 | addArtifact(urls, (Artifact) it.next(), log, verbose); |
44 | |
} |
45 | |
} else { |
46 | 0 | log.info("there are no resolved artifacts for the Maven project."); |
47 | |
} |
48 | |
|
49 | |
|
50 | 0 | if (includeArtifact) { |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | Artifact a = project.getArtifact(); |
57 | 0 | if (a.getFile() != null) { |
58 | 0 | addArtifact(urls, a, log, verbose); |
59 | |
} else { |
60 | 0 | addFile(urls, new File(project.getBuild().getOutputDirectory()), log, verbose); |
61 | |
} |
62 | |
} |
63 | 0 | if (includeTestOutputDirectory) { |
64 | 0 | addFile(urls, new File(project.getBuild().getTestOutputDirectory()), log, verbose); |
65 | |
} |
66 | 0 | if (verbose) { |
67 | 0 | log.info(LOG_SEPARATOR); |
68 | |
} |
69 | |
|
70 | 0 | URL[] urlArray = urls.toArray(new URL[urls.size()]); |
71 | 0 | return new URLClassLoader(urlArray, clazz.getClassLoader()); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private static void addArtifact(Set<URL> urls, Artifact artifact, Log log, boolean verbose) |
85 | |
throws MalformedURLException { |
86 | 0 | File f = artifact.getFile(); |
87 | 0 | if (f == null) { |
88 | 0 | log.warn("Artifact with no actual file, '" + artifact.getGroupId() + ":" + artifact.getArtifactId() + "'"); |
89 | |
} else { |
90 | 0 | addFile(urls, f, log, verbose); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | 0 | } |
103 | |
|
104 | |
private static void addFile(Set<URL> urls, File f, Log log, boolean verbose) throws MalformedURLException { |
105 | 0 | if (f != null) { |
106 | 0 | URL fileURL = f.toURI().toURL(); |
107 | 0 | if (verbose) { |
108 | 0 | log.info(" artifact: " + fileURL); |
109 | |
} |
110 | 0 | urls.add(fileURL); |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
public static Connection getDatabaseConnection(ClassLoader classLoader, String driver, String url, String username, |
115 | |
String password) throws LiquibaseException { |
116 | 0 | Driver dbDriver = null; |
117 | |
try { |
118 | 0 | dbDriver = (Driver) Class.forName(driver, true, classLoader).newInstance(); |
119 | 0 | } catch (InstantiationException e) { |
120 | 0 | throw new LiquibaseException("Failed to load JDBC driver, " + driver, e); |
121 | 0 | } catch (IllegalAccessException e) { |
122 | 0 | throw new LiquibaseException("Failed to load JDBC driver, " + driver, e); |
123 | 0 | } catch (ClassNotFoundException e) { |
124 | 0 | throw new LiquibaseException("Missing Class '" + e.getMessage() + "'. Database " |
125 | |
+ "driver may not be included in the project " + "dependencies or with wrong scope."); |
126 | 0 | } |
127 | |
|
128 | 0 | Properties info = new Properties(); |
129 | 0 | info.put("user", username); |
130 | 0 | info.put("password", password); |
131 | |
try { |
132 | 0 | Connection connection = dbDriver.connect(url, info); |
133 | 0 | if (connection == null) { |
134 | 0 | throw new LiquibaseException("Connection could not be created to " + url + " with driver " |
135 | |
+ dbDriver.getClass().getName() + ". Possibly the wrong driver for the given " |
136 | |
+ "database URL"); |
137 | |
} |
138 | 0 | return connection; |
139 | 0 | } catch (SQLException e) { |
140 | 0 | throw new LiquibaseException(e); |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public static Field getDeclaredField(Class clazz, String fieldName) throws NoSuchFieldException { |
157 | 49 | Field f = getField(clazz, fieldName); |
158 | 49 | if (f == null) { |
159 | |
|
160 | 36 | Class parent = clazz.getSuperclass(); |
161 | 36 | if (parent != null) { |
162 | 36 | return getDeclaredField(parent, fieldName); |
163 | |
} else { |
164 | 0 | throw new NoSuchFieldException("The field '" + fieldName + "' could not be " |
165 | |
+ "found in the class of any of its parent " + "classes."); |
166 | |
} |
167 | |
} else { |
168 | 13 | return f; |
169 | |
} |
170 | |
} |
171 | |
|
172 | |
private static Field getField(Class clazz, String fieldName) { |
173 | 49 | Field[] fields = clazz.getDeclaredFields(); |
174 | 135 | for (int i = 0; i < fields.length; i++) { |
175 | 99 | if (fields[i].getName().equals(fieldName)) { |
176 | 13 | return fields[i]; |
177 | |
} |
178 | |
} |
179 | 36 | return null; |
180 | |
} |
181 | |
} |