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<EventSpy>( 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 e )
88 {
89 logError( "initialize", e, eventSpy );
90 }
91 catch ( LinkageError e )
92 {
93 logError( "initialize", e, eventSpy );
94 }
95 }
96 }
97
98 public void onEvent( Object event )
99 {
100 if ( eventSpies.isEmpty() )
101 {
102 return;
103 }
104 for ( EventSpy eventSpy : eventSpies )
105 {
106 try
107 {
108 eventSpy.onEvent( event );
109 }
110 catch ( Exception e )
111 {
112 logError( "notify", e, eventSpy );
113 }
114 catch ( LinkageError e )
115 {
116 logError( "notify", e, eventSpy );
117 }
118 }
119 }
120
121 public void close()
122 {
123 if ( eventSpies.isEmpty() )
124 {
125 return;
126 }
127 for ( EventSpy eventSpy : eventSpies )
128 {
129 try
130 {
131 eventSpy.close();
132 }
133 catch ( Exception e )
134 {
135 logError( "close", e, eventSpy );
136 }
137 catch ( LinkageError e )
138 {
139 logError( "close", e, eventSpy );
140 }
141 }
142 }
143
144 private void logError( String action, Throwable e, EventSpy spy )
145 {
146 String msg = "Failed to " + action + " spy " + spy.getClass().getName() + ": " + e.getMessage();
147
148 if ( logger.isDebugEnabled() )
149 {
150 logger.warn( msg, e );
151 }
152 else
153 {
154 logger.warn( msg );
155 }
156 }
157
158 }