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.util;
020
021import java.nio.file.Path;
022import java.nio.file.Paths;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026
027import org.eclipse.aether.RepositorySystem;
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
030import org.eclipse.aether.repository.RemoteRepository;
031import org.eclipse.aether.supplier.SessionBuilderSupplier;
032import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
033
034/**
035 * A helper to boot the repository system and a repository system session.
036 */
037public class Booter {
038    public static final String SUPPLIER = "supplier";
039
040    public static final String SISU = "sisu";
041
042    public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println);
043
044    public static String selectFactory(String[] args) {
045        if (args == null || args.length == 0) {
046            return SUPPLIER;
047        } else {
048            return args[0];
049        }
050    }
051
052    public static RepositorySystem newRepositorySystem(final String factory) {
053        switch (factory) {
054            case SUPPLIER:
055                return org.apache.maven.resolver.examples.supplier.SupplierRepositorySystemFactory
056                        .newRepositorySystem();
057            case SISU:
058                return org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory.newRepositorySystem();
059            default:
060                throw new IllegalArgumentException("Unknown factory: " + factory);
061        }
062    }
063
064    public static SessionBuilder newRepositorySystemSession(RepositorySystem system) {
065        Path localRepo = Paths.get("target/local-repo");
066        // FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
067        return new SessionBuilderSupplier(system)
068                .get()
069                //        .withLocalRepositoryBaseDirectories(fs.getPath("local-repo"))
070                .withLocalRepositoryBaseDirectories(localRepo)
071                .setRepositoryListener(new ConsoleRepositoryListener())
072                .setTransferListener(new ConsoleTransferListener())
073                .setConfigProperty("aether.generator.gpg.enabled", Boolean.TRUE.toString())
074                .setConfigProperty(
075                        "aether.generator.gpg.keyFilePath",
076                        Paths.get("src/main/resources/alice.key")
077                                .toAbsolutePath()
078                                .toString())
079                .setConfigProperty("aether.syncContext.named.factory", "noop");
080        // result.addOnSessionEndedHandler(() -> {
081        //     try {
082        //         fs.close();
083        //     } catch (IOException e) {
084        //         throw new UncheckedIOException(e);
085        //     }
086        // });
087
088        // uncomment to generate dirty trees
089        // session.setDependencyGraphTransformer( null );
090    }
091
092    public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
093        return new ArrayList<>(Collections.singletonList(newCentralRepository()));
094    }
095
096    private static RemoteRepository newCentralRepository() {
097        return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
098    }
099}