Skip to content

Commit

Permalink
Switch to Palantir Java format
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Dec 4, 2023
1 parent a84657f commit 9fec4c0
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arguments> 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<Arguments> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading

0 comments on commit 9fec4c0

Please sign in to comment.