001/*
002 * Copyright 2012-2018 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.dependencies;
018
019import org.springframework.util.StringUtils;
020
021/**
022 * {@link ArtifactCoordinatesResolver} backed by
023 * {@link SpringBootDependenciesDependencyManagement}.
024 *
025 * @author Phillip Webb
026 * @author Andy Wilkinson
027 */
028public class DependencyManagementArtifactCoordinatesResolver
029                implements ArtifactCoordinatesResolver {
030
031        private final DependencyManagement dependencyManagement;
032
033        public DependencyManagementArtifactCoordinatesResolver() {
034                this(new SpringBootDependenciesDependencyManagement());
035        }
036
037        public DependencyManagementArtifactCoordinatesResolver(
038                        DependencyManagement dependencyManagement) {
039                this.dependencyManagement = dependencyManagement;
040        }
041
042        @Override
043        public String getGroupId(String artifactId) {
044                Dependency dependency = find(artifactId);
045                return (dependency != null) ? dependency.getGroupId() : null;
046        }
047
048        @Override
049        public String getArtifactId(String id) {
050                Dependency dependency = find(id);
051                return (dependency != null) ? dependency.getArtifactId() : null;
052        }
053
054        private Dependency find(String id) {
055                if (StringUtils.countOccurrencesOf(id, ":") == 2) {
056                        String[] tokens = id.split(":");
057                        return new Dependency(tokens[0], tokens[1], tokens[2]);
058                }
059                if (id != null) {
060                        if (id.startsWith("spring-boot")) {
061                                return new Dependency("org.springframework.boot", id,
062                                                this.dependencyManagement.getSpringBootVersion());
063                        }
064                        return this.dependencyManagement.find(id);
065                }
066                return null;
067        }
068
069        @Override
070        public String getVersion(String module) {
071                Dependency dependency = find(module);
072                return (dependency != null) ? dependency.getVersion() : null;
073        }
074
075}