-
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
229 additions
and
130 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. | ||
* | ||
|
@@ -79,7 +150,7 @@ public Supplier<ErrorManager> addErrorManager(final String name, final Supplier< | |
return removeErrorManager(name); | ||
} | ||
return errorManagers.putIfAbsent(Objects.requireNonNull(name, "The name cannot be null"), | ||
SingletonSupplier.of(errorManager)); | ||
LazySupplier.of(errorManager)); | ||
} | ||
|
||
/** | ||
|
@@ -140,7 +211,7 @@ public Supplier<Handler> addHandler(final String name, final Supplier<Handler> h | |
return removeHandler(name); | ||
} | ||
return handlers.putIfAbsent(Objects.requireNonNull(name, "The name cannot be null"), | ||
SingletonSupplier.of(handler)); | ||
LazySupplier.of(handler)); | ||
} | ||
|
||
/** | ||
|
@@ -201,7 +272,7 @@ public Supplier<Formatter> addFormatter(final String name, final Supplier<Format | |
return removeFormatter(name); | ||
} | ||
return formatters.putIfAbsent(Objects.requireNonNull(name, "The name cannot be null"), | ||
SingletonSupplier.of(formatter)); | ||
LazySupplier.of(formatter)); | ||
} | ||
|
||
/** | ||
|
@@ -262,7 +333,7 @@ public Supplier<Filter> addFilter(final String name, final Supplier<Filter> filt | |
return removeFilter(name); | ||
} | ||
return filters.putIfAbsent(Objects.requireNonNull(name, "The name cannot be null"), | ||
SingletonSupplier.of(filter)); | ||
LazySupplier.of(filter)); | ||
} | ||
|
||
/** | ||
|
@@ -324,7 +395,7 @@ public Supplier<Object> addObject(final String name, final Supplier<Object> obje | |
return removeObject(name); | ||
} | ||
return objects.putIfAbsent(Objects.requireNonNull(name, "The name cannot be null"), | ||
SingletonSupplier.of(object)); | ||
LazySupplier.of(object)); | ||
} | ||
|
||
/** | ||
|
@@ -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
53 changes: 53 additions & 0 deletions
53
src/main/java/org/jboss/logmanager/configuration/LazySupplier.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,53 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2022 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* 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 org.jboss.logmanager.configuration; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
class LazySupplier<T> implements Supplier<T> { | ||
private final Supplier<T> supplier; | ||
private volatile T instance; | ||
|
||
private LazySupplier(final Supplier<T> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
static <T> Supplier<T> of(final Supplier<T> supplier) { | ||
if (supplier instanceof LazySupplier) { | ||
return supplier; | ||
} | ||
return new LazySupplier<>(supplier); | ||
} | ||
|
||
@Override | ||
public T get() { | ||
if (instance == null) { | ||
synchronized (this) { | ||
if (instance == null) { | ||
instance = supplier.get(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
} |
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.