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