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