-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to track loggers to the ContextConfiguration. Rename …
…some components and make some minor changes. Signed-off-by: James R. Perkins <[email protected]>
- Loading branch information
Showing
7 changed files
with
224 additions
and
125 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import java.util.logging.Formatter; | ||
import java.util.logging.Handler; | ||
|
||
import org.jboss.logmanager.LogContext; | ||
import org.jboss.logmanager.Logger; | ||
|
||
/** | ||
|
@@ -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; | ||
|
@@ -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. | ||
* | ||
|
@@ -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(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.