001package org.apache.maven.eventspy.internal;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.maven.eventspy.EventSpy;
026import org.apache.maven.execution.ExecutionListener;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029import org.codehaus.plexus.logging.Logger;
030import org.eclipse.aether.RepositoryListener;
031
032/**
033 * Dispatches callbacks to all registered eventspies.
034 * @since 3.0.2
035 */
036@Component( role = EventSpyDispatcher.class )
037public class EventSpyDispatcher
038{
039
040    @Requirement
041    private Logger logger;
042
043    @Requirement( role = EventSpy.class )
044    private List<EventSpy> eventSpies;
045
046    public void setEventSpies( List<EventSpy> eventSpies )
047    {
048        // make copy to get rid of needless overhead for dynamic lookups
049        this.eventSpies = new ArrayList<EventSpy>( eventSpies );
050    }
051
052    public List<EventSpy> getEventSpies()
053    {
054        return eventSpies;
055    }
056
057    public ExecutionListener chainListener( ExecutionListener listener )
058    {
059        if ( eventSpies.isEmpty() )
060        {
061            return listener;
062        }
063        return new EventSpyExecutionListener( this, listener );
064    }
065
066    public RepositoryListener chainListener( RepositoryListener listener )
067    {
068        if ( eventSpies.isEmpty() )
069        {
070            return listener;
071        }
072        return new EventSpyRepositoryListener( this, listener );
073    }
074
075    public void init( EventSpy.Context context )
076    {
077        if ( eventSpies.isEmpty() )
078        {
079            return;
080        }
081        for ( EventSpy eventSpy : eventSpies )
082        {
083            try
084            {
085                eventSpy.init( context );
086            }
087            catch ( Exception e )
088            {
089                logError( "initialize", e, eventSpy );
090            }
091            catch ( LinkageError e )
092            {
093                logError( "initialize", e, eventSpy );
094            }
095        }
096    }
097
098    public void onEvent( Object event )
099    {
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}