Skip to content

Commit

Permalink
Fix reloading of the configuration from HTTP(S)
Browse files Browse the repository at this point in the history
The `HttpWatcher` didn't propagate the observed
last modification time back to the configuration.
As a result, each new configuration was already
deprecated when it started and the reconfiguration
process looped.

Closes #2937

Rewrite Jetty tests using WireMock

Closes #2813
  • Loading branch information
ppkarwasz committed Sep 22, 2024
1 parent 80b6a59 commit be24f92
Show file tree
Hide file tree
Showing 23 changed files with 916 additions and 810 deletions.
10 changes: 10 additions & 0 deletions log4j-appserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,32 @@
org.apache.tomcat.juli;transitive=false,
org.eclipse.jetty.util;transitive=false
</bnd-extra-module-options>

<!-- Dependencies unique for this module -->
<jetty.version>9.4.56.v20240826</jetty.version>
<tomcat-juli.version>10.0.27</tomcat-juli.version>
</properties>

<dependencies>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>${tomcat-juli.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.core.config;

import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -32,50 +33,57 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.core.net.UrlConnectionFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class ConfigurationSourceTest {
private static final Path JAR_FILE = Paths.get("target", "test-classes", "jarfile.jar");
private static final Path CONFIG_FILE = Paths.get("target", "test-classes", "log4j2-console.xml");
private static final byte[] buffer = new byte[1024];
/**
* The path inside the jar created by {@link #prepareJarConfigURL} containing the configuration.
*/
public static final String PATH_IN_JAR = "/config/console.xml";

private static final String CONFIG_FILE = "/config/ConfigurationSourceTest.xml";

@TempDir
private Path tempDir;

@Test
public void testJira_LOG4J2_2770_byteArray() throws Exception {
void testJira_LOG4J2_2770_byteArray() throws Exception {
final ConfigurationSource configurationSource =
new ConfigurationSource(new ByteArrayInputStream(new byte[] {'a', 'b'}));
assertNotNull(configurationSource.resetInputStream());
}

/**
* Checks if the usage of 'jar:' URLs does not increase the file descriptor
* count and the jar file can be deleted.
*
* @throws Exception
* count, and the jar file can be deleted.
*/
@Test
public void testNoJarFileLeak() throws Exception {
final URL jarConfigURL = prepareJarConfigURL();
void testNoJarFileLeak() throws Exception {
final Path jarFile = prepareJarConfigURL(tempDir);
final URL jarConfigURL = new URL("jar:" + jarFile.toUri().toURL() + "!" + PATH_IN_JAR);
final long expected = getOpenFileDescriptorCount();
UrlConnectionFactory.createConnection(jarConfigURL).getInputStream().close();
// This can only fail on UNIX
assertEquals(expected, getOpenFileDescriptorCount());
// This can only fail on Windows
try {
Files.delete(JAR_FILE);
Files.delete(jarFile);
} catch (IOException e) {
fail(e);
}
}

@Test
public void testLoadConfigurationSourceFromJarFile() throws Exception {
final URL jarConfigURL = prepareJarConfigURL();
final Path jarFile = prepareJarConfigURL(tempDir);
final URL jarConfigURL = new URL("jar:" + jarFile.toUri().toURL() + "!" + PATH_IN_JAR);
final long expectedFdCount = getOpenFileDescriptorCount();
ConfigurationSource configSource = ConfigurationSource.fromUri(jarConfigURL.toURI());
assertNotNull(configSource);
Expand All @@ -90,7 +98,7 @@ public void testLoadConfigurationSourceFromJarFile() throws Exception {
assertEquals(expectedFdCount, getOpenFileDescriptorCount());
// This can only fail on Windows
try {
Files.delete(JAR_FILE);
Files.delete(jarFile);
} catch (IOException e) {
fail(e);
}
Expand All @@ -104,22 +112,18 @@ private long getOpenFileDescriptorCount() {
return 0L;
}

public static URL prepareJarConfigURL() throws IOException {
if (!Files.exists(JAR_FILE)) {
final Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
try (final OutputStream os = Files.newOutputStream(JAR_FILE);
final JarOutputStream jar = new JarOutputStream(os, manifest);
final InputStream config = Files.newInputStream(CONFIG_FILE)) {
final JarEntry jarEntry = new JarEntry("config/console.xml");
jar.putNextEntry(jarEntry);
int len;
while ((len = config.read(buffer)) != -1) {
jar.write(buffer, 0, len);
}
jar.closeEntry();
}
public static Path prepareJarConfigURL(Path dir) throws IOException {
Path jarFile = dir.resolve("jarFile.jar");
final Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
try (final OutputStream os = Files.newOutputStream(jarFile);
final JarOutputStream jar = new JarOutputStream(os, manifest);
final InputStream config =
requireNonNull(ConfigurationSourceTest.class.getResourceAsStream(CONFIG_FILE))) {
final JarEntry jarEntry = new JarEntry("config/console.xml");
jar.putNextEntry(jarEntry);
IOUtils.copy(config, os);
}
return new URL("jar:" + JAR_FILE.toUri().toURL() + "!/config/console.xml");
return jarFile;
}
}

This file was deleted.

Loading

0 comments on commit be24f92

Please sign in to comment.