Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework the configuration API slightly #366

Merged
merged 6 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/main/java/org/jboss/logmanager/ExtHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.Flushable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Permission;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
Expand Down Expand Up @@ -51,13 +52,13 @@ public abstract class ExtHandler extends Handler implements AutoCloseable, Flush
private volatile Filter filter;
private volatile Formatter formatter;
private volatile Level level = Level.ALL;
private volatile ErrorManager errorManager = new ErrorManager();
private volatile ErrorManager errorManager;
// (skip `encoding` because we replace it with `charset` below)

private volatile boolean autoFlush = true;
private volatile boolean enabled = true;
private volatile boolean closeChildren;
private volatile Charset charset = Charset.defaultCharset();
private volatile Charset charset = StandardCharsets.UTF_8;

/**
* The sub-handlers for this handler. May only be updated using the {@link #handlersUpdater} atomic updater. The array
Expand All @@ -78,7 +79,7 @@ public abstract class ExtHandler extends Handler implements AutoCloseable, Flush
protected ExtHandler() {
handlersUpdater.clear(this);
closeChildren = true;
super.setErrorManager(DEFAULT_ERROR_MANAGER);
errorManager = DEFAULT_ERROR_MANAGER;
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -388,7 +389,7 @@ public Filter getFilter() {
*/
@Override
public void setEncoding(final String encoding) throws SecurityException, UnsupportedEncodingException {
if (encoding != null)
if (encoding != null) {
try {
setCharset(Charset.forName(encoding));
} catch (IllegalArgumentException e) {
Expand All @@ -397,6 +398,9 @@ public void setEncoding(final String encoding) throws SecurityException, Unsuppo
e2.initCause(e);
throw e2;
}
} else {
setCharset(StandardCharsets.UTF_8);
}
}

/**
Expand Down Expand Up @@ -522,7 +526,8 @@ public boolean isCallerCalculationRequired() {

@Override
protected void reportError(String msg, Exception ex, int code) {
super.reportError(msg, ex, code);
final ErrorManager errorManager = this.errorManager;
errorManager.error(msg, ex, code);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/jboss/logmanager/LogContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,20 @@ public void close() throws Exception {
for (AutoCloseable handler : closeHandlers) {
handler.close();
}
synchronized (this) {
attachmentKey1 = null;
attachmentKey2 = null;
final var value1 = attachmentValue1;
attachmentValue1 = null;
if (value1 instanceof AutoCloseable) {
((AutoCloseable) value1).close();
}
final var value2 = attachmentValue2;
attachmentValue2 = null;
if (value2 instanceof AutoCloseable) {
((AutoCloseable) value2).close();
}
}
} finally {
treeLock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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;

/**
* Represents a configuration resource. If the resource is a {@link AutoCloseable}, then invoking {@link #close()} on
* this resource will close the resource.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public interface ConfigurationResource<T> extends Supplier<T>, AutoCloseable {

/**
* Creates a configuration resource which lazily invokes the supplier. Note that {@link #close()} will only close
* the resource if {@link #get()} was first invoked to retrieve the value from the supplier.
*
* @param supplier the supplier used to create the configuration resource
* @return the configuration resource represented by a lazy instance
*/
static <T> ConfigurationResource<T> of(final Supplier<T> supplier) {
if (supplier instanceof ConfigurationResource) {
return (ConfigurationResource<T>) supplier;
}
return new LazyConfigurationResource<>(supplier);
}

/**
* Creates a configuration resource with the instance as a constant. Note that if {@link #close()} is invoked,
* {@link #get()} will return {@code null}.
*
* @param instance the constant instance
* @return the configuration resource represented by a constant instance
*/
static <T> ConfigurationResource<T> of(final T instance) {
return new ConstantConfigurationResource<>(instance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 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.concurrent.atomic.AtomicReference;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class ConstantConfigurationResource<T> implements ConfigurationResource<T> {
private AtomicReference<T> instance;

ConstantConfigurationResource(final T instance) {
this.instance = new AtomicReference<>(instance);
}

@Override
public T get() {
return instance.get();
}

@Override
public void close() throws Exception {
final T instance = this.instance.getAndSet(null);
if (instance instanceof AutoCloseable) {
((AutoCloseable) instance).close();
}
}
}
Loading