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                  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 }