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