Skip to content

Commit

Permalink
Refactor exception-handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Feb 27, 2024
1 parent 5341c0e commit 5b249c1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package eu.copernik.log4j.tomcat;

import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;

final class ClassLoaderUtil {

static final int PREFIX_LENGTH = 25;
Expand All @@ -40,4 +43,12 @@ static boolean isLog4jApiResource(final String name, final boolean isClassName)
|| name.startsWith("status/", PREFIX_LENGTH)
|| name.startsWith("util/", PREFIX_LENGTH));
}

static void startUnchecked(final Lifecycle lifecycle) {
try {
lifecycle.start();
} catch (final LifecycleException e) {
throw new IllegalStateException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import static eu.copernik.log4j.tomcat.ClassLoaderUtil.PREFIX_LENGTH;
import static eu.copernik.log4j.tomcat.ClassLoaderUtil.isLog4jApiResource;
import static eu.copernik.log4j.tomcat.ClassLoaderUtil.startUnchecked;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Objects;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.loader.ParallelWebappClassLoader;

/**
Expand Down Expand Up @@ -64,18 +64,9 @@ protected boolean filter(final String name, final boolean isClassName) {
@Override
@SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
public Log4jParallelWebappClassLoader copyWithoutTransformers() {

final Log4jParallelWebappClassLoader result = new Log4jParallelWebappClassLoader(getParent());

super.copyStateWithoutTransformers(result);

try {
result.start();
} catch (final LifecycleException e) {
// Currently unreachable
throw new IllegalStateException(e);
}

startUnchecked(result);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import static eu.copernik.log4j.tomcat.ClassLoaderUtil.PREFIX_LENGTH;
import static eu.copernik.log4j.tomcat.ClassLoaderUtil.isLog4jApiResource;
import static eu.copernik.log4j.tomcat.ClassLoaderUtil.startUnchecked;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Objects;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.loader.WebappClassLoader;

/**
Expand Down Expand Up @@ -64,18 +64,9 @@ protected boolean filter(final String name, final boolean isClassName) {
@Override
@SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
public Log4jWebappClassLoader copyWithoutTransformers() {

final Log4jWebappClassLoader result = new Log4jWebappClassLoader(getParent());

super.copyStateWithoutTransformers(result);

try {
result.start();
} catch (final LifecycleException e) {
// Currently unreachable
throw new IllegalStateException(e);
}

startUnchecked(result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright © 2024 Piotr P. Karwasz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.copernik.log4j.tomcat;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.junit.jupiter.api.Test;

class ClassLoaderUtilTest {

@Test
void when_lifecycle_exception_then_illegal_state_exception() throws Exception {
final Lifecycle lifecycle = mock(Lifecycle.class);
assertDoesNotThrow(() -> ClassLoaderUtil.startUnchecked(lifecycle));
doThrow(new LifecycleException()).when(lifecycle).start();
assertThrows(IllegalStateException.class, () -> ClassLoaderUtil.startUnchecked(lifecycle));
}
}
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,6 @@
</configuration>
</plugin>


<!--
~ SITE configuration
-->
Expand All @@ -892,10 +891,10 @@
<goal>process-asciidoc</goal>
</goals>
<phase>site</phase>
<inherited>false</inherited>
<configuration>
<preserveDirectories>true</preserveDirectories>
</configuration>
<inherited>false</inherited>
</execution>
</executions>
</plugin>
Expand Down

0 comments on commit 5b249c1

Please sign in to comment.