001 package 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 022 import java.util.ArrayList; 023 import java.util.List; 024 025 import org.apache.maven.eventspy.EventSpy; 026 import org.apache.maven.execution.ExecutionListener; 027 import org.codehaus.plexus.component.annotations.Component; 028 import org.codehaus.plexus.component.annotations.Requirement; 029 import org.codehaus.plexus.logging.Logger; 030 import org.sonatype.aether.RepositoryListener; 031 032 /** 033 * Dispatches callbacks to all registered eventspies. 034 */ 035 @Component( role = EventSpyDispatcher.class ) 036 public class EventSpyDispatcher 037 { 038 039 @Requirement 040 private Logger logger; 041 042 @Requirement( role = EventSpy.class ) 043 private List<EventSpy> eventSpies; 044 045 public void setEventSpies( List<EventSpy> eventSpies ) 046 { 047 // make copy to get rid of needless overhead for dynamic lookups 048 this.eventSpies = new ArrayList<EventSpy>( eventSpies ); 049 } 050 051 public List<EventSpy> getEventSpies() 052 { 053 return eventSpies; 054 } 055 056 public ExecutionListener chainListener( ExecutionListener listener ) 057 { 058 if ( eventSpies.isEmpty() ) 059 { 060 return listener; 061 } 062 return new EventSpyExecutionListener( this, listener ); 063 } 064 065 public RepositoryListener chainListener( RepositoryListener listener ) 066 { 067 if ( eventSpies.isEmpty() ) 068 { 069 return listener; 070 } 071 return new EventSpyRepositoryListener( this, listener ); 072 } 073 074 public void init( EventSpy.Context context ) 075 { 076 if ( eventSpies.isEmpty() ) 077 { 078 return; 079 } 080 for ( EventSpy eventSpy : eventSpies ) 081 { 082 try 083 { 084 eventSpy.init( context ); 085 } 086 catch ( Exception e ) 087 { 088 logError( "initialize", e, eventSpy ); 089 } 090 catch ( LinkageError e ) 091 { 092 logError( "initialize", e, eventSpy ); 093 } 094 } 095 } 096 097 public void onEvent( Object event ) 098 { 099 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 }