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.eventspy.internal;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.maven.eventspy.EventSpy;
25  import org.apache.maven.execution.ExecutionListener;
26  import org.codehaus.plexus.component.annotations.Component;
27  import org.codehaus.plexus.component.annotations.Requirement;
28  import org.codehaus.plexus.logging.Logger;
29  import org.eclipse.aether.RepositoryListener;
30  
31  /**
32   * Dispatches callbacks to all registered eventspies.
33   * @since 3.0.2
34   */
35  @Component(role = EventSpyDispatcher.class)
36  public class EventSpyDispatcher {
37  
38      @Requirement
39      private Logger logger;
40  
41      @Requirement(role = EventSpy.class)
42      private List<EventSpy> eventSpies;
43  
44      public void setEventSpies(List<EventSpy> eventSpies) {
45          // make copy to get rid of needless overhead for dynamic lookups
46          this.eventSpies = new ArrayList<>(eventSpies);
47      }
48  
49      public List<EventSpy> getEventSpies() {
50          return eventSpies;
51      }
52  
53      public ExecutionListener chainListener(ExecutionListener listener) {
54          if (eventSpies.isEmpty()) {
55              return listener;
56          }
57          return new EventSpyExecutionListener(this, listener);
58      }
59  
60      public RepositoryListener chainListener(RepositoryListener listener) {
61          if (eventSpies.isEmpty()) {
62              return listener;
63          }
64          return new EventSpyRepositoryListener(this, listener);
65      }
66  
67      public void init(EventSpy.Context context) {
68          if (eventSpies.isEmpty()) {
69              return;
70          }
71          for (EventSpy eventSpy : eventSpies) {
72              try {
73                  eventSpy.init(context);
74              } catch (Exception | LinkageError e) {
75                  logError("initialize", e, eventSpy);
76              }
77          }
78      }
79  
80      public void onEvent(Object event) {
81          if (eventSpies.isEmpty()) {
82              return;
83          }
84          for (EventSpy eventSpy : eventSpies) {
85              try {
86                  eventSpy.onEvent(event);
87              } catch (Exception | LinkageError e) {
88                  logError("notify", e, eventSpy);
89              }
90          }
91      }
92  
93      public void close() {
94          if (eventSpies.isEmpty()) {
95              return;
96          }
97          for (EventSpy eventSpy : eventSpies) {
98              try {
99                  eventSpy.close();
100             } catch (Exception | LinkageError e) {
101                 logError("close", e, eventSpy);
102             }
103         }
104     }
105 
106     private void logError(String action, Throwable e, EventSpy spy) {
107         String msg = "Failed to " + action + " spy " + spy.getClass().getName() + ": " + e.getMessage();
108 
109         if (logger.isDebugEnabled()) {
110             logger.warn(msg, e);
111         } else {
112             logger.warn(msg);
113         }
114     }
115 }