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