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