001/*
002 * Copyright 2012-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.cli.compiler.grape;
018
019import java.io.File;
020import java.util.Arrays;
021
022import org.eclipse.aether.DefaultRepositorySystemSession;
023import org.eclipse.aether.RepositorySystem;
024import org.eclipse.aether.repository.LocalRepository;
025import org.eclipse.aether.repository.LocalRepositoryManager;
026import org.eclipse.aether.repository.ProxySelector;
027import org.eclipse.aether.util.repository.JreProxySelector;
028
029import org.springframework.util.StringUtils;
030
031/**
032 * A {@link RepositorySystemSessionAutoConfiguration} that, in the absence of any
033 * configuration, applies sensible defaults.
034 *
035 * @author Andy Wilkinson
036 */
037public class DefaultRepositorySystemSessionAutoConfiguration
038                implements RepositorySystemSessionAutoConfiguration {
039
040        @Override
041        public void apply(DefaultRepositorySystemSession session,
042                        RepositorySystem repositorySystem) {
043
044                if (session.getLocalRepositoryManager() == null) {
045                        LocalRepository localRepository = new LocalRepository(getM2RepoDirectory());
046                        LocalRepositoryManager localRepositoryManager = repositorySystem
047                                        .newLocalRepositoryManager(session, localRepository);
048                        session.setLocalRepositoryManager(localRepositoryManager);
049                }
050
051                ProxySelector existing = session.getProxySelector();
052                if (existing == null || !(existing instanceof CompositeProxySelector)) {
053                        JreProxySelector fallback = new JreProxySelector();
054                        ProxySelector selector = existing == null ? fallback
055                                        : new CompositeProxySelector(Arrays.asList(existing, fallback));
056                        session.setProxySelector(selector);
057                }
058        }
059
060        private File getM2RepoDirectory() {
061                return new File(getDefaultM2HomeDirectory(), "repository");
062        }
063
064        private File getDefaultM2HomeDirectory() {
065                String mavenRoot = System.getProperty("maven.home");
066                if (StringUtils.hasLength(mavenRoot)) {
067                        return new File(mavenRoot);
068                }
069                return new File(System.getProperty("user.home"), ".m2");
070        }
071
072}