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