CLIng+resident: logging subsystem still needs an update (#1931)

As part of capsule creation, capsule logging setup happened as well
but it got "stuck" on very first invocation and never updated.
This commit is contained in:
Tamas Cservenak 2024-11-21 14:38:51 +01:00 committed by GitHub
parent 986f59683e
commit a55accd518
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 11 deletions

View File

@ -28,6 +28,11 @@ import org.apache.maven.api.services.Lookup;
* Container capsule.
*/
public interface ContainerCapsule extends AutoCloseable {
/**
* Updates the existing capsule logging setup.
*/
void updateLogging(LookupContext context);
/**
* The {@link Lookup} service backed by container in this capsule.
*/

View File

@ -22,22 +22,34 @@ import java.util.Optional;
import org.apache.maven.api.services.Lookup;
import org.apache.maven.internal.impl.DefaultLookup;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.DefaultPlexusContainer;
import static java.util.Objects.requireNonNull;
import static org.apache.maven.cling.invoker.Utils.toPlexusLoggingLevel;
/**
* Container capsule backed by Plexus Container.
*/
public class PlexusContainerCapsule implements ContainerCapsule {
private final ClassLoader previousClassLoader;
private final PlexusContainer plexusContainer;
private final DefaultPlexusContainer plexusContainer;
private final Lookup lookup;
public PlexusContainerCapsule(ClassLoader previousClassLoader, PlexusContainer plexusContainer) {
public PlexusContainerCapsule(
LookupContext context, ClassLoader previousClassLoader, DefaultPlexusContainer plexusContainer) {
this.previousClassLoader = requireNonNull(previousClassLoader, "previousClassLoader");
this.plexusContainer = requireNonNull(plexusContainer, "plexusContainer");
this.lookup = new DefaultLookup(plexusContainer);
updateLogging(context);
}
@Override
public void updateLogging(LookupContext context) {
plexusContainer.getLoggerManager().setThresholds(toPlexusLoggingLevel(context.loggerLevel));
org.slf4j.Logger l = context.loggerFactory.getLogger(this.getClass().getName());
context.logger = (level, message, error) -> l.atLevel(org.slf4j.event.Level.valueOf(level.name()))
.setCause(error)
.log(message);
}
@Override

View File

@ -73,13 +73,13 @@ public class PlexusContainerCapsuleFactory<C extends LookupContext> implements C
public ContainerCapsule createContainerCapsule(LookupInvoker<C> invoker, C context) throws InvokerException {
try {
return new PlexusContainerCapsule(
Thread.currentThread().getContextClassLoader(), container(invoker, context));
context, Thread.currentThread().getContextClassLoader(), container(invoker, context));
} catch (Exception e) {
throw new InvokerException("Failed to create plexus container capsule", e);
}
}
protected PlexusContainer container(LookupInvoker<C> invoker, C context) throws Exception {
protected DefaultPlexusContainer container(LookupInvoker<C> invoker, C context) throws Exception {
ClassWorld classWorld = invoker.protoLookup.lookup(ClassWorld.class);
ClassRealm coreRealm = classWorld.getClassRealm("plexus.core");
List<Path> extClassPath = parseExtClasspath(context);
@ -139,12 +139,6 @@ public class PlexusContainerCapsuleFactory<C extends LookupContext> implements C
container.getLoggerManager().setThresholds(toPlexusLoggingLevel(context.loggerLevel));
customizeContainer(context, container);
// refresh logger in case container got customized by spy
org.slf4j.Logger l = context.loggerFactory.getLogger(this.getClass().getName());
context.logger = (level, message, error) -> l.atLevel(org.slf4j.event.Level.valueOf(level.name()))
.setCause(error)
.log(message);
return container;
}

View File

@ -74,6 +74,8 @@ public class ResidentMavenInvoker extends MavenInvoker<ResidentMavenContext> {
protected void container(ResidentMavenContext context) throws Exception {
if (context.containerCapsule == null) {
super.container(context);
} else {
context.containerCapsule.updateLogging(context);
}
}