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.mvn.resident;
20  
21  import java.util.ArrayList;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import org.apache.maven.api.cli.InvokerException;
25  import org.apache.maven.api.cli.InvokerRequest;
26  import org.apache.maven.cling.invoker.ProtoLookup;
27  import org.apache.maven.cling.invoker.mvn.MavenInvoker;
28  
29  /**
30   * Resident invoker implementation, similar to "local", but keeps Maven instance resident. This implies, that
31   * things like environment, system properties, extensions etc. are loaded only once. It is caller duty to ensure
32   * that subsequent call is right for the resident instance (ie no env change or different extension needed).
33   */
34  public class ResidentMavenInvoker extends MavenInvoker<ResidentMavenContext> {
35  
36      private final ConcurrentHashMap<String, ResidentMavenContext> residentContext;
37  
38      public ResidentMavenInvoker(ProtoLookup protoLookup) {
39          super(protoLookup);
40          this.residentContext = new ConcurrentHashMap<>();
41      }
42  
43      @Override
44      public void close() throws InvokerException {
45          ArrayList<InvokerException> exceptions = new ArrayList<>();
46          for (ResidentMavenContext context : residentContext.values()) {
47              try {
48                  context.shutDown();
49              } catch (InvokerException e) {
50                  exceptions.add(e);
51              }
52          }
53          if (!exceptions.isEmpty()) {
54              InvokerException exception = new InvokerException("Could not cleanly shut down context pool");
55              exceptions.forEach(exception::addSuppressed);
56              throw exception;
57          }
58      }
59  
60      @Override
61      protected ResidentMavenContext createContext(InvokerRequest invokerRequest) {
62          return residentContext
63                  .computeIfAbsent(getContextId(invokerRequest), k -> new ResidentMavenContext(invokerRequest))
64                  .copy(invokerRequest);
65      }
66  
67      protected String getContextId(InvokerRequest invokerRequest) {
68          // TODO: in a moment Maven stop pushing user properties to system properties (and maybe something more)
69          // and allow multiple instances per JVM, this may become a pool?
70          return "resident";
71      }
72  
73      @Override
74      protected void container(ResidentMavenContext context) throws Exception {
75          if (context.containerCapsule == null) {
76              super.container(context);
77          } else {
78              context.containerCapsule.updateLogging(context);
79          }
80      }
81  
82      @Override
83      protected void lookup(ResidentMavenContext context) throws Exception {
84          if (context.maven == null) {
85              super.lookup(context);
86          }
87      }
88  }