001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.Collections;
026import java.util.Map;
027
028import org.eclipse.aether.RepositoryEvent;
029import org.eclipse.aether.RepositoryListener;
030import org.eclipse.aether.impl.RepositoryEventDispatcher;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import static java.util.Objects.requireNonNull;
035
036/**
037 */
038@Singleton
039@Named
040public class DefaultRepositoryEventDispatcher implements RepositoryEventDispatcher {
041
042    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryEventDispatcher.class);
043
044    private final Map<String, RepositoryListener> listeners;
045
046    @Inject
047    public DefaultRepositoryEventDispatcher(Map<String, RepositoryListener> listeners) {
048        this.listeners = Collections.unmodifiableMap(listeners);
049    }
050
051    @Override
052    public void dispatch(RepositoryEvent event) {
053        requireNonNull(event, "event cannot be null");
054        if (!listeners.isEmpty()) {
055            for (RepositoryListener listener : listeners.values()) {
056                dispatch(event, listener);
057            }
058        }
059
060        RepositoryListener listener = event.getSession().getRepositoryListener();
061
062        if (listener != null) {
063            dispatch(event, listener);
064        }
065    }
066
067    private void dispatch(RepositoryEvent event, RepositoryListener listener) {
068        try {
069            switch (event.getType()) {
070                case ARTIFACT_DEPLOYED:
071                    listener.artifactDeployed(event);
072                    break;
073                case ARTIFACT_DEPLOYING:
074                    listener.artifactDeploying(event);
075                    break;
076                case ARTIFACT_DESCRIPTOR_INVALID:
077                    listener.artifactDescriptorInvalid(event);
078                    break;
079                case ARTIFACT_DESCRIPTOR_MISSING:
080                    listener.artifactDescriptorMissing(event);
081                    break;
082                case ARTIFACT_DOWNLOADED:
083                    listener.artifactDownloaded(event);
084                    break;
085                case ARTIFACT_DOWNLOADING:
086                    listener.artifactDownloading(event);
087                    break;
088                case ARTIFACT_INSTALLED:
089                    listener.artifactInstalled(event);
090                    break;
091                case ARTIFACT_INSTALLING:
092                    listener.artifactInstalling(event);
093                    break;
094                case ARTIFACT_RESOLVED:
095                    listener.artifactResolved(event);
096                    break;
097                case ARTIFACT_RESOLVING:
098                    listener.artifactResolving(event);
099                    break;
100                case METADATA_DEPLOYED:
101                    listener.metadataDeployed(event);
102                    break;
103                case METADATA_DEPLOYING:
104                    listener.metadataDeploying(event);
105                    break;
106                case METADATA_DOWNLOADED:
107                    listener.metadataDownloaded(event);
108                    break;
109                case METADATA_DOWNLOADING:
110                    listener.metadataDownloading(event);
111                    break;
112                case METADATA_INSTALLED:
113                    listener.metadataInstalled(event);
114                    break;
115                case METADATA_INSTALLING:
116                    listener.metadataInstalling(event);
117                    break;
118                case METADATA_INVALID:
119                    listener.metadataInvalid(event);
120                    break;
121                case METADATA_RESOLVED:
122                    listener.metadataResolved(event);
123                    break;
124                case METADATA_RESOLVING:
125                    listener.metadataResolving(event);
126                    break;
127                default:
128                    throw new IllegalStateException("unknown repository event type " + event.getType());
129            }
130        } catch (Exception | LinkageError e) {
131            logError(e, listener);
132        }
133    }
134
135    private void logError(Throwable e, Object listener) {
136        LOGGER.warn(
137                "Failed to dispatch repository event to {}", listener.getClass().getCanonicalName(), e);
138    }
139}