Skip to content

Commit

Permalink
Add the ability to track loggers to the ContextConfiguration. Rename …
Browse files Browse the repository at this point in the history
…some components and make some minor changes.

Signed-off-by: James R. Perkins <[email protected]>
  • Loading branch information
jamezp committed May 11, 2023
1 parent ee6db02 commit ef7aeec
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.logging.Formatter;
import java.util.logging.Handler;

import org.jboss.logmanager.LogContext;
import org.jboss.logmanager.Logger;

/**
Expand All @@ -47,8 +48,10 @@
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@SuppressWarnings({ "UnusedReturnValue", "unused" })
public class ContextConfiguration {
public class ContextConfiguration implements AutoCloseable {
public static final Logger.AttachmentKey<ContextConfiguration> CONTEXT_CONFIGURATION_KEY = new Logger.AttachmentKey<>();
private final LogContext context;
private final Map<String, Logger> loggers;
private final Map<String, Supplier<ErrorManager>> errorManagers;
private final Map<String, Supplier<Filter>> filters;
private final Map<String, Supplier<Formatter>> formatters;
Expand All @@ -58,14 +61,82 @@ public class ContextConfiguration {
/**
* Creates a new context configuration.
*/
public ContextConfiguration() {
public ContextConfiguration(final LogContext context) {
this.context = context;
loggers = new ConcurrentHashMap<>();
errorManagers = new ConcurrentHashMap<>();
handlers = new ConcurrentHashMap<>();
formatters = new ConcurrentHashMap<>();
filters = new ConcurrentHashMap<>();
objects = new ConcurrentHashMap<>();
}

/**
* Returns the {@linkplain LogContext context} for this configuration.
*
* @return the context for this configuration
*/
public LogContext getContext() {
return context;
}

/**
* Adds a logger to the context configuration.
*
* @param name the name of the logger
* @param logger the logger
*
* @return the previous logger or {@code null} if one did not exist
*/
public Logger addLogger(final String name, final Logger logger) {
if (logger == null) {
return loggers.remove(Objects.requireNonNull(name, "The name cannot be null"));
}
return loggers.put(Objects.requireNonNull(name, "The name cannot be null"), logger);
}

/**
* Checks if the logger exists in this context.
*
* @param name the logger name
*
* @return {@code true} if the logger exists in this context, otherwise {@code false}
*/
public boolean hasLogger(final String name) {
return loggers.containsKey(Objects.requireNonNull(name, "The name cannot be null"));
}

/**
* Gets the logger if it exists.
*
* @param name the name of the logger
*
* @return the logger or {@code null} if the logger does not exist
*/
public Logger getLogger(final String name) {
return loggers.get(Objects.requireNonNull(name, "The name cannot be null"));
}

/**
* Returns an unmodifiable map of the loggers.
*
* @return an unmodified map of the loggers
*/
public Map<String, Logger> getLoggers() {
return Collections.unmodifiableMap(loggers);
}

/**
* Removes the logger from the context configuration.
*
* @param name the name of the logger
*
* @return the logger removed or {@code null} if the logger did not exist
*/
public Logger removeLogger(final String name) {
return loggers.remove(Objects.requireNonNull(name, "The name cannot be null"));
}

/**
* Adds an error manager to the context configuration.
*
Expand Down Expand Up @@ -372,28 +443,15 @@ public Map<String, Supplier<Object>> getObjects() {
return Collections.unmodifiableMap(objects);
}

private static class SingletonSupplier<T> implements Supplier<T> {
private final Supplier<T> supplier;
private volatile T instance;

private SingletonSupplier(final Supplier<T> supplier) {
this.supplier = supplier;
}

static <T> Supplier<T> of(final Supplier<T> supplier) {
return new SingletonSupplier<>(supplier);
}

@Override
public T get() {
if (instance == null) {
synchronized (this) {
if (instance == null) {
instance = supplier.get();
}
}
}
return instance;
}
@Override
public void close() throws Exception {
context.close();
loggers.clear();
handlers.clear();
filters.clear();
formatters.clear();
errorManagers.clear();
objects.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class DefaultConfiguratorFactory implements ConfiguratorFactory {

@Override
public LogContextConfigurator create() {
return new DefaultLogContextConfigurator();
return new PropertyLogContextConfigurator();
}

@Override
Expand Down
50 changes: 23 additions & 27 deletions src/main/java/org/jboss/logmanager/configuration/ObjectBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.logging.Level;
import java.util.regex.Pattern;

import org.jboss.logmanager.LogContext;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleLoader;

Expand All @@ -44,8 +43,6 @@
*/
@SuppressWarnings({ "UnusedReturnValue" })
class ObjectBuilder<T> {

private final LogContext logContext;
private final ContextConfiguration contextConfiguration;
private final Class<? extends T> baseClass;
private final String className;
Expand All @@ -55,9 +52,8 @@ class ObjectBuilder<T> {
private final Set<String> postConstructMethods;
private String moduleName;

private ObjectBuilder(final LogContext logContext, final ContextConfiguration contextConfiguration,
private ObjectBuilder(final ContextConfiguration contextConfiguration,
final Class<? extends T> baseClass, final String className) {
this.logContext = logContext;
this.contextConfiguration = contextConfiguration;
this.baseClass = baseClass;
this.className = className;
Expand All @@ -70,16 +66,15 @@ private ObjectBuilder(final LogContext logContext, final ContextConfiguration co
/**
* Create a new {@link ObjectBuilder}.
*
* @param logContext the log context being configured
* @param baseClass the base type
* @param className the name of the class to create
* @param <T> the type being created
* @param baseClass the base type
* @param className the name of the class to create
* @param <T> the type being created
*
* @return a new {@link ObjectBuilder}
*/
static <T> ObjectBuilder<T> of(final LogContext logContext, final ContextConfiguration contextConfiguration,
static <T> ObjectBuilder<T> of(final ContextConfiguration contextConfiguration,
final Class<? extends T> baseClass, final String className) {
return new ObjectBuilder<>(logContext, contextConfiguration, baseClass, className);
return new ObjectBuilder<>(contextConfiguration, baseClass, className);
}

/**
Expand Down Expand Up @@ -272,39 +267,40 @@ private Object getValue(final Class<?> objClass, final String propertyName, fina
}
return null;
}
final var trimmedValue = value.trim();
if (paramType == String.class) {
// Don't use the trimmed value for strings
return value;
} else if (paramType == java.util.logging.Level.class) {
return logContext.getLevelForName(value);
return contextConfiguration.getContext().getLevelForName(trimmedValue);
} else if (paramType == java.util.logging.Logger.class) {
return logContext.getLogger(value);
return contextConfiguration.getContext().getLogger(trimmedValue);
} else if (paramType == boolean.class || paramType == Boolean.class) {
return Boolean.valueOf(value);
return Boolean.valueOf(trimmedValue);
} else if (paramType == byte.class || paramType == Byte.class) {
return Byte.valueOf(value);
return Byte.valueOf(trimmedValue);
} else if (paramType == short.class || paramType == Short.class) {
return Short.valueOf(value);
return Short.valueOf(trimmedValue);
} else if (paramType == int.class || paramType == Integer.class) {
return Integer.valueOf(value);
return Integer.valueOf(trimmedValue);
} else if (paramType == long.class || paramType == Long.class) {
return Long.valueOf(value);
return Long.valueOf(trimmedValue);
} else if (paramType == float.class || paramType == Float.class) {
return Float.valueOf(value);
return Float.valueOf(trimmedValue);
} else if (paramType == double.class || paramType == Double.class) {
return Double.valueOf(value);
return Double.valueOf(trimmedValue);
} else if (paramType == char.class || paramType == Character.class) {
return value.length() > 0 ? value.charAt(0) : 0;
return trimmedValue.length() > 0 ? trimmedValue.charAt(0) : 0;
} else if (paramType == TimeZone.class) {
return TimeZone.getTimeZone(value);
return TimeZone.getTimeZone(trimmedValue);
} else if (paramType == Charset.class) {
return Charset.forName(value);
return Charset.forName(trimmedValue);
} else if (paramType.isAssignableFrom(Level.class)) {
return Level.parse(value);
return Level.parse(trimmedValue);
} else if (paramType.isEnum()) {
return Enum.valueOf(paramType.asSubclass(Enum.class), value);
} else if (contextConfiguration.hasObject(value)) {
return contextConfiguration.getObject(value);
return Enum.valueOf(paramType.asSubclass(Enum.class), trimmedValue);
} else if (contextConfiguration.hasObject(trimmedValue)) {
return contextConfiguration.getObject(trimmedValue);
} else if (definedPropertiesContains(propertyName)) {
final PropertyValue propertyValue = findDefinedProperty(propertyName);
if (propertyValue == null) {
Expand Down
Loading

0 comments on commit ef7aeec

Please sign in to comment.