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.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
026import org.eclipse.aether.DefaultRepositorySystemSession;
027import org.eclipse.aether.RepositorySystem;
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.repository.LocalRepository;
030import org.eclipse.aether.repository.RemoteRepository;
031import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
032
033/**
034 * A helper to boot the repository system and a repository system session.
035 */
036public class Booter {
037    public static final String SERVICE_LOCATOR = "serviceLocator";
038
039    public static final String SUPPLIER = "supplier";
040
041    public static final String GUICE = "guice";
042
043    public static final String SISU = "sisu";
044
045    public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println);
046
047    public static String selectFactory(String[] args) {
048        if (args == null || args.length == 0) {
049            return SUPPLIER;
050        } else {
051            return args[0];
052        }
053    }
054
055    public static RepositorySystem newRepositorySystem(final String factory) {
056        switch (factory) {
057            case SERVICE_LOCATOR:
058                return org.apache.maven.resolver.examples.manual.ManualRepositorySystemFactory.newRepositorySystem();
059            case SUPPLIER:
060                return org.apache.maven.resolver.examples.supplier.SupplierRepositorySystemFactory
061                        .newRepositorySystem();
062            case GUICE:
063                return org.apache.maven.resolver.examples.guice.GuiceRepositorySystemFactory.newRepositorySystem();
064            case SISU:
065                return org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory.newRepositorySystem();
066            default:
067                throw new IllegalArgumentException("Unknown factory: " + factory);
068        }
069    }
070
071    public static DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system) {
072        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
073
074        LocalRepository localRepo = new LocalRepository("target/local-repo");
075        session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
076
077        session.setTransferListener(new ConsoleTransferListener());
078        session.setRepositoryListener(new ConsoleRepositoryListener());
079
080        // uncomment to generate dirty trees
081        // session.setDependencyGraphTransformer( null );
082
083        return session;
084    }
085
086    public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
087        return new ArrayList<>(Collections.singletonList(newCentralRepository()));
088    }
089
090    private static RemoteRepository newCentralRepository() {
091        return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
092    }
093}