diff --git a/log4j-tomcat-juli/src/main/java/eu/copernik/log4j/tomcat/juli/TomcatLookup.java b/log4j-tomcat-juli/src/main/java/eu/copernik/log4j/tomcat/juli/TomcatLookup.java index 665cdbf..88387fb 100644 --- a/log4j-tomcat-juli/src/main/java/eu/copernik/log4j/tomcat/juli/TomcatLookup.java +++ b/log4j-tomcat-juli/src/main/java/eu/copernik/log4j/tomcat/juli/TomcatLookup.java @@ -28,56 +28,51 @@ @Plugin(name = "tomcat", category = StrLookup.CATEGORY) public class TomcatLookup implements StrLookup { - static final String CONTEXT_NAME = "context.name"; - static final String CONTEXT_NAME_COMPAT = "classloader.webappName"; - static final String CONTEXT_LOGGER = "context.logger"; - static final String ENGINE_NAME = "engine.name"; - static final String ENGINE_NAME_COMPAT = "classloader.serviceName"; - static final String ENGINE_LOGGER = "engine.logger"; - static final String HOST_NAME = "host.name"; - static final String HOST_NAME_COMPAT = "classloader.hostName"; - static final String HOST_LOGGER = "host.logger"; + static final String CONTEXT_NAME = "context.name"; + static final String CONTEXT_NAME_COMPAT = "classloader.webappName"; + static final String CONTEXT_LOGGER = "context.logger"; + static final String ENGINE_NAME = "engine.name"; + static final String ENGINE_NAME_COMPAT = "classloader.serviceName"; + static final String ENGINE_LOGGER = "engine.logger"; + static final String HOST_NAME = "host.name"; + static final String HOST_NAME_COMPAT = "classloader.hostName"; + static final String HOST_LOGGER = "host.logger"; - private static final String SERVICE_LOGGER_FORMAT = "org.apache.catalina.core.ContainerBase.[%s]"; - private static final String HOST_LOGGER_FORMAT = - "org.apache.catalina.core.ContainerBase.[%s].[%s]"; - private static final String CONTEXT_LOGGER_FORMAT = - "org.apache.catalina.core.ContainerBase.[%s].[%s].[%s]"; + private static final String SERVICE_LOGGER_FORMAT = "org.apache.catalina.core.ContainerBase.[%s]"; + private static final String HOST_LOGGER_FORMAT = "org.apache.catalina.core.ContainerBase.[%s].[%s]"; + private static final String CONTEXT_LOGGER_FORMAT = "org.apache.catalina.core.ContainerBase.[%s].[%s].[%s]"; - public static final TomcatLookup INSTANCE = new TomcatLookup(); + public static final TomcatLookup INSTANCE = new TomcatLookup(); - @Override - public String lookup(String key) { - final ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if (cl instanceof WebappProperties && key != null) { - final WebappProperties props = (WebappProperties) cl; - switch (key) { - case ENGINE_NAME: - case ENGINE_NAME_COMPAT: - return props.getServiceName(); - case ENGINE_LOGGER: - return String.format(SERVICE_LOGGER_FORMAT, props.getServiceName()); - case HOST_NAME: - case HOST_NAME_COMPAT: - return props.getHostName(); - case HOST_LOGGER: - return String.format(HOST_LOGGER_FORMAT, props.getServiceName(), props.getHostName()); - case CONTEXT_NAME: - case CONTEXT_NAME_COMPAT: - return props.getWebappName(); - case CONTEXT_LOGGER: - return String.format( - CONTEXT_LOGGER_FORMAT, - props.getServiceName(), - props.getHostName(), - props.getWebappName()); - } + @Override + public String lookup(String key) { + final ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if (cl instanceof WebappProperties && key != null) { + final WebappProperties props = (WebappProperties) cl; + switch (key) { + case ENGINE_NAME: + case ENGINE_NAME_COMPAT: + return props.getServiceName(); + case ENGINE_LOGGER: + return String.format(SERVICE_LOGGER_FORMAT, props.getServiceName()); + case HOST_NAME: + case HOST_NAME_COMPAT: + return props.getHostName(); + case HOST_LOGGER: + return String.format(HOST_LOGGER_FORMAT, props.getServiceName(), props.getHostName()); + case CONTEXT_NAME: + case CONTEXT_NAME_COMPAT: + return props.getWebappName(); + case CONTEXT_LOGGER: + return String.format( + CONTEXT_LOGGER_FORMAT, props.getServiceName(), props.getHostName(), props.getWebappName()); + } + } + return null; } - return null; - } - @Override - public String lookup(LogEvent event, String key) { - return lookup(key); - } + @Override + public String lookup(LogEvent event, String key) { + return lookup(key); + } } diff --git a/log4j-tomcat-juli/src/test/java/eu/copernik/log4j/tomcat/juli/TomcatLookupTest.java b/log4j-tomcat-juli/src/test/java/eu/copernik/log4j/tomcat/juli/TomcatLookupTest.java index fb52b7e..cd34461 100644 --- a/log4j-tomcat-juli/src/test/java/eu/copernik/log4j/tomcat/juli/TomcatLookupTest.java +++ b/log4j-tomcat-juli/src/test/java/eu/copernik/log4j/tomcat/juli/TomcatLookupTest.java @@ -31,51 +31,49 @@ public class TomcatLookupTest { - private static ClassLoader originalTccl; + private static ClassLoader originalTccl; - private static final String ENGINE_NAME = "Catalina"; - private static final String ENGINE_LOGGERNAME = - "org.apache.catalina.core.ContainerBase.[" + ENGINE_NAME + "]"; - private static final String HOST_NAME = "localhost"; - private static final String HOST_LOGGERNAME = ENGINE_LOGGERNAME + ".[" + HOST_NAME + "]"; - private static final String CONTEXT_NAME = "/myapp"; - private static final String CONTEXT_LOGGERNAME = HOST_LOGGERNAME + ".[" + CONTEXT_NAME + "]"; + private static final String ENGINE_NAME = "Catalina"; + private static final String ENGINE_LOGGERNAME = "org.apache.catalina.core.ContainerBase.[" + ENGINE_NAME + "]"; + private static final String HOST_NAME = "localhost"; + private static final String HOST_LOGGERNAME = ENGINE_LOGGERNAME + ".[" + HOST_NAME + "]"; + private static final String CONTEXT_NAME = "/myapp"; + private static final String CONTEXT_LOGGERNAME = HOST_LOGGERNAME + ".[" + CONTEXT_NAME + "]"; - @BeforeAll - public static void setupContextClassloader() { - originalTccl = Thread.currentThread().getContextClassLoader(); - final ClassLoader tccl = - mock(ClassLoader.class, withSettings().extraInterfaces(WebappProperties.class)); - final WebappProperties props = (WebappProperties) tccl; - when(props.getServiceName()).thenReturn(ENGINE_NAME); - when(props.getHostName()).thenReturn(HOST_NAME); - when(props.getWebappName()).thenReturn(CONTEXT_NAME); - Thread.currentThread().setContextClassLoader(tccl); - } + @BeforeAll + public static void setupContextClassloader() { + originalTccl = Thread.currentThread().getContextClassLoader(); + final ClassLoader tccl = mock(ClassLoader.class, withSettings().extraInterfaces(WebappProperties.class)); + final WebappProperties props = (WebappProperties) tccl; + when(props.getServiceName()).thenReturn(ENGINE_NAME); + when(props.getHostName()).thenReturn(HOST_NAME); + when(props.getWebappName()).thenReturn(CONTEXT_NAME); + Thread.currentThread().setContextClassLoader(tccl); + } - @AfterAll - public static void clearContextClassloader() { - Thread.currentThread().setContextClassLoader(originalTccl); - } + @AfterAll + public static void clearContextClassloader() { + Thread.currentThread().setContextClassLoader(originalTccl); + } - static Stream data() { - return Stream.of( - Arguments.of("classloader.serviceName", ENGINE_NAME), - Arguments.of("engine.name", ENGINE_NAME), - Arguments.of("engine.logger", ENGINE_LOGGERNAME), - Arguments.of("classloader.hostName", HOST_NAME), - Arguments.of("host.name", HOST_NAME), - Arguments.of("host.logger", HOST_LOGGERNAME), - Arguments.of("classloader.webappName", CONTEXT_NAME), - Arguments.of("context.name", CONTEXT_NAME), - Arguments.of("context.logger", CONTEXT_LOGGERNAME), - Arguments.of(null, null)); - } + static Stream data() { + return Stream.of( + Arguments.of("classloader.serviceName", ENGINE_NAME), + Arguments.of("engine.name", ENGINE_NAME), + Arguments.of("engine.logger", ENGINE_LOGGERNAME), + Arguments.of("classloader.hostName", HOST_NAME), + Arguments.of("host.name", HOST_NAME), + Arguments.of("host.logger", HOST_LOGGERNAME), + Arguments.of("classloader.webappName", CONTEXT_NAME), + Arguments.of("context.name", CONTEXT_NAME), + Arguments.of("context.logger", CONTEXT_LOGGERNAME), + Arguments.of(null, null)); + } - @ParameterizedTest - @MethodSource("data") - public void lookupWorksProperly(final String key, final String value) { - final StrLookup lookup = new TomcatLookup(); - assertThat(lookup.lookup(key)).isEqualTo(value); - } + @ParameterizedTest + @MethodSource("data") + public void lookupWorksProperly(final String key, final String value) { + final StrLookup lookup = new TomcatLookup(); + assertThat(lookup.lookup(key)).isEqualTo(value); + } } diff --git a/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/ClassLoaderUtil.java b/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/ClassLoaderUtil.java index fdd4938..1295e8d 100644 --- a/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/ClassLoaderUtil.java +++ b/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/ClassLoaderUtil.java @@ -17,28 +17,28 @@ class ClassLoaderUtil { - static boolean isLog4jApiResource(final String name, final boolean isClassName) { - if (isClassName && name.startsWith("org.apache.logging.log4j.")) { - if (name.indexOf('.', 25) == -1 - || name.startsWith("internal.", 25) - || name.startsWith("message.", 25) - || name.startsWith("simple.", 25) - || name.startsWith("spi.", 25) - || name.startsWith("status.", 25) - || name.startsWith("util.", 25)) { - return true; - } - } else if (!isClassName && name.startsWith("org/apache/logging/log4j/")) { - if (name.indexOf('/', 25) == -1 - || name.startsWith("internal/", 25) - || name.startsWith("message/", 25) - || name.startsWith("simple/", 25) - || name.startsWith("spi/", 25) - || name.startsWith("status/", 25) - || name.startsWith("util/", 25)) { - return true; - } + static boolean isLog4jApiResource(final String name, final boolean isClassName) { + if (isClassName && name.startsWith("org.apache.logging.log4j.")) { + if (name.indexOf('.', 25) == -1 + || name.startsWith("internal.", 25) + || name.startsWith("message.", 25) + || name.startsWith("simple.", 25) + || name.startsWith("spi.", 25) + || name.startsWith("status.", 25) + || name.startsWith("util.", 25)) { + return true; + } + } else if (!isClassName && name.startsWith("org/apache/logging/log4j/")) { + if (name.indexOf('/', 25) == -1 + || name.startsWith("internal/", 25) + || name.startsWith("message/", 25) + || name.startsWith("simple/", 25) + || name.startsWith("spi/", 25) + || name.startsWith("status/", 25) + || name.startsWith("util/", 25)) { + return true; + } + } + return false; } - return false; - } } diff --git a/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jParallelWebappClassLoader.java b/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jParallelWebappClassLoader.java index 1506f93..d6b16f8 100644 --- a/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jParallelWebappClassLoader.java +++ b/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jParallelWebappClassLoader.java @@ -39,38 +39,38 @@ */ public class Log4jParallelWebappClassLoader extends ParallelWebappClassLoader { - public Log4jParallelWebappClassLoader() { - super(); - } - - public Log4jParallelWebappClassLoader(ClassLoader parent) { - super(parent); - } + public Log4jParallelWebappClassLoader() { + super(); + } - @Override - protected boolean filter(String name, boolean isClassName) { - if (name == null || name.length() < 25) { - return super.filter(name, isClassName); + public Log4jParallelWebappClassLoader(ClassLoader parent) { + super(parent); } - if (ClassLoaderUtil.isLog4jApiResource(name, isClassName)) { - return true; + + @Override + protected boolean filter(String name, boolean isClassName) { + if (name == null || name.length() < 25) { + return super.filter(name, isClassName); + } + if (ClassLoaderUtil.isLog4jApiResource(name, isClassName)) { + return true; + } + return super.filter(name, isClassName); } - return super.filter(name, isClassName); - } - @Override - public Log4jParallelWebappClassLoader copyWithoutTransformers() { + @Override + public Log4jParallelWebappClassLoader copyWithoutTransformers() { - Log4jParallelWebappClassLoader result = new Log4jParallelWebappClassLoader(getParent()); + Log4jParallelWebappClassLoader result = new Log4jParallelWebappClassLoader(getParent()); - super.copyStateWithoutTransformers(result); + super.copyStateWithoutTransformers(result); - try { - result.start(); - } catch (LifecycleException e) { - throw new IllegalStateException(e); - } + try { + result.start(); + } catch (LifecycleException e) { + throw new IllegalStateException(e); + } - return result; - } + return result; + } } diff --git a/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoader.java b/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoader.java index 38e20a4..88074c6 100644 --- a/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoader.java +++ b/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoader.java @@ -39,38 +39,38 @@ */ public class Log4jWebappClassLoader extends WebappClassLoader { - public Log4jWebappClassLoader() { - super(); - } - - public Log4jWebappClassLoader(ClassLoader parent) { - super(parent); - } + public Log4jWebappClassLoader() { + super(); + } - @Override - protected boolean filter(String name, boolean isClassName) { - if (name == null || name.length() < 25) { - return super.filter(name, isClassName); + public Log4jWebappClassLoader(ClassLoader parent) { + super(parent); } - if (ClassLoaderUtil.isLog4jApiResource(name, isClassName)) { - return true; + + @Override + protected boolean filter(String name, boolean isClassName) { + if (name == null || name.length() < 25) { + return super.filter(name, isClassName); + } + if (ClassLoaderUtil.isLog4jApiResource(name, isClassName)) { + return true; + } + return super.filter(name, isClassName); } - return super.filter(name, isClassName); - } - @Override - public Log4jWebappClassLoader copyWithoutTransformers() { + @Override + public Log4jWebappClassLoader copyWithoutTransformers() { - Log4jWebappClassLoader result = new Log4jWebappClassLoader(getParent()); + Log4jWebappClassLoader result = new Log4jWebappClassLoader(getParent()); - super.copyStateWithoutTransformers(result); + super.copyStateWithoutTransformers(result); - try { - result.start(); - } catch (LifecycleException e) { - throw new IllegalStateException(e); - } + try { + result.start(); + } catch (LifecycleException e) { + throw new IllegalStateException(e); + } - return result; - } + return result; + } } diff --git a/log4j-tomcat/src/test/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoaderTest.java b/log4j-tomcat/src/test/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoaderTest.java index 8164c11..5665a62 100644 --- a/log4j-tomcat/src/test/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoaderTest.java +++ b/log4j-tomcat/src/test/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoaderTest.java @@ -54,110 +54,101 @@ @TestMethodOrder(Random.class) public class Log4jWebappClassLoaderTest { - private static final String LOG4J_API_LINK = "org.apache.logging.log4j.api.link"; - private static final String LOG4J_CORE_LINK = "org.apache.logging.log4j.core.link"; + private static final String LOG4J_API_LINK = "org.apache.logging.log4j.api.link"; + private static final String LOG4J_CORE_LINK = "org.apache.logging.log4j.core.link"; - private URL findJar(final String link) throws IOException { - final ClassLoader cl = Log4jWebappClassLoaderTest.class.getClassLoader(); - try (final InputStream is = cl.getResourceAsStream(link); - final Reader reader = new InputStreamReader(is, StandardCharsets.US_ASCII)) { - return new URL(IOUtils.toString(reader)); + private URL findJar(final String link) throws IOException { + final ClassLoader cl = Log4jWebappClassLoaderTest.class.getClassLoader(); + try (final InputStream is = cl.getResourceAsStream(link); + final Reader reader = new InputStreamReader(is, StandardCharsets.US_ASCII)) { + return new URL(IOUtils.toString(reader)); + } } - } - private T createClassLoader(final Class clazz) { - try { - final WebResourceRoot resources = new StandardRoot(mock(Context.class)); - resources.createWebResourceSet( - ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", findJar(LOG4J_API_LINK), "/"); - resources.createWebResourceSet( - ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", findJar(LOG4J_CORE_LINK), "/"); - resources.start(); - final Constructor constructor = clazz.getConstructor(ClassLoader.class); - final T cl = constructor.newInstance(Log4jWebappClassLoaderTest.class.getClassLoader()); - cl.setResources(resources); - cl.start(); - return cl; - } catch (ReflectiveOperationException | LifecycleException | IOException e) { - fail("Unable to instantiate classloader.", e); + private T createClassLoader(final Class clazz) { + try { + final WebResourceRoot resources = new StandardRoot(mock(Context.class)); + resources.createWebResourceSet( + ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", findJar(LOG4J_API_LINK), "/"); + resources.createWebResourceSet( + ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", findJar(LOG4J_CORE_LINK), "/"); + resources.start(); + final Constructor constructor = clazz.getConstructor(ClassLoader.class); + final T cl = constructor.newInstance(Log4jWebappClassLoaderTest.class.getClassLoader()); + cl.setResources(resources); + cl.start(); + return cl; + } catch (ReflectiveOperationException | LifecycleException | IOException e) { + fail("Unable to instantiate classloader.", e); + } + // never reached + return null; } - // never reached - return null; - } - static Stream classes() { - return Stream.of( - Arguments.of(LogManager.class, true, false), - Arguments.of(DefaultLogBuilder.class, true, false), - Arguments.of(Message.class, true, false), - Arguments.of(SimpleLogger.class, true, false), - Arguments.of(LoggerContext.class, true, false), - Arguments.of(StatusListener.class, true, false), - Arguments.of(PropertiesUtil.class, true, false), - // TODO: loading LoggerContext fails - // Arguments.of(org.apache.logging.log4j.core.LoggerContext.class, false, - // false), - Arguments.of(Configuration.class, false, false)); - // Arguments.of(TomcatLookup.class, true, true)); - } + static Stream classes() { + return Stream.of( + Arguments.of(LogManager.class, true, false), + Arguments.of(DefaultLogBuilder.class, true, false), + Arguments.of(Message.class, true, false), + Arguments.of(SimpleLogger.class, true, false), + Arguments.of(LoggerContext.class, true, false), + Arguments.of(StatusListener.class, true, false), + Arguments.of(PropertiesUtil.class, true, false), + // TODO: loading LoggerContext fails + // Arguments.of(org.apache.logging.log4j.core.LoggerContext.class, false, + // false), + Arguments.of(Configuration.class, false, false)); + // Arguments.of(TomcatLookup.class, true, true)); + } - @RepeatedTest(100) - public void testStandardClassloader() throws IOException { - try (final URLClassLoader cl = createClassLoader(WebappClassLoader.class); ) { - classes() - .forEach( - arg -> { + @RepeatedTest(100) + public void testStandardClassloader() throws IOException { + try (final URLClassLoader cl = createClassLoader(WebappClassLoader.class); ) { + classes().forEach(arg -> { final Class clazz = (Class) arg.get()[0]; final boolean isEqual = (boolean) arg.get()[2]; - final Class otherClazz = - assertDoesNotThrow(() -> Class.forName(clazz.getName(), true, cl)); + final Class otherClazz = assertDoesNotThrow(() -> Class.forName(clazz.getName(), true, cl)); final ClassAssert assertion = assertThat(otherClazz); if (isEqual) { - assertion.isEqualTo(clazz); + assertion.isEqualTo(clazz); } else { - assertion.isNotEqualTo(clazz); + assertion.isNotEqualTo(clazz); } - }); + }); + } } - } - @RepeatedTest(100) - public void testLog4jClassloader() throws IOException { - try (final URLClassLoader cl = createClassLoader(Log4jWebappClassLoader.class); ) { - classes() - .forEach( - arg -> { + @RepeatedTest(100) + public void testLog4jClassloader() throws IOException { + try (final URLClassLoader cl = createClassLoader(Log4jWebappClassLoader.class); ) { + classes().forEach(arg -> { final Class clazz = (Class) arg.get()[0]; final boolean isEqual = (boolean) arg.get()[1]; - final Class otherClazz = - assertDoesNotThrow(() -> Class.forName(clazz.getName(), true, cl)); + final Class otherClazz = assertDoesNotThrow(() -> Class.forName(clazz.getName(), true, cl)); final ClassAssert assertion = assertThat(otherClazz); if (isEqual) { - assertion.isEqualTo(clazz); + assertion.isEqualTo(clazz); } else { - assertion.isNotEqualTo(clazz); + assertion.isNotEqualTo(clazz); } - }); + }); + } } - } - @RepeatedTest(100) - public void testParallelLog4jClassloader() throws IOException { - try (final URLClassLoader cl = createClassLoader(Log4jParallelWebappClassLoader.class); ) { - classes() - .forEach( - arg -> { + @RepeatedTest(100) + public void testParallelLog4jClassloader() throws IOException { + try (final URLClassLoader cl = createClassLoader(Log4jParallelWebappClassLoader.class); ) { + classes().forEach(arg -> { final Class clazz = (Class) arg.get()[0]; final boolean isEqual = (boolean) arg.get()[1]; - final Class otherClazz = - assertDoesNotThrow(() -> Class.forName(clazz.getName(), true, cl)); + final Class otherClazz = assertDoesNotThrow(() -> Class.forName(clazz.getName(), true, cl)); final ClassAssert assertion = assertThat(otherClazz); if (isEqual) { - assertion.isEqualTo(clazz); + assertion.isEqualTo(clazz); } else { - assertion.isNotEqualTo(clazz); + assertion.isNotEqualTo(clazz); } - }); + }); + } } - } } diff --git a/pom.xml b/pom.xml index 47ec8b6..ca8d0b7 100644 --- a/pom.xml +++ b/pom.xml @@ -84,7 +84,7 @@ piotr.github@karwasz.org UTF-8 2.38.0 - 1.17.0 + 2.38.0 10.0.23 @@ -300,11 +300,9 @@ * limitations under the License. */ - - ${google-java-format.version} - - true - + + ${palantir-java-format.version} +