Skip to content

Commit 770a79a

Browse files
committed
LOG4J2-2621 - Initial commit
1 parent 8076a70 commit 770a79a

File tree

387 files changed

+1073
-3525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

387 files changed

+1073
-3525
lines changed

log4j-1.2-api/src/main/java/org/apache/log4j/Category.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.apache.log4j.spi.LoggerFactory;
2828
import org.apache.log4j.spi.LoggingEvent;
2929
import org.apache.logging.log4j.core.LoggerContext;
30-
import org.apache.logging.log4j.core.util.NameUtil;
30+
import org.apache.logging.log4j.util.NameUtil;
3131
import org.apache.logging.log4j.message.LocalizedMessage;
3232
import org.apache.logging.log4j.message.Message;
3333
import org.apache.logging.log4j.message.ObjectMessage;

log4j-1.2-api/src/main/java/org/apache/log4j/layout/Log4j1XmlLayout.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323

2424
import org.apache.logging.log4j.core.Layout;
2525
import org.apache.logging.log4j.core.LogEvent;
26-
import org.apache.logging.log4j.core.config.Node;
27-
import org.apache.logging.log4j.core.config.plugins.Plugin;
28-
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
29-
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
3026
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
3127
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
3228
import org.apache.logging.log4j.core.util.Transform;
29+
import org.apache.logging.log4j.plugins.Node;
30+
import org.apache.logging.log4j.plugins.Plugin;
31+
import org.apache.logging.log4j.plugins.PluginAttribute;
32+
import org.apache.logging.log4j.plugins.PluginFactory;
3333
import org.apache.logging.log4j.util.BiConsumer;
3434
import org.apache.logging.log4j.util.ReadOnlyStringMap;
3535
import org.apache.logging.log4j.util.Strings;

log4j-1.2-api/src/main/java/org/apache/log4j/pattern/Log4j1MdcPatternConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.log4j.pattern;
1818

1919
import org.apache.logging.log4j.core.LogEvent;
20-
import org.apache.logging.log4j.core.config.plugins.Plugin;
20+
import org.apache.logging.log4j.plugins.Plugin;
2121
import org.apache.logging.log4j.core.pattern.ConverterKeys;
2222
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
2323
import org.apache.logging.log4j.core.pattern.PatternConverter;

log4j-1.2-api/src/main/java/org/apache/log4j/pattern/Log4j1NdcPatternConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.log4j.pattern;
1818

1919
import org.apache.logging.log4j.core.LogEvent;
20-
import org.apache.logging.log4j.core.config.plugins.Plugin;
20+
import org.apache.logging.log4j.plugins.Plugin;
2121
import org.apache.logging.log4j.core.pattern.ConverterKeys;
2222
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
2323
import org.apache.logging.log4j.core.pattern.PatternConverter;

log4j-api/src/main/java/org/apache/logging/log4j/util/Assert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the license for the specific language governing permissions and
1515
* limitations under the license.
1616
*/
17-
package org.apache.logging.log4j.core.util;
17+
package org.apache.logging.log4j.util;
1818

1919
import java.util.Collection;
2020
import java.util.Map;

log4j-api/src/main/java/org/apache/logging/log4j/util/InternalException.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
* See the license for the specific language governing permissions and
1515
* limitations under the license.
1616
*/
17-
package org.apache.logging.log4j;
17+
package org.apache.logging.log4j.util;
1818

