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.execution.scope.internal;
20  
21  import java.util.Collection;
22  import java.util.IdentityHashMap;
23  
24  import com.google.inject.Key;
25  import com.google.inject.Provider;
26  import com.google.inject.Scope;
27  import com.google.inject.name.Named;
28  import org.apache.maven.execution.MojoExecutionEvent;
29  import org.apache.maven.execution.MojoExecutionListener;
30  import org.apache.maven.execution.scope.WeakMojoExecutionListener;
31  import org.apache.maven.plugin.MojoExecutionException;
32  
33  /**
34   * MojoExecutionScope
35   */
36  public class MojoExecutionScope extends org.apache.maven.internal.impl.di.MojoExecutionScope
37          implements Scope, MojoExecutionListener {
38  
39      public <T> void seed(Class<T> clazz, Provider<T> value) {
40          getScopeState().seed(clazz, value::get);
41      }
42  
43      public <T> Provider<T> scope(final Key<T> key, Provider<T> unscoped) {
44          Object qualifier = key.getAnnotation() instanceof Named n ? n.value() : key.getAnnotation();
45          org.apache.maven.di.Key<T> k =
46                  org.apache.maven.di.Key.ofType(key.getTypeLiteral().getType(), qualifier);
47          return scope(k, unscoped::get)::get;
48      }
49  
50      public static <T> Provider<T> seededKeyProvider(Class<? extends T> clazz) {
51          return MojoExecutionScope.<T>seededKeySupplier(clazz)::get;
52      }
53  
54      public void beforeMojoExecution(MojoExecutionEvent event) throws MojoExecutionException {
55          for (WeakMojoExecutionListener provided : getProvidedListeners()) {
56              provided.beforeMojoExecution(event);
57          }
58      }
59  
60      public void afterMojoExecutionSuccess(MojoExecutionEvent event) throws MojoExecutionException {
61          for (WeakMojoExecutionListener provided : getProvidedListeners()) {
62              provided.afterMojoExecutionSuccess(event);
63          }
64      }
65  
66      public void afterExecutionFailure(MojoExecutionEvent event) {
67          for (WeakMojoExecutionListener provided : getProvidedListeners()) {
68              provided.afterExecutionFailure(event);
69          }
70      }
71  
72      private Collection<WeakMojoExecutionListener> getProvidedListeners() {
73          // the same instance can be provided multiple times under different Key's
74          // deduplicate instances to avoid redundant beforeXXX/afterXXX callbacks
75          IdentityHashMap<WeakMojoExecutionListener, Object> listeners = new IdentityHashMap<>();
76          for (Object provided : getScopeState().provided()) {
77              if (provided instanceof WeakMojoExecutionListener) {
78                  listeners.put((WeakMojoExecutionListener) provided, null);
79              }
80          }
81          return listeners.keySet();
82      }
83  }