Skip to content

Commit

Permalink
fix: issue accessing jars inside an archive from the agent
Browse files Browse the repository at this point in the history
Issue: https://issues.redhat.com/browse/MWTELE-108

Signed-off-by: Emmanuel Hugonnet <[email protected]>
  • Loading branch information
ehsavoie committed Nov 27, 2023
1 parent 984f590 commit f88b8d4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class InsightsCustomScheduledExecutor extends ScheduledThreadPoolExecutor
private InsightsCustomScheduledExecutor(
InsightsLogger logger, InsightsConfiguration configuration) {
super(1);
setKeepAliveTime(10L, TimeUnit.MILLISECONDS);
this.logger = logger;
this.configuration = configuration;
}
Expand Down
19 changes: 14 additions & 5 deletions api/src/main/java/com/redhat/insights/jars/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public final class JarUtils {
private static final Map<String, String> EMBEDDED_FORMAT_TO_EXTENSION =
getEmbeddedFormatToExtension("ear", "war", "jar");
private static final int DEFAULT_BUFFER_SIZE = 1024 * 8;
private static final String JAR_PROTOCOL = "jar:";
private static final String BANG_SEPARATOR = "!/";

private JarUtils() {}

private static Map<String, String> getEmbeddedFormatToExtension(String... fileExtensions) {
Map<String, String> out = new HashMap<>();
Stream.of(fileExtensions).forEach(ext -> out.put('.' + ext + "!/", ext));
Stream.of(fileExtensions).forEach(ext -> out.put('.' + ext + BANG_SEPARATOR, ext));
return out;
}

Expand All @@ -37,6 +39,7 @@ private static Map<String, String> getEmbeddedFormatToExtension(String... fileEx
* input stream starting at the embedded jar.
*
* @param url
* @return a Stream to the specified URL.
* @throws IOException
*/
public static InputStream getInputStream(URL url) throws IOException {
Expand All @@ -48,10 +51,16 @@ public static InputStream getInputStream(URL url) throws IOException {
// jar content.
if (index > 0 && (index + entry.getKey().length()) < jarLocation.length()) {
String path = url.toExternalForm().substring(index + entry.getKey().length());
if (path.endsWith(BANG_SEPARATOR)) {
path = path.substring(0, path.length() - BANG_SEPARATOR.length());
}
// add 1 to skip past the `.` and the value length, which is the length of the file
// extension
jarURL =
new URL(jarURL.toExternalForm().substring(0, index + 1 + entry.getValue().length()));
String jar = jarURL.toExternalForm().substring(0, index + 1 + entry.getValue().length());
if (jarLocation.startsWith(JAR_PROTOCOL)) {
jar = jar.substring(JAR_PROTOCOL.length());
}
jarURL = new URL(jar);
InputStream inputStream = jarURL.openStream();
JarInputStream jarStream = new JarInputStream(inputStream);

Expand All @@ -63,8 +72,8 @@ public static InputStream getInputStream(URL url) throws IOException {
return jarStream;
}
}
if (jarLocation.startsWith("jar:") && jarLocation.endsWith("!/")) {
String jarLoc = jarLocation.substring(4, jarLocation.length() - 2);
if (jarLocation.startsWith(JAR_PROTOCOL) && jarLocation.endsWith(BANG_SEPARATOR)) {
String jarLoc = jarLocation.substring(4, jarLocation.length() - BANG_SEPARATOR.length());
jarURL = new URL(jarLoc);
}
return jarURL.openStream();
Expand Down
10 changes: 10 additions & 0 deletions api/src/test/java/com/redhat/insights/jars/TestJarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -53,5 +54,14 @@ public void basicCheckJarUrls() throws NoSuchAlgorithmException, IOException, UR
} catch (Exception ex) {
fail(ex);
}
file =
"jar:file:"
+ new File(System.getProperty("basedir")).toPath().toAbsolutePath().toString()
+ "/target/test-classes/com/redhat/insights/jars/sti-sb-tomcat.jar!/BOOT-INF/lib/jackson-databind-2.13.5.jar!/";
try {
getInputStream(new URL(file));
} catch (Exception ex) {
fail(ex);
}
}
}
Binary file not shown.

0 comments on commit f88b8d4

Please sign in to comment.