1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling.invoker;
20
21 import java.util.Optional;
22
23 import org.apache.maven.api.services.Lookup;
24 import org.apache.maven.internal.impl.DefaultLookup;
25 import org.codehaus.plexus.DefaultPlexusContainer;
26
27 import static java.util.Objects.requireNonNull;
28 import static org.apache.maven.cling.invoker.Utils.toPlexusLoggingLevel;
29
30
31
32
33 public class PlexusContainerCapsule implements ContainerCapsule {
34 private final ClassLoader previousClassLoader;
35 private final DefaultPlexusContainer plexusContainer;
36 private final Lookup lookup;
37
38 public PlexusContainerCapsule(
39 LookupContext context, ClassLoader previousClassLoader, DefaultPlexusContainer plexusContainer) {
40 this.previousClassLoader = requireNonNull(previousClassLoader, "previousClassLoader");
41 this.plexusContainer = requireNonNull(plexusContainer, "plexusContainer");
42 this.lookup = new DefaultLookup(plexusContainer);
43 updateLogging(context);
44 }
45
46 @Override
47 public void updateLogging(LookupContext context) {
48 plexusContainer.getLoggerManager().setThresholds(toPlexusLoggingLevel(context.loggerLevel));
49 org.slf4j.Logger l = context.loggerFactory.getLogger(this.getClass().getName());
50 context.logger = (level, message, error) -> l.atLevel(org.slf4j.event.Level.valueOf(level.name()))
51 .setCause(error)
52 .log(message);
53 }
54
55 @Override
56 public Lookup getLookup() {
57 return lookup;
58 }
59
60 @Override
61 public Optional<ClassLoader> currentThreadClassLoader() {
62 return Optional.of(plexusContainer.getContainerRealm());
63 }
64
65 @Override
66 public void close() {
67 try {
68 plexusContainer.dispose();
69 } finally {
70 Thread.currentThread().setContextClassLoader(previousClassLoader);
71 }
72 }
73 }