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.sonatype.aether.RepositoryListener;
31  
32  /**
33   * Dispatches callbacks to all registered eventspies.
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          // make copy to get rid of needless overhead for dynamic lookups
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 }