Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ClassLoaderResourceAccessor implement Closable #2308

Merged
merged 4 commits into from Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,7 +24,7 @@
*
* @see OSGiResourceAccessor for OSGi-based classloaders
*/
public class ClassLoaderResourceAccessor extends AbstractResourceAccessor {
public class ClassLoaderResourceAccessor extends AbstractResourceAccessor implements AutoCloseable {

private ClassLoader classLoader;
protected List<FileSystem> rootPaths;
Expand Down Expand Up @@ -270,7 +270,7 @@ protected SortedSet<String> listFromClassLoader(String path, boolean recursive,
try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
String comparePath = path;
if (comparePath.startsWith("/")) {
comparePath = "/"+comparePath;
comparePath = "/" + comparePath;
}
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
Expand Down Expand Up @@ -355,4 +355,17 @@ public SortedSet<String> describeLocations() {

return description;
}

@Override
public void close() throws Exception {
if (rootPaths != null) {
for (final FileSystem rootPath : rootPaths) {
try {
rootPath.close();
} catch (final Exception e) {
Scope.getCurrentScope().getLog(getClass()).fine("Cannot close path " + e.getMessage(), e);
}
}
}
}
}
Expand Up @@ -5,6 +5,8 @@ import liquibase.util.StreamUtil
import spock.lang.Specification
import spock.lang.Unroll

import java.nio.file.FileSystem

class ClassLoaderResourceAccessorTest extends Specification {

def testResourceAccessor = new ClassLoaderResourceAccessor(new URLClassLoader(
Expand Down Expand Up @@ -153,4 +155,23 @@ class ClassLoaderResourceAccessorTest extends Specification {
],
]
}

def close() {
setup:
FileSystem path1 = Mock()
FileSystem path2 = Mock()

when:
def testAccessor = new ClassLoaderResourceAccessor(this.getClass().getClassLoader())

testAccessor.rootPaths = null;
testAccessor.close() //no errors thrown from close() when rootPaths is null

testAccessor.rootPaths = [path1, path2]
testAccessor.close()

then:
2 * path1.close()
1 * path2.close()
}
}