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