1919
/**
20-
* Exception thrown when an error occurs while logging. In most cases exceptions will be handled
21-
* within Log4j but certain Appenders may be configured to allow exceptions to propagate to the
22-
* application. This is a RuntimeException so that the exception may be thrown in those cases without
23-
* requiring all Logger methods be contained with try/catch blocks.
20+
* Exception thrown when an error occurs while accessing internal resources. This is generally used to
21+
* convert checked exceptions to runtime exceptions.
2422
*/
25-
public class LoggingException extends RuntimeException {
23+
public class InternalException extends RuntimeException {
2624

2725
private static final long serialVersionUID = 6366395965071580537L;
2826

@@ -31,7 +29,7 @@ public class LoggingException extends RuntimeException {
3129
*
3230
* @param message The reason for the exception
3331
*/
34-
public LoggingException(final String message) {
32+
public InternalException(final String message) {
3533
super(message);
3634
}
3735

@@ -41,7 +39,7 @@ public LoggingException(final String message) {
4139
* @param message The reason for the exception
4240
* @param cause The underlying cause of the exception
4341
*/
44-
public LoggingException(final String message, final Throwable cause) {
42+
public InternalException(final String message, final Throwable cause) {
4543
super(message, cause);
4644
}
4745

@@ -50,7 +48,7 @@ public LoggingException(final String message, final Throwable cause) {
5048
*
5149
* @param cause The underlying cause of the exception
5250
*/
53-
public LoggingException(final Throwable cause) {
51+
public InternalException(final Throwable cause) {
5452
super(cause);
5553
}
5654
}

log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public final class LoaderUtil {
7676
private LoaderUtil() {
7777
}
7878

79+
/**
80+
* Returns the ClassLoader to use.
81+
* @return the ClassLoader.
82+
*/
83+
public static ClassLoader getClassLoader() {
84+
return getClassLoader(LoaderUtil.class, null);
85+
}
86+
87+
// TODO: this method could use some explanation
88+
public static ClassLoader getClassLoader(final Class<?> class1, final Class<?> class2) {
89+
final ClassLoader threadContextClassLoader = getThreadContextClassLoader();
90+
final ClassLoader loader1 = class1 == null ? null : class1.getClassLoader();
91+
final ClassLoader loader2 = class2 == null ? null : class2.getClassLoader();
92+
93+
if (isChild(threadContextClassLoader, loader1)) {
94+
return isChild(threadContextClassLoader, loader2) ? threadContextClassLoader : loader2;
95+
}
96+
return isChild(loader1, loader2) ? loader1 : loader2;
97+
}
98+
7999
/**
80100
* Gets the current Thread ClassLoader. Returns the system ClassLoader if the TCCL is {@code null}. If the system
81101
* ClassLoader is {@code null} as well, then the ClassLoader for this class is returned. If running with a
@@ -93,6 +113,26 @@ public static ClassLoader getThreadContextClassLoader() {
93113
return SECURITY_MANAGER == null ? TCCL_GETTER.run() : AccessController.doPrivileged(TCCL_GETTER);
94114
}
95115

116+
/**
117+
* Determines if one ClassLoader is a child of another ClassLoader. Note that a {@code null} ClassLoader is
118+
* interpreted as the system ClassLoader as per convention.
119+
*
120+
* @param loader1 the ClassLoader to check for childhood.
121+
* @param loader2 the ClassLoader to check for parenthood.
122+
* @return {@code true} if the first ClassLoader is a strict descendant of the second ClassLoader.
123+
*/
124+
private static boolean isChild(final ClassLoader loader1, final ClassLoader loader2) {
125+
if (loader1 != null && loader2 != null) {
126+
ClassLoader parent = loader1.getParent();
127+
while (parent != null && parent != loader2) {
128+
parent = parent.getParent();
129+
}
130+
// once parent is null, we're at the system CL, which would indicate they have separate ancestry
131+
return parent != null;
132+
}
133+
return loader1 != null;
134+
}
135+
96136
/**
97137
*
98138
*/

log4j-api/src/main/java/org/apache/logging/log4j/util/NameUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the license for the specific language governing permissions and
1515
* limitations under the license.
1616
*/
17-
package org.apache.logging.log4j.core.util;
17+
package org.apache.logging.log4j.util;
1818

1919
import java.security.MessageDigest;
2020

log4j-api/src/main/java/org/apache/logging/log4j/util/ReflectionUtil.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the license.
1616
*/
1717

18-
package org.apache.logging.log4j.core.util;
18+
package org.apache.logging.log4j.util;
1919

2020
import java.lang.reflect.AccessibleObject;
2121
import java.lang.reflect.Constructor;
@@ -193,10 +193,13 @@ public static <T> T instantiate(final Class<T> clazz) {
193193
} catch (final IllegalAccessException e) {
194194
throw new IllegalStateException(e);
195195
} catch (final InvocationTargetException e) {
196-
if (e.getCause() instanceof RuntimeException) {
197-
throw (RuntimeException) e.getCause();
196+
if (e.getCause() != null) {
197+
if (e.getCause() instanceof RuntimeException) {
198+
throw (RuntimeException) e.getCause();
199+
}
200+
throw new InternalException(e.getCause());
198201
}
199-
throw new
202+
throw new InternalException("Error creating new instance of " + clazz.getName(), e);
200203
}
201204
}
202205
}

log4j-api/src/main/java/org/apache/logging/log4j/util/StringMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ public interface StringMap extends ReadOnlyStringMap {
9595
* @throws UnsupportedOperationException if this collection has been {@linkplain #isFrozen() frozen}.
9696
*/
9797
void remove(final String key);
98-
}
98+
}

0 commit comments

Comments
 (0)