-
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
232 additions
and
133 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/org/jboss/logmanager/configuration/ConfigurationResource.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,65 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* A configuration resource | ||
* | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
class ConfigurationResource<T> implements Supplier<T>, AutoCloseable { | ||
private final Supplier<T> supplier; | ||
private volatile T instance; | ||
|
||
private ConfigurationResource(final Supplier<T> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
static <T> ConfigurationResource<T> of(final Supplier<T> supplier) { | ||
if (supplier instanceof ConfigurationResource) { | ||
return (ConfigurationResource<T>) supplier; | ||
} | ||
return new ConfigurationResource<>(supplier); | ||
} | ||
|
||
@Override | ||
public T get() { | ||
if (instance == null) { | ||
synchronized (this) { | ||
if (instance == null) { | ||
instance = supplier.get(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
synchronized (this) { | ||
if (instance instanceof AutoCloseable) { | ||
((AutoCloseable) instance).close(); | ||
} | ||
instance = null; | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -19,16 +19,19 @@ | |
|
||
package org.jboss.logmanager.configuration; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
import java.util.logging.ErrorManager; | ||
import java.util.logging.Filter; | ||
import java.util.logging.Formatter; | ||
import java.util.logging.Handler; | ||
|
||
import org.jboss.logmanager.LogContext; | ||
import org.jboss.logmanager.Logger; | ||
|
||
/** | ||
|
@@ -47,25 +50,67 @@ | |
* @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 Map<String, Supplier<ErrorManager>> errorManagers; | ||
private final Map<String, Supplier<Filter>> filters; | ||
private final Map<String, Supplier<Formatter>> formatters; | ||
private final Map<String, Supplier<Handler>> handlers; | ||
private final Map<String, Supplier<Object>> objects; | ||
private final LogContext context; | ||
private final Map<String, ConfigurationResource<ErrorManager>> errorManagers; | ||
private final Map<String, ConfigurationResource<Filter>> filters; | ||
private final Map<String, ConfigurationResource<Formatter>> formatters; | ||
private final Map<String, ConfigurationResource<Handler>> handlers; | ||
private final Map<String, ConfigurationResource<Object>> objects; | ||
|
||
/** | ||
* Creates a new context configuration. | ||
*/ | ||
public ContextConfiguration() { | ||
public ContextConfiguration(final LogContext context) { | ||
this.context = context; | ||
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; | ||
} | ||
|
||
/** | ||
* 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 getContext().getLoggerIfExists(Objects.requireNonNull(name, "The name cannot be null")) != 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 getContext().getLogger(Objects.requireNonNull(name, "The name cannot be null")); | ||
} | ||
|
||
/** | ||
* Returns an unmodifiable set of the configured logger names | ||
* | ||
* @return an unmodified set of the logger names | ||
*/ | ||
public Set<String> getLoggers() { | ||
return Set.copyOf(Collections.list(getContext().getLoggerNames())); | ||
} | ||
|
||
/** | ||
* Adds an error manager to the context configuration. | ||
* | ||
|
@@ -79,7 +124,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)); | ||
ConfigurationResource.of(errorManager)); | ||
} | ||
|
||
/** | ||
|
@@ -140,7 +185,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)); | ||
ConfigurationResource.of(handler)); | ||
} | ||
|
||
/** | ||
|
@@ -201,7 +246,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)); | ||
ConfigurationResource.of(formatter)); | ||
} | ||
|
||
/** | ||
|
@@ -262,7 +307,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)); | ||
ConfigurationResource.of(filter)); | ||
} | ||
|
||
/** | ||
|
@@ -324,7 +369,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)); | ||
ConfigurationResource.of(object)); | ||
} | ||
|
||
/** | ||
|
@@ -372,28 +417,30 @@ 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 void close() throws Exception { | ||
context.close(); | ||
// TODO (jrp) these are not really thread safe | ||
closeResources(handlers.values()); | ||
handlers.clear(); | ||
closeResources(filters.values()); | ||
filters.clear(); | ||
closeResources(formatters.values()); | ||
formatters.clear(); | ||
closeResources(errorManagers.values()); | ||
errorManagers.clear(); | ||
closeResources(objects.values()); | ||
objects.clear(); | ||
} | ||
|
||
@Override | ||
public T get() { | ||
if (instance == null) { | ||
synchronized (this) { | ||
if (instance == null) { | ||
instance = supplier.get(); | ||
} | ||
} | ||
private static void closeResources(final Collection<? extends AutoCloseable> closeables) { | ||
for (var closeable : closeables) { | ||
try { | ||
closeable.close(); | ||
} catch (Throwable ignore) { | ||
// do nothing | ||
} | ||
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
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.