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 javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.inject.Singleton;
28
29 import org.apache.maven.eventspy.EventSpy;
30 import org.apache.maven.execution.ExecutionListener;
31 import org.eclipse.aether.RepositoryListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39 @Named
40 @Singleton
41 public class EventSpyDispatcher
42 {
43 private final Logger logger = LoggerFactory.getLogger( getClass() );
44
45 private final List<EventSpy> eventSpies;
46
47 @Inject
48 public EventSpyDispatcher( List<EventSpy> eventSpies )
49 {
50
51 this.eventSpies = new ArrayList<>( eventSpies );
52 }
53
54 public ExecutionListener chainListener( ExecutionListener listener )
55 {
56 if ( eventSpies.isEmpty() )
57 {
58 return listener;
59 }
60 return new EventSpyExecutionListener( this, listener );
61 }
62
63 public RepositoryListener chainListener( RepositoryListener listener )
64 {
65 if ( eventSpies.isEmpty() )
66 {
67 return listener;
68 }
69 return new EventSpyRepositoryListener( this, listener );
70 }
71
72 public void init( EventSpy.Context context )
73 {
74 if ( eventSpies.isEmpty() )
75 {
76 return;
77 }
78 for ( EventSpy eventSpy : eventSpies )
79 {
80 try
81 {
82 eventSpy.init( context );
83 }
84 catch ( Exception | LinkageError e )
85 {
86 logError( "initialize", e, eventSpy );
87 }
88 }
89 }
90
91 public void onEvent( Object event )
92 {
93 if ( eventSpies.isEmpty() )
94 {
95 return;
96 }
97 for ( EventSpy eventSpy : eventSpies )
98 {
99 try
100 {
101 eventSpy.onEvent( event );
102 }
103 catch ( Exception | LinkageError e )
104 {
105 logError( "notify", e, eventSpy );
106 }
107 }
108 }
109
110 public void close()
111 {
112 if ( eventSpies.isEmpty() )
113 {
114 return;
115 }
116 for ( EventSpy eventSpy : eventSpies )
117 {
118 try
119 {
120 eventSpy.close();
121 }
122 catch ( Exception | LinkageError e )
123 {
124 logError( "close", e, eventSpy );
125 }
126 }
127 }
128
129 private void logError( String action, Throwable e, EventSpy spy )
130 {
131 String msg = "Failed to " + action + " spy " + spy.getClass().getName() + ": " + e.getMessage();
132
133 if ( logger.isDebugEnabled() )
134 {
135 logger.warn( msg, e );
136 }
137 else
138 {
139 logger.warn( msg );
140 }
141 }
142
143 }