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.Collections;
26 import java.util.Map;
27
28 import org.eclipse.aether.RepositoryEvent;
29 import org.eclipse.aether.RepositoryListener;
30 import org.eclipse.aether.impl.RepositoryEventDispatcher;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import static java.util.Objects.requireNonNull;
35
36
37
38 @Singleton
39 @Named
40 public class DefaultRepositoryEventDispatcher implements RepositoryEventDispatcher {
41
42 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryEventDispatcher.class);
43
44 private final Map<String, RepositoryListener> listeners;
45
46 @Inject
47 public DefaultRepositoryEventDispatcher(Map<String, RepositoryListener> listeners) {
48 this.listeners = Collections.unmodifiableMap(listeners);
49 }
50
51 @Override
52 public void dispatch(RepositoryEvent event) {
53 requireNonNull(event, "event cannot be null");
54 if (!listeners.isEmpty()) {
55 for (RepositoryListener listener : listeners.values()) {
56 dispatch(event, listener);
57 }
58 }
59
60 RepositoryListener listener = event.getSession().getRepositoryListener();
61
62 if (listener != null) {
63 dispatch(event, listener);
64 }
65 }
66
67 private void dispatch(RepositoryEvent event, RepositoryListener listener) {
68 try {
69 switch (event.getType()) {
70 case ARTIFACT_DEPLOYED:
71 listener.artifactDeployed(event);
72 break;
73 case ARTIFACT_DEPLOYING:
74 listener.artifactDeploying(event);
75 break;
76 case ARTIFACT_DESCRIPTOR_INVALID:
77 listener.artifactDescriptorInvalid(event);
78 break;
79 case ARTIFACT_DESCRIPTOR_MISSING:
80 listener.artifactDescriptorMissing(event);
81 break;
82 case ARTIFACT_DOWNLOADED:
83 listener.artifactDownloaded(event);
84 break;
85 case ARTIFACT_DOWNLOADING:
86 listener.artifactDownloading(event);
87 break;
88 case ARTIFACT_INSTALLED:
89 listener.artifactInstalled(event);
90 break;
91 case ARTIFACT_INSTALLING:
92 listener.artifactInstalling(event);
93 break;
94 case ARTIFACT_RESOLVED:
95 listener.artifactResolved(event);
96 break;
97 case ARTIFACT_RESOLVING:
98 listener.artifactResolving(event);
99 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 }