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.supplier;
020
021import java.util.HashMap;
022import java.util.LinkedHashMap;
023import java.util.Map;
024import java.util.concurrent.atomic.AtomicBoolean;
025import java.util.function.Supplier;
026
027import org.apache.maven.model.building.DefaultModelBuilderFactory;
028import org.apache.maven.model.building.ModelBuilder;
029import org.apache.maven.repository.internal.*;
030import org.apache.maven.repository.internal.relocation.DistributionManagementArtifactRelocationSource;
031import org.apache.maven.repository.internal.relocation.UserPropertiesArtifactRelocationSource;
032import org.eclipse.aether.RepositoryListener;
033import org.eclipse.aether.RepositorySystem;
034import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
035import org.eclipse.aether.impl.ArtifactDescriptorReader;
036import org.eclipse.aether.impl.ArtifactResolver;
037import org.eclipse.aether.impl.DependencyCollector;
038import org.eclipse.aether.impl.Deployer;
039import org.eclipse.aether.impl.Installer;
040import org.eclipse.aether.impl.LocalRepositoryProvider;
041import org.eclipse.aether.impl.MetadataGeneratorFactory;
042import org.eclipse.aether.impl.MetadataResolver;
043import org.eclipse.aether.impl.OfflineController;
044import org.eclipse.aether.impl.RemoteRepositoryFilterManager;
045import org.eclipse.aether.impl.RemoteRepositoryManager;
046import org.eclipse.aether.impl.RepositoryConnectorProvider;
047import org.eclipse.aether.impl.RepositoryEventDispatcher;
048import org.eclipse.aether.impl.RepositorySystemLifecycle;
049import org.eclipse.aether.impl.UpdateCheckManager;
050import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
051import org.eclipse.aether.impl.VersionRangeResolver;
052import org.eclipse.aether.impl.VersionResolver;
053import org.eclipse.aether.internal.impl.*;
054import org.eclipse.aether.internal.impl.checksum.DefaultChecksumAlgorithmFactorySelector;
055import org.eclipse.aether.internal.impl.checksum.Md5ChecksumAlgorithmFactory;
056import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
057import org.eclipse.aether.internal.impl.checksum.Sha256ChecksumAlgorithmFactory;
058import org.eclipse.aether.internal.impl.checksum.Sha512ChecksumAlgorithmFactory;
059import org.eclipse.aether.internal.impl.checksum.SparseDirectoryTrustedChecksumsSource;
060import org.eclipse.aether.internal.impl.checksum.SummaryFileTrustedChecksumsSource;
061import org.eclipse.aether.internal.impl.checksum.TrustedToProvidedChecksumsSourceAdapter;
062import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector;
063import org.eclipse.aether.internal.impl.collect.DependencyCollectorDelegate;
064import org.eclipse.aether.internal.impl.collect.bf.BfDependencyCollector;
065import org.eclipse.aether.internal.impl.collect.df.DfDependencyCollector;
066import org.eclipse.aether.internal.impl.filter.DefaultRemoteRepositoryFilterManager;
067import org.eclipse.aether.internal.impl.filter.GroupIdRemoteRepositoryFilterSource;
068import org.eclipse.aether.internal.impl.filter.PrefixesRemoteRepositoryFilterSource;
069import org.eclipse.aether.internal.impl.resolution.TrustedChecksumsArtifactResolverPostProcessor;
070import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory;
071import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
072import org.eclipse.aether.internal.impl.synccontext.named.NameMappers;
073import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactoryAdapterFactory;
074import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactoryAdapterFactoryImpl;
075import org.eclipse.aether.internal.impl.transport.http.DefaultChecksumExtractor;
076import org.eclipse.aether.internal.impl.transport.http.Nx2ChecksumExtractor;
077import org.eclipse.aether.internal.impl.transport.http.XChecksumExtractor;
078import org.eclipse.aether.named.NamedLockFactory;
079import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
080import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
081import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
082import org.eclipse.aether.named.providers.NoopNamedLockFactory;
083import org.eclipse.aether.spi.checksums.ProvidedChecksumsSource;
084import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
085import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
086import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
087import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
088import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
089import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
090import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
091import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
092import org.eclipse.aether.spi.connector.transport.TransporterFactory;
093import org.eclipse.aether.spi.connector.transport.TransporterProvider;
094import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractor;
095import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractorStrategy;
096import org.eclipse.aether.spi.io.ChecksumProcessor;
097import org.eclipse.aether.spi.io.PathProcessor;
098import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
099import org.eclipse.aether.spi.resolution.ArtifactResolverPostProcessor;
100import org.eclipse.aether.spi.synccontext.SyncContextFactory;
101import org.eclipse.aether.transport.apache.ApacheTransporterFactory;
102import org.eclipse.aether.transport.file.FileTransporterFactory;
103import org.eclipse.aether.util.version.GenericVersionScheme;
104import org.eclipse.aether.version.VersionScheme;
105
106/**
107 * A simple memorizing {@link Supplier} of {@link org.eclipse.aether.RepositorySystem} instance, that on first call
108 * supplies lazily constructed instance, and on each subsequent call same instance. Hence, this instance should be
109 * thrown away immediately once repository system was created and there is no need for more instances. If new
110 * repository system instance needed, new instance of this class must be created. For proper shut down of returned
111 * repository system instance(s) use {@link RepositorySystem#shutdown()} method on supplied instance(s).
112 * <p>
113 * Since Resolver 2.0 this class offers access to various components via public getters, and allows even partial object
114 * graph construction.
115 * <p>
116 * Extend this class {@code createXXX()} methods and override to customize, if needed. The contract of this class makes
117 * sure that these (potentially overridden) methods are invoked only once, and instance created by those methods are
118 * memorized and kept as long as supplier instance is kept open.
119 * <p>
120 * This class is not thread safe and must be used from one thread only, while the constructed {@link RepositorySystem}
121 * is thread safe.
122 * <p>
123 * Important: Given the instance of supplier memorizes the supplier {@link RepositorySystem} instance it supplies,
124 * their lifecycle is shared as well: once supplied repository system is shut-down, this instance becomes closed as
125 * well. Any subsequent {@code getXXX} method invocation attempt will fail with {@link IllegalStateException}.
126 *
127 * @since 1.9.15
128 */
129public class RepositorySystemSupplier implements Supplier<RepositorySystem> {
130    private final AtomicBoolean closed = new AtomicBoolean(false);
131
132    public RepositorySystemSupplier() {}
133
134    private void checkClosed() {
135        if (closed.get()) {
136            throw new IllegalStateException("Supplier is closed");
137        }
138    }
139
140    private PathProcessor pathProcessor;
141
142    public final PathProcessor getPathProcessor() {
143        checkClosed();
144        if (pathProcessor == null) {
145            pathProcessor = createPathProcessor();
146        }
147        return pathProcessor;
148    }
149
150    protected PathProcessor createPathProcessor() {
151        return new DefaultPathProcessor();
152    }
153
154    private ChecksumProcessor checksumProcessor;
155
156    public final ChecksumProcessor getChecksumProcessor() {
157        checkClosed();
158        if (checksumProcessor == null) {
159            checksumProcessor = createChecksumProcessor();
160        }
161        return checksumProcessor;
162    }
163
164    protected ChecksumProcessor createChecksumProcessor() {
165        return new DefaultChecksumProcessor(getPathProcessor());
166    }
167
168    private TrackingFileManager trackingFileManager;
169
170    public final TrackingFileManager getTrackingFileManager() {
171        checkClosed();
172        if (trackingFileManager == null) {
173            trackingFileManager = createTrackingFileManager();
174        }
175        return trackingFileManager;
176    }
177
178    protected TrackingFileManager createTrackingFileManager() {
179        return new DefaultTrackingFileManager();
180    }
181
182    private LocalPathComposer localPathComposer;
183
184    public final LocalPathComposer getLocalPathComposer() {
185        checkClosed();
186        if (localPathComposer == null) {
187            localPathComposer = createLocalPathComposer();
188        }
189        return localPathComposer;
190    }
191
192    protected LocalPathComposer createLocalPathComposer() {
193        return new DefaultLocalPathComposer();
194    }
195
196    private LocalPathPrefixComposerFactory localPathPrefixComposerFactory;
197
198    public final LocalPathPrefixComposerFactory getLocalPathPrefixComposerFactory() {
199        checkClosed();
200        if (localPathPrefixComposerFactory == null) {
201            localPathPrefixComposerFactory = createLocalPathPrefixComposerFactory();
202        }
203        return localPathPrefixComposerFactory;
204    }
205
206    protected LocalPathPrefixComposerFactory createLocalPathPrefixComposerFactory() {
207        return new DefaultLocalPathPrefixComposerFactory();
208    }
209
210    private RepositorySystemLifecycle repositorySystemLifecycle;
211
212    public final RepositorySystemLifecycle getRepositorySystemLifecycle() {
213        checkClosed();
214        if (repositorySystemLifecycle == null) {
215            repositorySystemLifecycle = createRepositorySystemLifecycle();
216            repositorySystemLifecycle.addOnSystemEndedHandler(() -> closed.set(true));
217        }
218        return repositorySystemLifecycle;
219    }
220
221    protected RepositorySystemLifecycle createRepositorySystemLifecycle() {
222        return new DefaultRepositorySystemLifecycle();
223    }
224
225    private OfflineController offlineController;
226
227    public final OfflineController getOfflineController() {
228        checkClosed();
229        if (offlineController == null) {
230            offlineController = createOfflineController();
231        }
232        return offlineController;
233    }
234
235    protected OfflineController createOfflineController() {
236        return new DefaultOfflineController();
237    }
238
239    private UpdatePolicyAnalyzer updatePolicyAnalyzer;
240
241    public final UpdatePolicyAnalyzer getUpdatePolicyAnalyzer() {
242        checkClosed();
243        if (updatePolicyAnalyzer == null) {
244            updatePolicyAnalyzer = createUpdatePolicyAnalyzer();
245        }
246        return updatePolicyAnalyzer;
247    }
248
249    protected UpdatePolicyAnalyzer createUpdatePolicyAnalyzer() {
250        return new DefaultUpdatePolicyAnalyzer();
251    }
252
253    private ChecksumPolicyProvider checksumPolicyProvider;
254
255    public final ChecksumPolicyProvider getChecksumPolicyProvider() {
256        checkClosed();
257        if (checksumPolicyProvider == null) {
258            checksumPolicyProvider = createChecksumPolicyProvider();
259        }
260        return checksumPolicyProvider;
261    }
262
263    protected ChecksumPolicyProvider createChecksumPolicyProvider() {
264        return new DefaultChecksumPolicyProvider();
265    }
266
267    private UpdateCheckManager updateCheckManager;
268
269    public final UpdateCheckManager getUpdateCheckManager() {
270        checkClosed();
271        if (updateCheckManager == null) {
272            updateCheckManager = createUpdateCheckManager();
273        }
274        return updateCheckManager;
275    }
276
277    protected UpdateCheckManager createUpdateCheckManager() {
278        return new DefaultUpdateCheckManager(getTrackingFileManager(), getUpdatePolicyAnalyzer(), getPathProcessor());
279    }
280
281    private Map<String, NamedLockFactory> namedLockFactories;
282
283    public final Map<String, NamedLockFactory> getNamedLockFactories() {
284        checkClosed();
285        if (namedLockFactories == null) {
286            namedLockFactories = createNamedLockFactories();
287        }
288        return namedLockFactories;
289    }
290
291    protected Map<String, NamedLockFactory> createNamedLockFactories() {
292        HashMap<String, NamedLockFactory> result = new HashMap<>();
293        result.put(NoopNamedLockFactory.NAME, new NoopNamedLockFactory());
294        result.put(LocalReadWriteLockNamedLockFactory.NAME, new LocalReadWriteLockNamedLockFactory());
295        result.put(LocalSemaphoreNamedLockFactory.NAME, new LocalSemaphoreNamedLockFactory());
296        result.put(FileLockNamedLockFactory.NAME, new FileLockNamedLockFactory());
297        return result;
298    }
299
300    private Map<String, NameMapper> nameMappers;
301
302    public final Map<String, NameMapper> getNameMappers() {
303        checkClosed();
304        if (nameMappers == null) {
305            nameMappers = createNameMappers();
306        }
307        return nameMappers;
308    }
309
310    protected Map<String, NameMapper> createNameMappers() {
311        HashMap<String, NameMapper> result = new HashMap<>();
312        result.put(NameMappers.STATIC_NAME, NameMappers.staticNameMapper());
313        result.put(NameMappers.GAV_NAME, NameMappers.gavNameMapper());
314        result.put(NameMappers.DISCRIMINATING_NAME, NameMappers.discriminatingNameMapper());
315        result.put(NameMappers.FILE_GAV_NAME, NameMappers.fileGavNameMapper());
316        result.put(NameMappers.FILE_HGAV_NAME, NameMappers.fileHashingGavNameMapper());
317        return result;
318    }
319
320    private NamedLockFactoryAdapterFactory namedLockFactoryAdapterFactory;
321
322    public final NamedLockFactoryAdapterFactory getNamedLockFactoryAdapterFactory() {
323        checkClosed();
324        if (namedLockFactoryAdapterFactory == null) {
325            namedLockFactoryAdapterFactory = createNamedLockFactoryAdapterFactory();
326        }
327        return namedLockFactoryAdapterFactory;
328    }
329
330    protected NamedLockFactoryAdapterFactory createNamedLockFactoryAdapterFactory() {
331        return new NamedLockFactoryAdapterFactoryImpl(
332                getNamedLockFactories(), getNameMappers(), getRepositorySystemLifecycle());
333    }
334
335    private SyncContextFactory syncContextFactory;
336
337    public final SyncContextFactory getSyncContextFactory() {
338        checkClosed();
339        if (syncContextFactory == null) {
340            syncContextFactory = createSyncContextFactory();
341        }
342        return syncContextFactory;
343    }
344
345    protected SyncContextFactory createSyncContextFactory() {
346        return new DefaultSyncContextFactory(getNamedLockFactoryAdapterFactory());
347    }
348
349    private Map<String, ChecksumAlgorithmFactory> checksumAlgorithmFactories;
350
351    public final Map<String, ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() {
352        checkClosed();
353        if (checksumAlgorithmFactories == null) {
354            checksumAlgorithmFactories = createChecksumAlgorithmFactories();
355        }
356        return checksumAlgorithmFactories;
357    }
358
359    protected Map<String, ChecksumAlgorithmFactory> createChecksumAlgorithmFactories() {
360        HashMap<String, ChecksumAlgorithmFactory> result = new HashMap<>();
361        result.put(Sha512ChecksumAlgorithmFactory.NAME, new Sha512ChecksumAlgorithmFactory());
362        result.put(Sha256ChecksumAlgorithmFactory.NAME, new Sha256ChecksumAlgorithmFactory());
363        result.put(Sha1ChecksumAlgorithmFactory.NAME, new Sha1ChecksumAlgorithmFactory());
364        result.put(Md5ChecksumAlgorithmFactory.NAME, new Md5ChecksumAlgorithmFactory());
365        return result;
366    }
367
368    private ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
369
370    public final ChecksumAlgorithmFactorySelector getChecksumAlgorithmFactorySelector() {
371        checkClosed();
372        if (checksumAlgorithmFactorySelector == null) {
373            checksumAlgorithmFactorySelector = createChecksumAlgorithmFactorySelector();
374        }
375        return checksumAlgorithmFactorySelector;
376    }
377
378    protected ChecksumAlgorithmFactorySelector createChecksumAlgorithmFactorySelector() {
379        return new DefaultChecksumAlgorithmFactorySelector(getChecksumAlgorithmFactories());
380    }
381
382    private Map<String, RepositoryLayoutFactory> repositoryLayoutFactories;
383
384    public final Map<String, RepositoryLayoutFactory> getRepositoryLayoutFactories() {
385        checkClosed();
386        if (repositoryLayoutFactories == null) {
387            repositoryLayoutFactories = createRepositoryLayoutFactories();
388        }
389        return repositoryLayoutFactories;
390    }
391
392    protected Map<String, RepositoryLayoutFactory> createRepositoryLayoutFactories() {
393        HashMap<String, RepositoryLayoutFactory> result = new HashMap<>();
394        result.put(
395                Maven2RepositoryLayoutFactory.NAME,
396                new Maven2RepositoryLayoutFactory(getChecksumAlgorithmFactorySelector()));
397        return result;
398    }
399
400    private RepositoryLayoutProvider repositoryLayoutProvider;
401
402    public final RepositoryLayoutProvider getRepositoryLayoutProvider() {
403        checkClosed();
404        if (repositoryLayoutProvider == null) {
405            repositoryLayoutProvider = createRepositoryLayoutProvider();
406        }
407        return repositoryLayoutProvider;
408    }
409
410    protected RepositoryLayoutProvider createRepositoryLayoutProvider() {
411        return new DefaultRepositoryLayoutProvider(getRepositoryLayoutFactories());
412    }
413
414    private LocalRepositoryProvider localRepositoryProvider;
415
416    public final LocalRepositoryProvider getLocalRepositoryProvider() {
417        checkClosed();
418        if (localRepositoryProvider == null) {
419            localRepositoryProvider = createLocalRepositoryProvider();
420        }
421        return localRepositoryProvider;
422    }
423
424    protected LocalRepositoryProvider createLocalRepositoryProvider() {
425        LocalPathComposer localPathComposer = getLocalPathComposer();
426        HashMap<String, LocalRepositoryManagerFactory> localRepositoryProviders = new HashMap<>(2);
427        localRepositoryProviders.put(
428                SimpleLocalRepositoryManagerFactory.NAME, new SimpleLocalRepositoryManagerFactory(localPathComposer));
429        localRepositoryProviders.put(
430                EnhancedLocalRepositoryManagerFactory.NAME,
431                new EnhancedLocalRepositoryManagerFactory(
432                        localPathComposer, getTrackingFileManager(), getLocalPathPrefixComposerFactory()));
433        return new DefaultLocalRepositoryProvider(localRepositoryProviders);
434    }
435
436    private RemoteRepositoryManager remoteRepositoryManager;
437
438    public final RemoteRepositoryManager getRemoteRepositoryManager() {
439        checkClosed();
440        if (remoteRepositoryManager == null) {
441            remoteRepositoryManager = createRemoteRepositoryManager();
442        }
443        return remoteRepositoryManager;
444    }
445
446    protected RemoteRepositoryManager createRemoteRepositoryManager() {
447        return new DefaultRemoteRepositoryManager(getUpdatePolicyAnalyzer(), getChecksumPolicyProvider());
448    }
449
450    private Map<String, RemoteRepositoryFilterSource> remoteRepositoryFilterSources;
451
452    public final Map<String, RemoteRepositoryFilterSource> getRemoteRepositoryFilterSources() {
453        checkClosed();
454        if (remoteRepositoryFilterSources == null) {
455            remoteRepositoryFilterSources = createRemoteRepositoryFilterSources();
456        }
457        return remoteRepositoryFilterSources;
458    }
459
460    protected Map<String, RemoteRepositoryFilterSource> createRemoteRepositoryFilterSources() {
461        HashMap<String, RemoteRepositoryFilterSource> result = new HashMap<>();
462        result.put(
463                GroupIdRemoteRepositoryFilterSource.NAME,
464                new GroupIdRemoteRepositoryFilterSource(getRepositorySystemLifecycle()));
465        result.put(
466                PrefixesRemoteRepositoryFilterSource.NAME,
467                new PrefixesRemoteRepositoryFilterSource(getRepositoryLayoutProvider()));
468        return result;
469    }
470
471    private RemoteRepositoryFilterManager remoteRepositoryFilterManager;
472
473    public final RemoteRepositoryFilterManager getRemoteRepositoryFilterManager() {
474        checkClosed();
475        if (remoteRepositoryFilterManager == null) {
476            remoteRepositoryFilterManager = createRemoteRepositoryFilterManager();
477        }
478        return remoteRepositoryFilterManager;
479    }
480
481    protected RemoteRepositoryFilterManager createRemoteRepositoryFilterManager() {
482        return new DefaultRemoteRepositoryFilterManager(getRemoteRepositoryFilterSources());
483    }
484
485    private Map<String, RepositoryListener> repositoryListeners;
486
487    public final Map<String, RepositoryListener> getRepositoryListeners() {
488        checkClosed();
489        if (repositoryListeners == null) {
490            repositoryListeners = createRepositoryListeners();
491        }
492        return repositoryListeners;
493    }
494
495    protected Map<String, RepositoryListener> createRepositoryListeners() {
496        return new HashMap<>();
497    }
498
499    private RepositoryEventDispatcher repositoryEventDispatcher;
500
501    public final RepositoryEventDispatcher getRepositoryEventDispatcher() {
502        checkClosed();
503        if (repositoryEventDispatcher == null) {
504            repositoryEventDispatcher = createRepositoryEventDispatcher();
505        }
506        return repositoryEventDispatcher;
507    }
508
509    protected RepositoryEventDispatcher createRepositoryEventDispatcher() {
510        return new DefaultRepositoryEventDispatcher(getRepositoryListeners());
511    }
512
513    private Map<String, TrustedChecksumsSource> trustedChecksumsSources;
514
515    public final Map<String, TrustedChecksumsSource> getTrustedChecksumsSources() {
516        checkClosed();
517        if (trustedChecksumsSources == null) {
518            trustedChecksumsSources = createTrustedChecksumsSources();
519        }
520        return trustedChecksumsSources;
521    }
522
523    protected Map<String, TrustedChecksumsSource> createTrustedChecksumsSources() {
524        HashMap<String, TrustedChecksumsSource> result = new HashMap<>();
525        result.put(
526                SparseDirectoryTrustedChecksumsSource.NAME,
527                new SparseDirectoryTrustedChecksumsSource(getChecksumProcessor(), getLocalPathComposer()));
528        result.put(
529                SummaryFileTrustedChecksumsSource.NAME,
530                new SummaryFileTrustedChecksumsSource(getLocalPathComposer(), getRepositorySystemLifecycle()));
531        return result;
532    }
533
534    private Map<String, ProvidedChecksumsSource> providedChecksumsSources;
535
536    public final Map<String, ProvidedChecksumsSource> getProvidedChecksumsSources() {
537        checkClosed();
538        if (providedChecksumsSources == null) {
539            providedChecksumsSources = createProvidedChecksumsSources();
540        }
541        return providedChecksumsSources;
542    }
543
544    protected Map<String, ProvidedChecksumsSource> createProvidedChecksumsSources() {
545        HashMap<String, ProvidedChecksumsSource> result = new HashMap<>();
546        result.put(
547                TrustedToProvidedChecksumsSourceAdapter.NAME,
548                new TrustedToProvidedChecksumsSourceAdapter(getTrustedChecksumsSources()));
549        return result;
550    }
551
552    private Map<String, ChecksumExtractorStrategy> checksumExtractorStrategies;
553
554    public final Map<String, ChecksumExtractorStrategy> getChecksumExtractorStrategies() {
555        checkClosed();
556        if (checksumExtractorStrategies == null) {
557            checksumExtractorStrategies = createChecksumExtractorStrategies();
558        }
559        return checksumExtractorStrategies;
560    }
561
562    protected Map<String, ChecksumExtractorStrategy> createChecksumExtractorStrategies() {
563        HashMap<String, ChecksumExtractorStrategy> result = new HashMap<>();
564        result.put(XChecksumExtractor.NAME, new XChecksumExtractor());
565        result.put(Nx2ChecksumExtractor.NAME, new Nx2ChecksumExtractor());
566        return result;
567    }
568
569    private ChecksumExtractor checksumExtractor;
570
571    public final ChecksumExtractor getChecksumExtractor() {
572        checkClosed();
573        if (checksumExtractor == null) {
574            checksumExtractor = createChecksumExtractor();
575        }
576        return checksumExtractor;
577    }
578
579    protected ChecksumExtractor createChecksumExtractor() {
580        return new DefaultChecksumExtractor(getChecksumExtractorStrategies());
581    }
582
583    private Map<String, TransporterFactory> transporterFactories;
584
585    public final Map<String, TransporterFactory> getTransporterFactories() {
586        checkClosed();
587        if (transporterFactories == null) {
588            transporterFactories = createTransporterFactories();
589        }
590        return transporterFactories;
591    }
592
593    protected Map<String, TransporterFactory> createTransporterFactories() {
594        HashMap<String, TransporterFactory> result = new HashMap<>();
595        result.put(FileTransporterFactory.NAME, new FileTransporterFactory());
596        result.put(ApacheTransporterFactory.NAME, new ApacheTransporterFactory(getChecksumExtractor()));
597        return result;
598    }
599
600    private TransporterProvider transporterProvider;
601
602    public final TransporterProvider getTransporterProvider() {
603        checkClosed();
604        if (transporterProvider == null) {
605            transporterProvider = createTransporterProvider();
606        }
607        return transporterProvider;
608    }
609
610    protected TransporterProvider createTransporterProvider() {
611        return new DefaultTransporterProvider(getTransporterFactories());
612    }
613
614    private BasicRepositoryConnectorFactory basicRepositoryConnectorFactory;
615
616    public final BasicRepositoryConnectorFactory getBasicRepositoryConnectorFactory() {
617        checkClosed();
618        if (basicRepositoryConnectorFactory == null) {
619            basicRepositoryConnectorFactory = createBasicRepositoryConnectorFactory();
620        }
621        return basicRepositoryConnectorFactory;
622    }
623
624    protected BasicRepositoryConnectorFactory createBasicRepositoryConnectorFactory() {
625        return new BasicRepositoryConnectorFactory(
626                getTransporterProvider(),
627                getRepositoryLayoutProvider(),
628                getChecksumPolicyProvider(),
629                getChecksumProcessor(),
630                getProvidedChecksumsSources());
631    }
632
633    private Map<String, RepositoryConnectorFactory> repositoryConnectorFactories;
634
635    public final Map<String, RepositoryConnectorFactory> getRepositoryConnectorFactories() {
636        checkClosed();
637        if (repositoryConnectorFactories == null) {
638            repositoryConnectorFactories = createRepositoryConnectorFactories();
639        }
640        return repositoryConnectorFactories;
641    }
642
643    protected Map<String, RepositoryConnectorFactory> createRepositoryConnectorFactories() {
644        HashMap<String, RepositoryConnectorFactory> result = new HashMap<>();
645        result.put(BasicRepositoryConnectorFactory.NAME, getBasicRepositoryConnectorFactory());
646        return result;
647    }
648
649    private RepositoryConnectorProvider repositoryConnectorProvider;
650
651    public final RepositoryConnectorProvider getRepositoryConnectorProvider() {
652        checkClosed();
653        if (repositoryConnectorProvider == null) {
654            repositoryConnectorProvider = createRepositoryConnectorProvider();
655        }
656        return repositoryConnectorProvider;
657    }
658
659    protected RepositoryConnectorProvider createRepositoryConnectorProvider() {
660        return new DefaultRepositoryConnectorProvider(
661                getRepositoryConnectorFactories(), getRemoteRepositoryFilterManager());
662    }
663
664    private Installer installer;
665
666    public final Installer getInstaller() {
667        checkClosed();
668        if (installer == null) {
669            installer = createInstaller();
670        }
671        return installer;
672    }
673
674    protected Installer createInstaller() {
675        return new DefaultInstaller(
676                getPathProcessor(),
677                getRepositoryEventDispatcher(),
678                getMetadataGeneratorFactories(),
679                getSyncContextFactory());
680    }
681
682    private Deployer deployer;
683
684    public final Deployer getDeployer() {
685        checkClosed();
686        if (deployer == null) {
687            deployer = createDeployer();
688        }
689        return deployer;
690    }
691
692    protected Deployer createDeployer() {
693        return new DefaultDeployer(
694                getPathProcessor(),
695                getRepositoryEventDispatcher(),
696                getRepositoryConnectorProvider(),
697                getRemoteRepositoryManager(),
698                getUpdateCheckManager(),
699                getMetadataGeneratorFactories(),
700                getSyncContextFactory(),
701                getOfflineController());
702    }
703
704    private Map<String, DependencyCollectorDelegate> dependencyCollectorDelegates;
705
706    public final Map<String, DependencyCollectorDelegate> getDependencyCollectorDelegates() {
707        checkClosed();
708        if (dependencyCollectorDelegates == null) {
709            dependencyCollectorDelegates = createDependencyCollectorDelegates();
710        }
711        return dependencyCollectorDelegates;
712    }
713
714    protected Map<String, DependencyCollectorDelegate> createDependencyCollectorDelegates() {
715        RemoteRepositoryManager remoteRepositoryManager = getRemoteRepositoryManager();
716        ArtifactDescriptorReader artifactDescriptorReader = getArtifactDescriptorReader();
717        VersionRangeResolver versionRangeResolver = getVersionRangeResolver();
718        HashMap<String, DependencyCollectorDelegate> result = new HashMap<>();
719        result.put(
720                DfDependencyCollector.NAME,
721                new DfDependencyCollector(remoteRepositoryManager, artifactDescriptorReader, versionRangeResolver));
722        result.put(
723                BfDependencyCollector.NAME,
724                new BfDependencyCollector(remoteRepositoryManager, artifactDescriptorReader, versionRangeResolver));
725        return result;
726    }
727
728    private DependencyCollector dependencyCollector;
729
730    public final DependencyCollector getDependencyCollector() {
731        checkClosed();
732        if (dependencyCollector == null) {
733            dependencyCollector = createDependencyCollector();
734        }
735        return dependencyCollector;
736    }
737
738    protected DependencyCollector createDependencyCollector() {
739        return new DefaultDependencyCollector(getDependencyCollectorDelegates());
740    }
741
742    private Map<String, ArtifactResolverPostProcessor> artifactResolverPostProcessors;
743
744    public final Map<String, ArtifactResolverPostProcessor> getArtifactResolverPostProcessors() {
745        checkClosed();
746        if (artifactResolverPostProcessors == null) {
747            artifactResolverPostProcessors = createArtifactResolverPostProcessors();
748        }
749        return artifactResolverPostProcessors;
750    }
751
752    protected Map<String, ArtifactResolverPostProcessor> createArtifactResolverPostProcessors() {
753        HashMap<String, ArtifactResolverPostProcessor> result = new HashMap<>();
754        result.put(
755                TrustedChecksumsArtifactResolverPostProcessor.NAME,
756                new TrustedChecksumsArtifactResolverPostProcessor(
757                        getChecksumAlgorithmFactorySelector(), getTrustedChecksumsSources()));
758        return result;
759    }
760
761    private ArtifactResolver artifactResolver;
762
763    public final ArtifactResolver getArtifactResolver() {
764        checkClosed();
765        if (artifactResolver == null) {
766            artifactResolver = createArtifactResolver();
767        }
768        return artifactResolver;
769    }
770
771    protected ArtifactResolver createArtifactResolver() {
772        return new DefaultArtifactResolver(
773                getPathProcessor(),
774                getRepositoryEventDispatcher(),
775                getVersionResolver(),
776                getUpdateCheckManager(),
777                getRepositoryConnectorProvider(),
778                getRemoteRepositoryManager(),
779                getSyncContextFactory(),
780                getOfflineController(),
781                getArtifactResolverPostProcessors(),
782                getRemoteRepositoryFilterManager());
783    }
784
785    private MetadataResolver metadataResolver;
786
787    public final MetadataResolver getMetadataResolver() {
788        checkClosed();
789        if (metadataResolver == null) {
790            metadataResolver = createMetadataResolver();
791        }
792        return metadataResolver;
793    }
794
795    protected MetadataResolver createMetadataResolver() {
796        return new DefaultMetadataResolver(
797                getRepositoryEventDispatcher(),
798                getUpdateCheckManager(),
799                getRepositoryConnectorProvider(),
800                getRemoteRepositoryManager(),
801                getSyncContextFactory(),
802                getOfflineController(),
803                getRemoteRepositoryFilterManager(),
804                getPathProcessor());
805    }
806
807    private VersionScheme versionScheme;
808
809    public final VersionScheme getVersionScheme() {
810        checkClosed();
811        if (versionScheme == null) {
812            versionScheme = createVersionScheme();
813        }
814        return versionScheme;
815    }
816
817    protected VersionScheme createVersionScheme() {
818        return new GenericVersionScheme();
819    }
820
821    // Maven provided
822
823    private Map<String, MetadataGeneratorFactory> metadataGeneratorFactories;
824
825    public final Map<String, MetadataGeneratorFactory> getMetadataGeneratorFactories() {
826        checkClosed();
827        if (metadataGeneratorFactories == null) {
828            metadataGeneratorFactories = createMetadataGeneratorFactories();
829        }
830        return metadataGeneratorFactories;
831    }
832
833    protected Map<String, MetadataGeneratorFactory> createMetadataGeneratorFactories() {
834        // from maven-resolver-provider
835        HashMap<String, MetadataGeneratorFactory> result = new HashMap<>();
836        result.put(PluginsMetadataGeneratorFactory.NAME, new PluginsMetadataGeneratorFactory());
837        result.put(VersionsMetadataGeneratorFactory.NAME, new VersionsMetadataGeneratorFactory());
838        result.put(SnapshotMetadataGeneratorFactory.NAME, new SnapshotMetadataGeneratorFactory());
839        return result;
840    }
841
842    private LinkedHashMap<String, MavenArtifactRelocationSource> artifactRelocationSources;
843
844    public final LinkedHashMap<String, MavenArtifactRelocationSource> getMavenArtifactRelocationSources() {
845        checkClosed();
846        if (artifactRelocationSources == null) {
847            artifactRelocationSources = createMavenArtifactRelocationSources();
848        }
849        return artifactRelocationSources;
850    }
851
852    protected LinkedHashMap<String, MavenArtifactRelocationSource> createMavenArtifactRelocationSources() {
853        // from maven-resolver-provider
854        LinkedHashMap<String, MavenArtifactRelocationSource> result = new LinkedHashMap<>();
855        result.put(UserPropertiesArtifactRelocationSource.NAME, new UserPropertiesArtifactRelocationSource());
856        result.put(
857                DistributionManagementArtifactRelocationSource.NAME,
858                new DistributionManagementArtifactRelocationSource());
859        return result;
860    }
861
862    private ArtifactDescriptorReader artifactDescriptorReader;
863
864    public final ArtifactDescriptorReader getArtifactDescriptorReader() {
865        checkClosed();
866        if (artifactDescriptorReader == null) {
867            artifactDescriptorReader = createArtifactDescriptorReader();
868        }
869        return artifactDescriptorReader;
870    }
871
872    protected ArtifactDescriptorReader createArtifactDescriptorReader() {
873        // from maven-resolver-provider
874        return new DefaultArtifactDescriptorReader(
875                getRemoteRepositoryManager(),
876                getVersionResolver(),
877                getVersionRangeResolver(),
878                getArtifactResolver(),
879                getModelBuilder(),
880                getRepositoryEventDispatcher(),
881                getModelCacheFactory(),
882                getMavenArtifactRelocationSources());
883    }
884
885    private VersionResolver versionResolver;
886
887    public final VersionResolver getVersionResolver() {
888        checkClosed();
889        if (versionResolver == null) {
890            versionResolver = createVersionResolver();
891        }
892        return versionResolver;
893    }
894
895    protected VersionResolver createVersionResolver() {
896        // from maven-resolver-provider
897        return new DefaultVersionResolver(
898                getMetadataResolver(), getSyncContextFactory(), getRepositoryEventDispatcher());
899    }
900
901    private VersionRangeResolver versionRangeResolver;
902
903    public final VersionRangeResolver getVersionRangeResolver() {
904        checkClosed();
905        if (versionRangeResolver == null) {
906            versionRangeResolver = createVersionRangeResolver();
907        }
908        return versionRangeResolver;
909    }
910
911    protected VersionRangeResolver createVersionRangeResolver() {
912        // from maven-resolver-provider
913        return new DefaultVersionRangeResolver(
914                getMetadataResolver(), getSyncContextFactory(), getRepositoryEventDispatcher(), getVersionScheme());
915    }
916
917    private ModelBuilder modelBuilder;
918
919    public final ModelBuilder getModelBuilder() {
920        checkClosed();
921        if (modelBuilder == null) {
922            modelBuilder = createModelBuilder();
923        }
924        return modelBuilder;
925    }
926
927    protected ModelBuilder createModelBuilder() {
928        // from maven-model-builder
929        return new DefaultModelBuilderFactory().newInstance();
930    }
931
932    private ModelCacheFactory modelCacheFactory;
933
934    public final ModelCacheFactory getModelCacheFactory() {
935        checkClosed();
936        if (modelCacheFactory == null) {
937            modelCacheFactory = createModelCacheFactory();
938        }
939        return modelCacheFactory;
940    }
941
942    protected ModelCacheFactory createModelCacheFactory() {
943        // from maven-resolver-provider
944        return new DefaultModelCacheFactory();
945    }
946
947    private RepositorySystem repositorySystem;
948
949    public final RepositorySystem getRepositorySystem() {
950        checkClosed();
951        if (repositorySystem == null) {
952            repositorySystem = createRepositorySystem();
953        }
954        return repositorySystem;
955    }
956
957    protected RepositorySystem createRepositorySystem() {
958        return new DefaultRepositorySystem(
959                getVersionResolver(),
960                getVersionRangeResolver(),
961                getArtifactResolver(),
962                getMetadataResolver(),
963                getArtifactDescriptorReader(),
964                getDependencyCollector(),
965                getInstaller(),
966                getDeployer(),
967                getLocalRepositoryProvider(),
968                getSyncContextFactory(),
969                getRemoteRepositoryManager(),
970                getRepositorySystemLifecycle());
971    }
972
973    @Override
974    public RepositorySystem get() {
975        return getRepositorySystem();
976    }
977}