1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.impl;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Modifier;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.LinkedHashSet;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.eclipse.aether.RepositorySystem;
32 import org.eclipse.aether.internal.impl.DefaultArtifactResolver;
33 import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
34 import org.eclipse.aether.internal.impl.DefaultDeployer;
35 import org.eclipse.aether.internal.impl.DefaultFileProcessor;
36 import org.eclipse.aether.internal.impl.DefaultInstaller;
37 import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
38 import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider;
39 import org.eclipse.aether.internal.impl.DefaultMetadataResolver;
40 import org.eclipse.aether.internal.impl.DefaultOfflineController;
41 import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
42 import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider;
43 import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
44 import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider;
45 import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
46 import org.eclipse.aether.internal.impl.DefaultRepositorySystemLifecycle;
47 import org.eclipse.aether.internal.impl.DefaultTrackingFileManager;
48 import org.eclipse.aether.internal.impl.DefaultTransporterProvider;
49 import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager;
50 import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
51 import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
52 import org.eclipse.aether.internal.impl.LocalPathComposer;
53 import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory;
54 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
55 import org.eclipse.aether.internal.impl.TrackingFileManager;
56 import org.eclipse.aether.internal.impl.checksum.DefaultChecksumAlgorithmFactorySelector;
57 import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector;
58 import org.eclipse.aether.internal.impl.filter.DefaultRemoteRepositoryFilterManager;
59 import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
60 import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory;
61 import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactoryAdapterFactory;
62 import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactoryAdapterFactoryImpl;
63 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
64 import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
65 import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
66 import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
67 import org.eclipse.aether.spi.connector.transport.TransporterProvider;
68 import org.eclipse.aether.spi.io.FileProcessor;
69 import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
70 import org.eclipse.aether.spi.locator.Service;
71 import org.eclipse.aether.spi.locator.ServiceLocator;
72 import org.eclipse.aether.spi.log.LoggerFactory;
73 import org.eclipse.aether.spi.synccontext.SyncContextFactory;
74
75 import static java.util.Objects.requireNonNull;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 @Deprecated
93 public final class DefaultServiceLocator implements ServiceLocator {
94
95 private class Entry<T> {
96
97 private final Class<T> type;
98
99 private final Collection<Object> providers;
100
101 private List<T> instances;
102
103 Entry(Class<T> type) {
104 this.type = requireNonNull(type, "service type cannot be null");
105 providers = new LinkedHashSet<>(8);
106 }
107
108 public synchronized void setServices(T... services) {
109 providers.clear();
110 if (services != null) {
111 for (T service : services) {
112 providers.add(requireNonNull(service, "service instance cannot be null"));
113 }
114 }
115 instances = null;
116 }
117
118 public synchronized void setService(Class<? extends T> impl) {
119 providers.clear();
120 addService(impl);
121 }
122
123 public synchronized void addService(Class<? extends T> impl) {
124 providers.add(requireNonNull(impl, "implementation class cannot be null"));
125 instances = null;
126 }
127
128 public T getInstance() {
129 List<T> instances = getInstances();
130 return instances.isEmpty() ? null : instances.get(0);
131 }
132
133 public synchronized List<T> getInstances() {
134 if (instances == null) {
135 instances = new ArrayList<>(providers.size());
136 for (Object provider : providers) {
137 T instance;
138 if (provider instanceof Class) {
139 instance = newInstance((Class<?>) provider);
140 } else {
141 instance = type.cast(provider);
142 }
143 if (instance != null) {
144 instances.add(instance);
145 }
146 }
147 instances = Collections.unmodifiableList(instances);
148 }
149 return instances;
150 }
151
152 private T newInstance(Class<?> impl) {
153 try {
154 Constructor<?> constr = impl.getDeclaredConstructor();
155 if (!Modifier.isPublic(constr.getModifiers())) {
156 constr.setAccessible(true);
157 }
158 Object obj = constr.newInstance();
159
160 T instance = type.cast(obj);
161 if (instance instanceof Service) {
162 ((Service) instance).initService(DefaultServiceLocator.this);
163 }
164 return instance;
165 } catch (Exception | LinkageError e) {
166 serviceCreationFailed(type, impl, e);
167 }
168 return null;
169 }
170 }
171
172 private final Map<Class<?>, Entry<?>> entries;
173
174 private ErrorHandler errorHandler;
175
176
177
178
179 public DefaultServiceLocator() {
180 entries = new HashMap<>();
181
182 addService(RepositorySystem.class, DefaultRepositorySystem.class);
183 addService(ArtifactResolver.class, DefaultArtifactResolver.class);
184 addService(DependencyCollector.class, DefaultDependencyCollector.class);
185 addService(Deployer.class, DefaultDeployer.class);
186 addService(Installer.class, DefaultInstaller.class);
187 addService(MetadataResolver.class, DefaultMetadataResolver.class);
188 addService(RepositoryLayoutProvider.class, DefaultRepositoryLayoutProvider.class);
189 addService(RepositoryLayoutFactory.class, Maven2RepositoryLayoutFactory.class);
190 addService(TransporterProvider.class, DefaultTransporterProvider.class);
191 addService(ChecksumPolicyProvider.class, DefaultChecksumPolicyProvider.class);
192 addService(RepositoryConnectorProvider.class, DefaultRepositoryConnectorProvider.class);
193 addService(RemoteRepositoryManager.class, DefaultRemoteRepositoryManager.class);
194 addService(UpdateCheckManager.class, DefaultUpdateCheckManager.class);
195 addService(UpdatePolicyAnalyzer.class, DefaultUpdatePolicyAnalyzer.class);
196 addService(FileProcessor.class, DefaultFileProcessor.class);
197 addService(
198 org.eclipse.aether.impl.SyncContextFactory.class,
199 org.eclipse.aether.internal.impl.synccontext.legacy.DefaultSyncContextFactory.class);
200 addService(SyncContextFactory.class, DefaultSyncContextFactory.class);
201 addService(RepositoryEventDispatcher.class, DefaultRepositoryEventDispatcher.class);
202 addService(OfflineController.class, DefaultOfflineController.class);
203 addService(LocalRepositoryProvider.class, DefaultLocalRepositoryProvider.class);
204 addService(LocalRepositoryManagerFactory.class, SimpleLocalRepositoryManagerFactory.class);
205 addService(LocalRepositoryManagerFactory.class, EnhancedLocalRepositoryManagerFactory.class);
206 addService(LoggerFactory.class, Slf4jLoggerFactory.class);
207 addService(TrackingFileManager.class, DefaultTrackingFileManager.class);
208 addService(ChecksumAlgorithmFactorySelector.class, DefaultChecksumAlgorithmFactorySelector.class);
209 addService(LocalPathComposer.class, DefaultLocalPathComposer.class);
210 addService(RemoteRepositoryFilterManager.class, DefaultRemoteRepositoryFilterManager.class);
211 addService(RepositorySystemLifecycle.class, DefaultRepositorySystemLifecycle.class);
212 addService(NamedLockFactoryAdapterFactory.class, NamedLockFactoryAdapterFactoryImpl.class);
213 }
214
215 private <T> Entry<T> getEntry(Class<T> type, boolean create) {
216 @SuppressWarnings("unchecked")
217 Entry<T> entry = (Entry<T>) entries.get(requireNonNull(type, "service type cannot be null"));
218 if (entry == null && create) {
219 entry = new Entry<>(type);
220 entries.put(type, entry);
221 }
222 return entry;
223 }
224
225
226
227
228
229
230
231
232
233
234
235 public <T> DefaultServiceLocator setService(Class<T> type, Class<? extends T> impl) {
236 getEntry(type, true).setService(impl);
237 return this;
238 }
239
240
241
242
243
244
245
246
247
248
249
250 public <T> DefaultServiceLocator addService(Class<T> type, Class<? extends T> impl) {
251 getEntry(type, true).addService(impl);
252 return this;
253 }
254
255
256
257
258
259
260
261
262
263 public <T> DefaultServiceLocator setServices(Class<T> type, T... services) {
264 getEntry(type, true).setServices(services);
265 return this;
266 }
267
268 public <T> T getService(Class<T> type) {
269 Entry<T> entry = getEntry(type, false);
270 return (entry != null) ? entry.getInstance() : null;
271 }
272
273 public <T> List<T> getServices(Class<T> type) {
274 Entry<T> entry = getEntry(type, false);
275 return (entry != null) ? entry.getInstances() : null;
276 }
277
278 private void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception) {
279 if (errorHandler != null) {
280 errorHandler.serviceCreationFailed(type, impl, exception);
281 }
282 }
283
284
285
286
287
288
289 public void setErrorHandler(ErrorHandler errorHandler) {
290 this.errorHandler = errorHandler;
291 }
292
293
294
295
296 public abstract static class ErrorHandler {
297
298
299
300
301
302
303
304
305
306 public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception) {}
307 }
308 }