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