001package org.eclipse.aether.internal.impl;
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.Collection;
024import static java.util.Objects.requireNonNull;
025import java.util.Set;
026
027import javax.inject.Inject;
028import javax.inject.Named;
029import javax.inject.Singleton;
030
031import org.eclipse.aether.RepositoryEvent;
032import org.eclipse.aether.RepositoryListener;
033import org.eclipse.aether.impl.RepositoryEventDispatcher;
034import org.eclipse.aether.spi.locator.Service;
035import org.eclipse.aether.spi.locator.ServiceLocator;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 */
041@Singleton
042@Named
043public class DefaultRepositoryEventDispatcher
044    implements RepositoryEventDispatcher, Service
045{
046
047    private static final Logger LOGGER = LoggerFactory.getLogger( DefaultRepositoryEventDispatcher.class );
048
049    private Collection<RepositoryListener> listeners = new ArrayList<>();
050
051    public DefaultRepositoryEventDispatcher()
052    {
053        // enables no-arg constructor
054    }
055
056    @Inject
057    DefaultRepositoryEventDispatcher( Set<RepositoryListener> listeners )
058    {
059        setRepositoryListeners( listeners );
060    }
061
062    public DefaultRepositoryEventDispatcher addRepositoryListener( RepositoryListener listener )
063    {
064        this.listeners.add( requireNonNull( listener, "repository listener cannot be null" ) );
065        return this;
066    }
067
068    public DefaultRepositoryEventDispatcher setRepositoryListeners( Collection<RepositoryListener> listeners )
069    {
070        if ( listeners == null )
071        {
072            this.listeners = new ArrayList<>();
073        }
074        else
075        {
076            this.listeners = listeners;
077        }
078        return this;
079    }
080
081    public void initService( ServiceLocator locator )
082    {
083        setRepositoryListeners( locator.getServices( RepositoryListener.class ) );
084    }
085
086    public void dispatch( RepositoryEvent event )
087    {
088        requireNonNull( event, "event cannot be null" );
089        if ( !listeners.isEmpty() )
090        {
091            for ( RepositoryListener listener : listeners )
092            {
093                dispatch( event, listener );
094            }
095        }
096
097        RepositoryListener listener = event.getSession().getRepositoryListener();
098
099        if ( listener != null )
100        {
101            dispatch( event, listener );
102        }
103    }
104
105    private void dispatch( RepositoryEvent event, RepositoryListener listener )
106    {
107        try
108        {
109            switch ( event.getType() )
110            {
111                case ARTIFACT_DEPLOYED:
112                    listener.artifactDeployed( event );
113                    break;
114                case ARTIFACT_DEPLOYING:
115                    listener.artifactDeploying( event );
116                    break;
117                case ARTIFACT_DESCRIPTOR_INVALID:
118                    listener.artifactDescriptorInvalid( event );
119                    break;
120                case ARTIFACT_DESCRIPTOR_MISSING:
121                    listener.artifactDescriptorMissing( event );
122                    break;
123                case ARTIFACT_DOWNLOADED:
124                    listener.artifactDownloaded( event );
125                    break;
126                case ARTIFACT_DOWNLOADING:
127                    listener.artifactDownloading( event );
128                    break;
129                case ARTIFACT_INSTALLED:
130                    listener.artifactInstalled( event );
131                    break;
132                case ARTIFACT_INSTALLING:
133                    listener.artifactInstalling( event );
134                    break;
135                case ARTIFACT_RESOLVED:
136                    listener.artifactResolved( event );
137                    break;
138                case ARTIFACT_RESOLVING:
139                    listener.artifactResolving( event );
140                    break;
141                case METADATA_DEPLOYED:
142                    listener.metadataDeployed( event );
143                    break;
144                case METADATA_DEPLOYING:
145                    listener.metadataDeploying( event );
146                    break;
147                case METADATA_DOWNLOADED:
148                    listener.metadataDownloaded( event );
149                    break;
150                case METADATA_DOWNLOADING:
151                    listener.metadataDownloading( event );
152                    break;
153                case METADATA_INSTALLED:
154                    listener.metadataInstalled( event );
155                    break;
156                case METADATA_INSTALLING:
157                    listener.metadataInstalling( event );
158                    break;
159                case METADATA_INVALID:
160                    listener.metadataInvalid( event );
161                    break;
162                case METADATA_RESOLVED:
163                    listener.metadataResolved( event );
164                    break;
165                case METADATA_RESOLVING:
166                    listener.metadataResolving( event );
167                    break;
168                default:
169                    throw new IllegalStateException( "unknown repository event type " + event.getType() );
170            }
171        }
172        catch ( Exception | LinkageError e )
173        {
174            logError( e, listener );
175        }
176    }
177
178    private void logError( Throwable e, Object listener )
179    {
180        LOGGER.warn( "Failed to dispatch repository event to {}", listener.getClass().getCanonicalName(), e );
181    }
182
183}