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.apache.maven.resolver.examples.supplier;
020
021import java.util.HashMap;
022import java.util.LinkedHashMap;
023import java.util.Map;
024
025import org.eclipse.aether.RepositorySystem;
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.generator.gnupg.GnupgSignatureArtifactGeneratorFactory;
029import org.eclipse.aether.generator.gnupg.loaders.GpgAgentPasswordLoader;
030import org.eclipse.aether.generator.gnupg.loaders.GpgConfLoader;
031import org.eclipse.aether.generator.gnupg.loaders.GpgEnvLoader;
032import org.eclipse.aether.resolution.ArtifactDescriptorResult;
033import org.eclipse.aether.spi.artifact.decorator.ArtifactDecorator;
034import org.eclipse.aether.spi.artifact.decorator.ArtifactDecoratorFactory;
035import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory;
036import org.eclipse.aether.spi.connector.transport.TransporterFactory;
037import org.eclipse.aether.supplier.RepositorySystemSupplier;
038import org.eclipse.aether.transport.jdk.JdkTransporterFactory;
039import org.eclipse.aether.transport.jetty.JettyTransporterFactory;
040
041/**
042 * A factory for repository system instances that employs Maven Artifact Resolver's provided supplier.
043 */
044public class SupplierRepositorySystemFactory {
045    public static RepositorySystem newRepositorySystem() {
046        return new RepositorySystemSupplier() {
047            @Override
048            protected Map<String, ArtifactGeneratorFactory> createArtifactGeneratorFactories() {
049                Map<String, ArtifactGeneratorFactory> result = super.createArtifactGeneratorFactories();
050                result.put(
051                        GnupgSignatureArtifactGeneratorFactory.NAME,
052                        new GnupgSignatureArtifactGeneratorFactory(
053                                getArtifactPredicateFactory(), getGnupgSignatureArtifactGeneratorFactoryLoaders()));
054                return result;
055            }
056
057            private Map<String, GnupgSignatureArtifactGeneratorFactory.Loader>
058                    getGnupgSignatureArtifactGeneratorFactoryLoaders() {
059                // order matters
060                LinkedHashMap<String, GnupgSignatureArtifactGeneratorFactory.Loader> loaders = new LinkedHashMap<>();
061                loaders.put(GpgEnvLoader.NAME, new GpgEnvLoader());
062                loaders.put(GpgConfLoader.NAME, new GpgConfLoader());
063                loaders.put(GpgAgentPasswordLoader.NAME, new GpgAgentPasswordLoader());
064                return loaders;
065            }
066
067            @Override
068            protected Map<String, ArtifactDecoratorFactory> createArtifactDecoratorFactories() {
069                Map<String, ArtifactDecoratorFactory> result = super.createArtifactDecoratorFactories();
070                result.put("color", new ArtifactDecoratorFactory() {
071                    @Override
072                    public ArtifactDecorator newInstance(RepositorySystemSession session) {
073                        return new ArtifactDecorator() {
074                            @Override
075                            public Artifact decorateArtifact(ArtifactDescriptorResult artifactDescriptorResult) {
076                                Map<String, String> properties = new HashMap<>(
077                                        artifactDescriptorResult.getArtifact().getProperties());
078                                properties.put("color", "red");
079                                return artifactDescriptorResult.getArtifact().setProperties(properties);
080                            }
081                        };
082                    }
083
084                    @Override
085                    public float getPriority() {
086                        return 0;
087                    }
088                });
089                return result;
090            }
091
092            @Override
093            protected Map<String, TransporterFactory> createTransporterFactories() {
094                Map<String, TransporterFactory> result = super.createTransporterFactories();
095                result.put(
096                        JdkTransporterFactory.NAME,
097                        new JdkTransporterFactory(getChecksumExtractor(), getPathProcessor()));
098                result.put(
099                        JettyTransporterFactory.NAME,
100                        new JettyTransporterFactory(getChecksumExtractor(), getPathProcessor()));
101                return result;
102            }
103        }.get();
104    }
105}