Skip to content

Commit

Permalink
Avoid locking JARs on Windows when opening input streams
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky committed Oct 30, 2024
1 parent b2424a6 commit cee000c
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.smallrye.openapi.runtime;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Constructor;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -306,7 +311,7 @@ private static List<OpenApiStaticFile> loadOpenApiStaticFile(List<OpenApiStaticF
Optional.ofNullable(loadFunction.apply(path))
.map(locator -> {
try {
return new OpenApiStaticFile(locator, locator.openStream(), format);
return new OpenApiStaticFile(locator, openStream(locator), format);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -315,4 +320,21 @@ private static List<OpenApiStaticFile> loadOpenApiStaticFile(List<OpenApiStaticF

return apiStaticFiles;
}

private static InputStream openStream(URL url) throws IOException {
if ("jar".equals(url.getProtocol())) {
URLConnection urlConnection = url.openConnection();
// prevent locking the jar after the inputstream is closed
urlConnection.setUseCaches(false);
return urlConnection.getInputStream();
}
if ("file".equals(url.getProtocol())) {
try {
return Files.newInputStream(Path.of(url.toURI()));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Failed to translate " + url + " to local path", e);
}
}
return url.openStream();
}
}

0 comments on commit cee000c

Please sign in to comment.