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.buildcache;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.ConcurrentMap;
27  
28  import org.apache.maven.execution.MojoExecutionEvent;
29  import org.apache.maven.execution.MojoExecutionListener;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.project.MavenProject;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * MojoParametersListener
37   */
38  @Singleton
39  @Named
40  public class MojoParametersListener implements MojoExecutionListener {
41  
42      private static final Logger LOGGER = LoggerFactory.getLogger(MojoParametersListener.class);
43  
44      @SuppressWarnings("checkstyle:LineLength")
45      private final ConcurrentMap<MavenProject, Map<String, MojoExecutionEvent>> projectExecutions =
46              new ConcurrentHashMap<>();
47  
48      @Override
49      public void beforeMojoExecution(MojoExecutionEvent event) {
50          final String executionKey = CacheUtils.mojoExecutionKey(event.getExecution());
51          LOGGER.debug(
52                  "Starting mojo execution: {}, class: {}",
53                  executionKey,
54                  event.getMojo().getClass());
55          final MavenProject project = event.getProject();
56          Map<String, MojoExecutionEvent> projectEvents = projectExecutions.get(project);
57          if (projectEvents == null) {
58              Map<String, MojoExecutionEvent> candidate = new ConcurrentHashMap<>();
59              projectEvents = projectExecutions.putIfAbsent(project, candidate);
60              if (projectEvents == null) {
61                  projectEvents = candidate;
62              }
63          }
64          projectEvents.put(executionKey, event);
65      }
66  
67      @Override
68      public void afterMojoExecutionSuccess(MojoExecutionEvent event) throws MojoExecutionException {
69          // do nothing
70      }
71  
72      @Override
73      public void afterExecutionFailure(MojoExecutionEvent event) {
74          // do nothing
75      }
76  
77      public Map<String, MojoExecutionEvent> getProjectExecutions(MavenProject project) {
78          return projectExecutions.get(project);
79      }
80  }