1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Set;
28
29 import org.eclipse.aether.RepositoryEvent;
30 import org.eclipse.aether.RepositoryListener;
31 import org.eclipse.aether.impl.RepositoryEventDispatcher;
32 import org.eclipse.aether.spi.locator.Service;
33 import org.eclipse.aether.spi.locator.ServiceLocator;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import static java.util.Objects.requireNonNull;
38
39
40
41 @Singleton
42 @Named
43 public class DefaultRepositoryEventDispatcher implements RepositoryEventDispatcher, Service {
44
45 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryEventDispatcher.class);
46
47 private Collection<RepositoryListener> listeners = new ArrayList<>();
48
49 @Deprecated
50 public DefaultRepositoryEventDispatcher() {
51
52 }
53
54 @Inject
55 public DefaultRepositoryEventDispatcher(Set<RepositoryListener> listeners) {
56 setRepositoryListeners(listeners);
57 }
58
59 public DefaultRepositoryEventDispatcher addRepositoryListener(RepositoryListener listener) {
60 this.listeners.add(requireNonNull(listener, "repository listener cannot be null"));
61 return this;
62 }
63
64 public DefaultRepositoryEventDispatcher setRepositoryListeners(Collection<RepositoryListener> listeners) {
65 if (listeners == null) {
66 this.listeners = new ArrayList<>();
67 } else {
68 this.listeners = listeners;
69 }
70 return this;
71 }
72
73 public void initService(ServiceLocator locator) {
74 setRepositoryListeners(locator.getServices(RepositoryListener.class));
75 }
76
77 public void dispatch(RepositoryEvent event) {
78 requireNonNull(event, "event cannot be null");
79 if (!listeners.isEmpty()) {
80 for (RepositoryListener listener : listeners) {
81 dispatch(event, listener);
82 }
83 }
84
85 RepositoryListener listener = event.getSession().getRepositoryListener();
86
87 if (listener != null) {
88 dispatch(event, listener);
89 }
90 }
91
92 private void dispatch(RepositoryEvent event, RepositoryListener listener) {
93 try {
94 switch (event.getType()) {
95 case ARTIFACT_DEPLOYED:
96 listener.artifactDeployed(event);
97 break;
98 case ARTIFACT_DEPLOYING:
99 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 }