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                .setSystemProperties(System.getProperties())
073                .withLocalRepositoryBaseDirectories(fs.getPath("local-repo"))
074                .setRepositoryListener(new ConsoleRepositoryListener())
075                .setTransferListener(new ConsoleTransferListener())
076                .setConfigProperty("aether.generator.gpg.enabled", Boolean.TRUE.toString())
077                .setConfigProperty(
078                        "aether.generator.gpg.keyFilePath",
079                        Paths.get("src/main/resources/alice.key")
080                                .toAbsolutePath()
081                                .toString())
082                .setConfigProperty("aether.syncContext.named.factory", "noop");
083        result.addOnSessionEndedHandler(() -> {
084            try {
085                fs.close();
086            } catch (IOException e) {
087                throw new UncheckedIOException(e);
088            }
089        });
090
091        // uncomment to generate dirty trees
092        // session.setDependencyGraphTransformer( null );
093
094        return result;
095    }
096
097    public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
098        return new ArrayList<>(Collections.singletonList(newCentralRepository()));
099    }
100
101    private static RemoteRepository newCentralRepository() {
102        return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
103    }
104}