-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates a parallel-capable classloader
Closes #46.
- Loading branch information
Showing
4 changed files
with
145 additions
and
23 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/ClassLoaderUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright © 2022 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; | ||
|
||
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; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jParallelWebappClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright © 2022 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 org.apache.catalina.LifecycleException; | ||
import org.apache.catalina.loader.ParallelWebappClassLoader; | ||
|
||
/** | ||
* A classloader that delegates lookups for Log4j2 API classes to the parent classloader. | ||
* | ||
* <p>This allows multiple applications bundled with Log4j2 API to use a single copy of Log4j2 in | ||
* the parent classloader, without modifying the servlet delegation model for the application. | ||
* | ||
* <p>In order to replace the default application classloader add: | ||
* | ||
* <pre> | ||
* <Context> | ||
* ... | ||
* <Loader loaderClass="eu.copernik.log4j.tomcat.Log4jParallelWebappClassLoader"/> | ||
* </Context> | ||
* </pre> | ||
* | ||
* <p>to the application's context definition (cf. <a href= | ||
* "https://tomcat.apache.org/tomcat-10.0-doc/config/context.html#Defining_a_context">defining a | ||
* context</a>). | ||
*/ | ||
public class Log4jParallelWebappClassLoader extends ParallelWebappClassLoader { | ||
|
||
public Log4jParallelWebappClassLoader() { | ||
super(); | ||
} | ||
|
||
public Log4jParallelWebappClassLoader(ClassLoader parent) { | ||
super(parent); | ||
} | ||
|
||
@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); | ||
} | ||
|
||
@Override | ||
public Log4jParallelWebappClassLoader copyWithoutTransformers() { | ||
|
||
Log4jParallelWebappClassLoader result = new Log4jParallelWebappClassLoader(getParent()); | ||
|
||
super.copyStateWithoutTransformers(result); | ||
|
||
try { | ||
result.start(); | ||
} catch (LifecycleException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters