1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
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
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 }