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 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
34
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
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 }