View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Container capsule backed by Plexus Container.
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  }