Skip to content

Commit 503aae8

Browse files
authored
Add close() methods throughout the SecureJar->JarContents chain to allow closing the backing file systems (#68)
1 parent f970393 commit 503aae8

File tree

8 files changed

+50
-10
lines changed

8 files changed

+50
-10
lines changed

src/main/java/cpw/mods/jarhandling/JarContents.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.jetbrains.annotations.ApiStatus;
44

5+
import java.io.Closeable;
6+
import java.io.IOException;
57
import java.net.URI;
68
import java.nio.file.Path;
79
import java.util.Collection;
@@ -18,7 +20,7 @@
1820
* Convert to a full jar with {@link SecureJar#from(JarContents)}.
1921
*/
2022
@ApiStatus.NonExtendable
21-
public interface JarContents {
23+
public interface JarContents extends Closeable {
2224
/**
2325
* @see SecureJar#getPrimaryPath()
2426
*/

src/main/java/cpw/mods/jarhandling/SecureJar.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
import java.security.CodeSigner;
1818
import java.util.List;
1919
import java.util.Optional;
20-
import java.util.Set;
21-
import java.util.function.BiPredicate;
22-
import java.util.function.Function;
23-
import java.util.function.Supplier;
2420
import java.util.jar.Attributes;
2521
import java.util.jar.Manifest;
2622

@@ -86,6 +82,12 @@ static SecureJar from(JarContents contents, JarMetadata metadata) {
8682
*/
8783
Path getRootPath();
8884

85+
/**
86+
* Closes the underlying file system resources (if any).
87+
* Renders this object unusable.
88+
*/
89+
void close() throws IOException;
90+
8991
/**
9092
* All the functions that are necessary to turn a {@link SecureJar} into a module.
9193
*/

src/main/java/cpw/mods/jarhandling/VirtualJar.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cpw.mods.niofs.union.UnionFileSystemProvider;
55
import org.jetbrains.annotations.Nullable;
66

7+
import java.io.IOException;
78
import java.io.InputStream;
89
import java.lang.module.ModuleDescriptor;
910
import java.net.URI;
@@ -106,6 +107,11 @@ public Path getRootPath() {
106107
return dummyFileSystem.getRoot();
107108
}
108109

110+
@Override
111+
public void close() throws IOException {
112+
dummyFileSystem.close();
113+
}
114+
109115
private class VirtualJarModuleDataProvider implements ModuleDataProvider {
110116
@Override
111117
public String name() {

src/main/java/cpw/mods/jarhandling/impl/Jar.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import cpw.mods.jarhandling.JarMetadata;
44
import cpw.mods.jarhandling.SecureJar;
55
import cpw.mods.niofs.union.UnionFileSystem;
6-
import cpw.mods.niofs.union.UnionPathFilter;
76
import cpw.mods.util.LambdaExceptionUtils;
87
import org.jetbrains.annotations.Nullable;
98

9+
import java.io.IOException;
1010
import java.io.InputStream;
1111
import java.lang.module.ModuleDescriptor;
1212
import java.net.URI;
@@ -104,6 +104,11 @@ public Path getRootPath() {
104104
return filesystem.getPath("");
105105
}
106106

107+
@Override
108+
public void close() throws IOException {
109+
contents.close();
110+
}
111+
107112
@Override
108113
public String toString() {
109114
return "Jar[" + getURI() + "]";

src/main/java/cpw/mods/jarhandling/impl/JarContentsImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Map;
2424
import java.util.Optional;
2525
import java.util.Set;
26-
import java.util.function.BiPredicate;
2726
import java.util.function.Supplier;
2827
import java.util.jar.JarFile;
2928
import java.util.jar.JarInputStream;
@@ -218,4 +217,9 @@ public List<SecureJar.Provider> getMetaInfServices() {
218217
}
219218
return this.providers;
220219
}
220+
221+
@Override
222+
public void close() throws IOException {
223+
filesystem.close();
224+
}
221225
}

src/main/java/cpw/mods/niofs/union/UnionFileSystem.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,23 @@ public UnionFileSystemProvider provider() {
155155
}
156156

157157
@Override
158-
public void close() {
158+
public void close() throws IOException {
159159
provider().removeFileSystem(this);
160+
IOException closeException = null;
161+
for (var embeddedFs : embeddedFileSystems.values()) {
162+
try {
163+
embeddedFs.fs.close();
164+
} catch (IOException e) {
165+
if (closeException != null) {
166+
closeException.addSuppressed(e);
167+
} else {
168+
closeException = e;
169+
}
170+
}
171+
}
172+
if (closeException != null) {
173+
throw closeException;
174+
}
160175
}
161176

162177
@Override

src/test/java/cpw/mods/jarhandling/impl/TestDummyJarProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import java.lang.module.Configuration;
1414
import java.lang.module.ModuleDescriptor;
1515
import java.lang.module.ModuleFinder;
16-
import java.lang.module.ResolvedModule;
1716
import java.net.URI;
1817
import java.nio.file.Path;
1918
import java.security.CodeSigner;
20-
import java.util.Arrays;
2119
import java.util.List;
2220
import java.util.Optional;
2321
import java.util.Set;
@@ -133,5 +131,9 @@ public Path getPath(final String first, final String... rest) {
133131
public Path getRootPath() {
134132
return null;
135133
}
134+
135+
@Override
136+
public void close() {
137+
}
136138
}
137139
}

src/test/java/cpw/mods/jarhandling/impl/TestMetadata.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,9 @@ public Set<String> getPackagesExcluding(String... excludedRootPackages) {
7070
public List<SecureJar.Provider> getMetaInfServices() {
7171
return List.of();
7272
}
73+
74+
@Override
75+
public void close() {
76+
}
7377
}
7478
}

0 commit comments

Comments
 (0)