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 javax.inject.Inject;
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  
29  import org.apache.maven.eventspy.EventSpy;
30  import org.apache.maven.execution.ExecutionListener;
31  import org.eclipse.aether.RepositoryListener;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Dispatches callbacks to all registered EventSpies.
37   * @since 3.0.2
38   */
39  @Named
40  @Singleton
41  public class EventSpyDispatcher
42  {
43      private final Logger logger = LoggerFactory.getLogger( getClass() );
44  
45      private final List<EventSpy> eventSpies;
46  
47      @Inject
48      public EventSpyDispatcher( List<EventSpy> eventSpies )
49      {
50          // make copy to get rid of needless overhead for dynamic lookups
51          this.eventSpies = new ArrayList<>( eventSpies );
52      }
53  
54      public ExecutionListener chainListener( ExecutionListener listener )
55      {
56          if ( eventSpies.isEmpty() )
57          {
58              return listener;
59          }
60          return new EventSpyExecutionListener( this, listener );
61      }
62  
63      public RepositoryListener chainListener( RepositoryListener listener )
64      {
65          if ( eventSpies.isEmpty() )
66          {
67              return listener;
68          }
69          return new EventSpyRepositoryListener( this, listener );
70      }
71  
72      public void init( EventSpy.Context context )
73      {
74          if ( eventSpies.isEmpty() )
75          {
76              return;
77          }
78          for ( EventSpy eventSpy : eventSpies )
79          {
80              try
81              {
82                  eventSpy.init( context );
83              }
84              catch ( Exception | LinkageError e )
85              {
86                  logError( "initialize", e, eventSpy );
87              }
88          }
89      }
90  
91      public void onEvent( Object event )
92      {
93          if ( eventSpies.isEmpty() )
94          {
95              return;
96          }
97          for ( EventSpy eventSpy : eventSpies )
98          {
99              try
100             {
101                 eventSpy.onEvent( event );
102             }
103             catch ( Exception | LinkageError e )
104             {
105                 logError( "notify", e, eventSpy );
106             }
107         }
108     }
109 
110     public void close()
111     {
112         if ( eventSpies.isEmpty() )
113         {
114             return;
115         }
116         for ( EventSpy eventSpy : eventSpies )
117         {
118             try
119             {
120                 eventSpy.close();
121             }
122             catch ( Exception | LinkageError e )
123             {
124                 logError( "close", e, eventSpy );
125             }
126         }
127     }
128 
129     private void logError( String action, Throwable e, EventSpy spy )
130     {
131         String msg = "Failed to " + action + " spy " + spy.getClass().getName() + ": " + e.getMessage();
132 
133         if ( logger.isDebugEnabled() )
134         {
135             logger.warn( msg, e );
136         }
137         else
138         {
139             logger.warn( msg );
140         }
141     }
142 
143 }