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