View Javadoc
1   package org.apache.maven.eventspy.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Dispatches callbacks to all registered eventspies.
34   * @since 3.0.2
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          // make copy to get rid of needless overhead for dynamic lookups
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 }