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