package org.apache.logging.log4j.spi;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.Properties;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;
public class Provider {
public static final String FACTORY_PRIORITY = "FactoryPriority";
public static final String THREAD_CONTEXT_MAP = "ThreadContextMap";
public static final String LOGGER_CONTEXT_FACTORY = "LoggerContextFactory";
private static final Integer DEFAULT_PRIORITY = Integer.valueOf(-1);
private static final Logger LOGGER = StatusLogger.getLogger();
private final Integer priority;
private final String className;
private final Class<? extends LoggerContextFactory> loggerContextFactoryClass;
private final String threadContextMap;
private final Class<? extends ThreadContextMap> threadContextMapClass;
private final String versions;
private final URL url;
private final WeakReference<ClassLoader> classLoader;
public Provider(final Properties props, final URL url, final ClassLoader classLoader) {
this.url = url;
this.classLoader = new WeakReference<>(classLoader);
final String weight = props.getProperty(FACTORY_PRIORITY);
priority = weight == null ? DEFAULT_PRIORITY : Integer.valueOf(weight);
className = props.getProperty(LOGGER_CONTEXT_FACTORY);
threadContextMap = props.getProperty(THREAD_CONTEXT_MAP);
loggerContextFactoryClass = null;
threadContextMapClass = null;
versions = null;
}
public Provider(final Integer priority, final String versions,
final Class<? extends LoggerContextFactory> loggerContextFactoryClass) {
this(priority, versions, loggerContextFactoryClass, null);
}
public Provider(final Integer priority, final String versions,
final Class<? extends LoggerContextFactory> loggerContextFactoryClass,
final Class<? extends ThreadContextMap> threadContextMapClass) {
this.url = null;
this.classLoader = null;
this.priority = priority;
this.loggerContextFactoryClass = loggerContextFactoryClass;
this.threadContextMapClass = threadContextMapClass;
this.className = null;
this.threadContextMap = null;
this.versions = versions;
}
public String getVersions() {
return versions;
}
public Integer getPriority() {
return priority;
}
public String getClassName() {
if (loggerContextFactoryClass != null) {
return loggerContextFactoryClass.getName();
}
return className;
}
public Class<? extends LoggerContextFactory> loadLoggerContextFactory() {
if (loggerContextFactoryClass != null) {
return loggerContextFactoryClass;
}
if (className == null) {
return null;
}
final ClassLoader loader = classLoader.get();
if (loader == null) {
return null;
}
try {
final Class<?> clazz = loader.loadClass(className);
if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
return clazz.asSubclass(LoggerContextFactory.class);
}
} catch (final Exception e) {
LOGGER.error("Unable to create class {} specified in {}", className, url.toString(), e);
}
return null;
}
public String getThreadContextMap() {
if (threadContextMapClass != null) {
return threadContextMapClass.getName();
}
return threadContextMap;
}
public Class<? extends ThreadContextMap> loadThreadContextMap() {
if (threadContextMapClass != null) {
return threadContextMapClass;
}
if (threadContextMap == null) {
return null;
}
final ClassLoader loader = classLoader.get();
if (loader == null) {
return null;
}
try {
final Class<?> clazz = loader.loadClass(threadContextMap);
if (ThreadContextMap.class.isAssignableFrom(clazz)) {
return clazz.asSubclass(ThreadContextMap.class);
}
} catch (final Exception e) {
LOGGER.error("Unable to create class {} specified in {}", threadContextMap, url.toString(), e);
}
return null;
}
public URL getUrl() {
return url;
}
@Override
public String toString() {
final StringBuilder result = new StringBuilder("Provider[");
if (!DEFAULT_PRIORITY.equals(priority)) {
result.append("priority=").append(priority).append(", ");
}
if (threadContextMap != null) {
result.append("threadContextMap=").append(threadContextMap).append(", ");
} else if (threadContextMapClass != null) {
result.append("threadContextMapClass=").append(threadContextMapClass.getName());
}
if (className != null) {
result.append("className=").append(className).append(", ");
} else if (loggerContextFactoryClass != null) {
result.append("class=").append(loggerContextFactoryClass.getName());
}
if (url != null) {
result.append("url=").append(url);
}
final ClassLoader loader;
if (classLoader == null || (loader = classLoader.get()) == null) {
result.append(", classLoader=null(not reachable)");
} else {
result.append(", classLoader=").append(loader);
}
result.append("]");
return result.toString();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Provider provider = (Provider) o;
if (priority != null ? !priority.equals(provider.priority) : provider.priority != null) {
return false;
}
if (className != null ? !className.equals(provider.className) : provider.className != null) {
return false;
}
if (loggerContextFactoryClass != null ? !loggerContextFactoryClass.equals(provider.loggerContextFactoryClass) : provider.loggerContextFactoryClass != null) {
return false;
}
return versions != null ? versions.equals(provider.versions) : provider.versions == null;
}
@Override
public int hashCode() {
int result = priority != null ? priority.hashCode() : 0;
result = 31 * result + (className != null ? className.hashCode() : 0);
result = 31 * result + (loggerContextFactoryClass != null ? loggerContextFactoryClass.hashCode() : 0);
result = 31 * result + (versions != null ? versions.hashCode() : 0);
return result;
}
}