001 /* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 /* 015 * This file has been modified by Chris Pheby in accordance with Section 4.2 016 * of the Apache Software License 017 */ 018 package org.jadira.dependencynavigator.controller; 019 020 import org.apache.maven.model.Dependency; 021 import org.apache.maven.model.Exclusion; 022 import org.apache.maven.model.Model; 023 import org.jadira.dependencynavigator.assembler.Assembler; 024 import org.jadira.dependencynavigator.assembler.AssemblerException; 025 import org.jadira.dependencynavigator.controller.model.DuplicatesListManager; 026 import org.jadira.dependencynavigator.implementations.IProgressMeter; 027 import org.jadira.dependencynavigator.implementations.Repository; 028 import org.jadira.dependencynavigator.model.Artifact; 029 import org.jadira.dependencynavigator.model.ArtifactInitialisationException; 030 import org.jadira.dependencynavigator.model.LeafDependency; 031 import org.jadira.dependencynavigator.model.PomFile; 032 033 import java.util.Stack; 034 035 public class ArtifactResolver { 036 037 public static final String ROLE = "org.jadira.dependencynavigator.controller.ArtifactResolver"; 038 039 private Controller controller; 040 private Assembler assembler; 041 private DuplicatesListManager duplicatesListManager; 042 private Repository repository; 043 044 /** 045 * @param dependencyHeirarchyParent - the pom file containing the dependency 046 * @param progress 047 * @param path 048 * @param dependency - the dependency requiring resolution 049 * @return the artifact indicated by this dependency information or null if the artifact has been excluded by the 050 * scope filters or exclusions 051 * @throws DependencyResolutionException if smth is out of order 052 */ 053 public Artifact resolveArtifact(PomFile dependencyHeirarchyParent, Dependency dependency, IProgressMeter progress, Stack<LeafDependency> path) throws DependencyResolutionException { 054 String groupId = dependency.getGroupId() == null ? dependencyHeirarchyParent.getGroupId() : dependency.getGroupId(); 055 String artifactId = dependency.getArtifactId(); 056 String version = dependency.getVersion() == null ? dependencyHeirarchyParent.getGroupId() : dependency.getGroupId(); 057 058 // resolve scope and exclude if scope is not being shown 059 String scope = dependency.getScope(); 060 if (isNullOrEmpty(scope)) { 061 scope = Artifact.DEFAULT_SCOPE; 062 } 063 if (!controller.includeScope(scope)) { 064 return null; 065 } 066 if (controller.getResolveExclusions().getValue()) { 067 if (dependencyHeirarchyParent.isExcluded(groupId, artifactId, new Stack<Artifact>())) { 068 return null; 069 } 070 } 071 072 // attempt to locate a pom file for this dependency 073 try { 074 Model model = repository.resolveFile(groupId, artifactId, version); 075 if (model == null) { 076 // if a corresponsing pom file does not exist then add the 077 // dependancy as a leaf node 078 LeafDependency leafDependency = new LeafDependency(dependencyHeirarchyParent, dependency, scope); 079 duplicatesListManager.add(leafDependency); 080 return leafDependency; 081 } 082 // if a pom file exists then add as a non-leaf node 083 PomFile pomFile = new PomFile(assembler.assemble(model, repository), dependencyHeirarchyParent, scope); 084 addPomToDuplicatesList(pomFile); 085 if (controller.getResolveExclusions().getValue()) { 086 addExclusions(pomFile, dependency); 087 } 088 pomFile.resolveDependencies(this, progress, path); 089 return pomFile; 090 } catch (ArtifactInitialisationException e) { 091 throw new DependencyResolutionException(e); 092 } catch (AssemblerException e) { 093 throw new DependencyResolutionException(e); 094 } 095 } 096 097 public void addPomToDuplicatesList(PomFile pomFile) { 098 duplicatesListManager.add(pomFile); 099 } 100 101 private void addExclusions(Artifact artifact, Dependency dependency) { 102 for (Object o : dependency.getExclusions()) { 103 Exclusion exclusion = (Exclusion) o; 104 artifact.addExclusion(exclusion.getGroupId(), exclusion.getArtifactId()); 105 } 106 } 107 108 private boolean isNullOrEmpty(String value) { 109 return value == null || value.trim().length() == 0; 110 } 111 }