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