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 com.google.inject.Key;
22  import com.google.inject.OutOfScopeException;
23  import com.google.inject.Provider;
24  import com.google.inject.Scope;
25  import com.google.inject.util.Providers;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.IdentityHashMap;
29  import java.util.LinkedList;
30  import java.util.Map;
31  import org.apache.maven.execution.MojoExecutionEvent;
32  import org.apache.maven.execution.MojoExecutionListener;
33  import org.apache.maven.execution.scope.WeakMojoExecutionListener;
34  import org.apache.maven.plugin.MojoExecutionException;
35  
36  /**
37   * MojoExecutionScope
38   */
39  public class MojoExecutionScope implements Scope, MojoExecutionListener {
40      private static final Provider<Object> SEEDED_KEY_PROVIDER = () -> {
41          throw new IllegalStateException();
42      };
43  
44      private static final class ScopeState {
45          private final Map<Key<?>, Provider<?>> seeded = new HashMap<>();
46  
47          private final Map<Key<?>, Object> provided = new HashMap<>();
48      }
49  
50      private final ThreadLocal<LinkedList<ScopeState>> values = new ThreadLocal<>();
51  
52      public MojoExecutionScope() {}
53  
54      public void enter() {
55          LinkedList<ScopeState> stack = values.get();
56          if (stack == null) {
57              stack = new LinkedList<>();
58              values.set(stack);
59          }
60          stack.addFirst(new ScopeState());
61      }
62  
63      private ScopeState getScopeState() {
64          LinkedList<ScopeState> stack = values.get();
65          if (stack == null || stack.isEmpty()) {
66              throw new IllegalStateException();
67          }
68          return stack.getFirst();
69      }
70  
71      public void exit() throws MojoExecutionException {
72          final LinkedList<ScopeState> stack = values.get();
73          if (stack == null || stack.isEmpty()) {
74              throw new IllegalStateException();
75          }
76          stack.removeFirst();
77          if (stack.isEmpty()) {
78              values.remove();
79          }
80      }
81  
82      public <T> void seed(Class<T> clazz, Provider<T> value) {
83          getScopeState().seeded.put(Key.get(clazz), value);
84      }
85  
86      public <T> void seed(Class<T> clazz, final T value) {
87          getScopeState().seeded.put(Key.get(clazz), Providers.of(value));
88      }
89  
90      public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) {
91          return () -> {
92              LinkedList<ScopeState> stack = values.get();
93              if (stack == null || stack.isEmpty()) {
94                  throw new OutOfScopeException("Cannot access " + key + " outside of a scoping block");
95              }
96  
97              ScopeState state = stack.getFirst();
98  
99              Provider<?> seeded = state.seeded.get(key);
100 
101             if (seeded != null) {
102                 return (T) seeded.get();
103             }
104 
105             T provided = (T) state.provided.get(key);
106             if (provided == null && unscoped != null) {
107                 provided = unscoped.get();
108                 state.provided.put(key, provided);
109             }
110 
111             return provided;
112         };
113     }
114 
115     @SuppressWarnings({"unchecked"})
116     public static <T> Provider<T> seededKeyProvider() {
117         return (Provider<T>) SEEDED_KEY_PROVIDER;
118     }
119 
120     public void beforeMojoExecution(MojoExecutionEvent event) throws MojoExecutionException {
121         for (WeakMojoExecutionListener provided : getProvidedListeners()) {
122             provided.beforeMojoExecution(event);
123         }
124     }
125 
126     public void afterMojoExecutionSuccess(MojoExecutionEvent event) throws MojoExecutionException {
127         for (WeakMojoExecutionListener provided : getProvidedListeners()) {
128             provided.afterMojoExecutionSuccess(event);
129         }
130     }
131 
132     public void afterExecutionFailure(MojoExecutionEvent event) {
133         for (WeakMojoExecutionListener provided : getProvidedListeners()) {
134             provided.afterExecutionFailure(event);
135         }
136     }
137 
138     private Collection<WeakMojoExecutionListener> getProvidedListeners() {
139         // the same instance can be provided multiple times under different Key's
140         // deduplicate instances to avoid redundant beforeXXX/afterXXX callbacks
141         IdentityHashMap<WeakMojoExecutionListener, Object> listeners = new IdentityHashMap<>();
142         for (Object provided : getScopeState().provided.values()) {
143             if (provided instanceof WeakMojoExecutionListener) {
144                 listeners.put((WeakMojoExecutionListener) provided, null);
145             }
146         }
147         return listeners.keySet();
148     }
149 }