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  import javax.inject.Inject;
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  import org.apache.maven.eventspy.EventSpy;
27  import org.apache.maven.execution.ExecutionListener;
28  import org.eclipse.aether.RepositoryListener;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Dispatches callbacks to all registered EventSpies.
34   * @since 3.0.2
35   */
36  @Named
37  @Singleton
38  public class EventSpyDispatcher {
39      private final Logger logger = LoggerFactory.getLogger(getClass());
40  
41      private final List<EventSpy> eventSpies;
42  
43      @Inject
44      public EventSpyDispatcher(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 ExecutionListener chainListener(ExecutionListener listener) {
50          if (eventSpies.isEmpty()) {
51              return listener;
52          }
53          return new EventSpyExecutionListener(this, listener);
54      }
55  
56      public RepositoryListener chainListener(RepositoryListener listener) {
57          if (eventSpies.isEmpty()) {
58              return listener;
59          }
60          return new EventSpyRepositoryListener(this, listener);
61      }
62  
63      public void init(EventSpy.Context context) {
64          if (eventSpies.isEmpty()) {
65              return;
66          }
67          for (EventSpy eventSpy : eventSpies) {
68              try {
69                  eventSpy.init(context);
70              } catch (Exception | LinkageError e) {
71                  logError("initialize", e, eventSpy);
72              }
73          }
74      }
75  
76      public void onEvent(Object event) {
77          if (eventSpies.isEmpty()) {
78              return;
79          }
80          for (EventSpy eventSpy : eventSpies) {
81              try {
82                  eventSpy.onEvent(event);
83              } catch (Exception | LinkageError e) {
84                  logError("notify", e, eventSpy);
85              }
86          }
87      }
88  
89      public void close() {
90          if (eventSpies.isEmpty()) {
91              return;
92          }
93          for (EventSpy eventSpy : eventSpies) {
94              try {
95                  eventSpy.close();
96              } catch (Exception | LinkageError e) {
97                  logError("close", e, eventSpy);
98              }
99          }
100     }
101 
102     private void logError(String action, Throwable e, EventSpy spy) {
103         String msg = "Failed to " + action + " spy " + spy.getClass().getName() + ": " + e.getMessage();
104 
105         if (logger.isDebugEnabled()) {
106             logger.warn(msg, e);
107         } else {
108             logger.warn(msg);
109         }
110     }
111 }