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<>( 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 | LinkageError e ) 088 { 089 logError( "initialize", e, eventSpy ); 090 } 091 } 092 } 093 094 public void onEvent( Object event ) 095 { 096 if ( eventSpies.isEmpty() ) 097 { 098 return; 099 } 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}