Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 3543296

Browse files
authored
Merge pull request #981 from grails/embeddedJarHandling
Handle loading migrations from embedded jars (plugins)
2 parents 2b814d3 + 97e873e commit 3543296

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.grails.plugins.databasemigration.liquibase
2+
3+
import groovy.transform.CompileStatic
4+
import liquibase.resource.AbstractPathResourceAccessor
5+
import liquibase.resource.PathResource
6+
import liquibase.resource.Resource
7+
import liquibase.resource.ResourceAccessor
8+
import liquibase.resource.ZipPathHandler
9+
10+
import java.nio.file.FileSystem
11+
import java.nio.file.FileSystems
12+
import java.nio.file.Path
13+
import java.nio.file.Paths
14+
15+
@CompileStatic
16+
class EmbeddedJarPathHandler extends ZipPathHandler {
17+
@Override
18+
int getPriority(String root) {
19+
if (root.startsWith("jar:file:") && root.endsWith("!/")) { //only can handle `jar:` urls for the entire jar
20+
if (parseJarPath(root).contains('!')) {
21+
return PRIORITY_SPECIALIZED
22+
}
23+
}
24+
PRIORITY_NOT_APPLICABLE
25+
}
26+
27+
private String parseJarPath(String root) {
28+
root.substring(9, root.lastIndexOf("!"))
29+
}
30+
31+
@Override
32+
ResourceAccessor getResourceAccessor(String root) throws FileNotFoundException {
33+
String jarPath = parseJarPath(root)
34+
new EmbeddedJarResourceAccessor(jarPath.split('!').toList())
35+
}
36+
}
37+
38+
@CompileStatic
39+
class EmbeddedJarResourceAccessor extends AbstractPathResourceAccessor {
40+
private FileSystem fileSystem
41+
42+
EmbeddedJarResourceAccessor(List<String> jarPaths) {
43+
try {
44+
Path firstPath = Paths.get(jarPaths.pop())
45+
fileSystem = FileSystems.newFileSystem(firstPath, null as ClassLoader)
46+
47+
while(jarPaths) {
48+
Path innerPath = fileSystem.getPath(jarPaths.pop())
49+
fileSystem = FileSystems.newFileSystem(innerPath, null as ClassLoader)
50+
}
51+
} catch (e) {
52+
throw new IllegalArgumentException(e.getMessage(), e)
53+
}
54+
}
55+
56+
@Override
57+
void close() throws Exception {
58+
//can't close the filesystem because they often get reused and/or are being used by other things
59+
}
60+
61+
@Override
62+
protected Path getRootPath() {
63+
return this.fileSystem.getPath("/")
64+
}
65+
66+
@Override
67+
protected Resource createResource(Path file, String pathToAdd) {
68+
return new PathResource(pathToAdd, file)
69+
}
70+
71+
@Override
72+
List<String> describeLocations() {
73+
return Collections.singletonList(fileSystem.toString())
74+
}
75+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.grails.plugins.databasemigration.liquibase.EmbeddedJarPathHandler

0 commit comments

Comments
 (0)