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