1 package org.apache.maven.eventspy.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.eventspy.EventSpy;
26 import org.apache.maven.execution.ExecutionListener;
27 import org.codehaus.plexus.component.annotations.Component;
28 import org.codehaus.plexus.component.annotations.Requirement;
29 import org.codehaus.plexus.logging.Logger;
30 import org.eclipse.aether.RepositoryListener;
31
32
33
34
35
36 @Component( role = EventSpyDispatcher.class )
37 public class EventSpyDispatcher
38 {
39
40 @Requirement
41 private Logger logger;
42
43 @Requirement( role = EventSpy.class )
44 private List<EventSpy> eventSpies;
45
46 public void setEventSpies( List<EventSpy> eventSpies )
47 {
48
49 this.eventSpies = new ArrayList<>( eventSpies );
50 }
51
52 public List<EventSpy> getEventSpies()
53 {
54 return eventSpies;
55 }
56
57 public ExecutionListener chainListener( ExecutionListener listener )
58 {
59 if ( eventSpies.isEmpty() )
60 {
61 return listener;
62 }
63 return new EventSpyExecutionListener( this, listener );
64 }
65
66 public RepositoryListener chainListener( RepositoryListener listener )
67 {
68 if ( eventSpies.isEmpty() )
69 {
70 return listener;
71 }
72 return new EventSpyRepositoryListener( this, listener );
73 }
74
75 public void init( EventSpy.Context context )
76 {
77 if ( eventSpies.isEmpty() )
78 {
79 return;
80 }
81 for ( EventSpy eventSpy : eventSpies )
82 {
83 try
84 {
85 eventSpy.init( context );
86 }
87 catch ( Exception | LinkageError e )
88 {
89 logError( "initialize", e, eventSpy );
90 }
91 }
92 }
93
94 public void onEvent( Object event )
95 {
96 if ( eventSpies.isEmpty() )
97 {
98 return;
99 }
100 for ( EventSpy eventSpy : eventSpies )
101 {
102 try
103 {
104 eventSpy.onEvent( event );
105 }
106 catch ( Exception | LinkageError e )
107 {
108 logError( "notify", e, eventSpy );
109 }
110 }
111 }
112
113 public void close()
114 {
115 if ( eventSpies.isEmpty() )
116 {
117 return;
118 }
119 for ( EventSpy eventSpy : eventSpies )
120 {
121 try
122 {
123 eventSpy.close();
124 }
125 catch ( Exception | LinkageError e )
126 {
127 logError( "close", e, eventSpy );
128 }
129 }
130 }
131
132 private void logError( String action, Throwable e, EventSpy spy )
133 {
134 String msg = "Failed to " + action + " spy " + spy.getClass().getName() + ": " + e.getMessage();
135
136 if ( logger.isDebugEnabled() )
137 {
138 logger.warn( msg, e );
139 }
140 else
141 {
142 logger.warn( msg );
143 }
144 }
145
146 }