View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
17   */
18  package org.jadira.dependencynavigator.controller;
19  
20  import org.apache.maven.model.Dependency;
21  import org.apache.maven.model.Exclusion;
22  import org.apache.maven.model.Model;
23  import org.jadira.dependencynavigator.assembler.Assembler;
24  import org.jadira.dependencynavigator.assembler.AssemblerException;
25  import org.jadira.dependencynavigator.controller.model.DuplicatesListManager;
26  import org.jadira.dependencynavigator.implementations.IProgressMeter;
27  import org.jadira.dependencynavigator.implementations.Repository;
28  import org.jadira.dependencynavigator.model.Artifact;
29  import org.jadira.dependencynavigator.model.ArtifactInitialisationException;
30  import org.jadira.dependencynavigator.model.LeafDependency;
31  import org.jadira.dependencynavigator.model.PomFile;
32  
33  import java.util.Stack;
34  
35  public class ArtifactResolver {
36  
37      public static final String ROLE = "org.jadira.dependencynavigator.controller.ArtifactResolver";
38  
39      private Controller controller;
40      private Assembler assembler;
41      private DuplicatesListManager duplicatesListManager;
42      private Repository repository;
43  
44      /**
45       * @param dependencyHeirarchyParent - the pom file containing the dependency
46       * @param progress
47       * @param path
48       * @param dependency - the dependency requiring resolution
49       * @return the artifact indicated by this dependency information or null if the artifact has been excluded by the
50       *         scope filters or exclusions
51       * @throws DependencyResolutionException if smth is out of order
52       */
53      public Artifact resolveArtifact(PomFile dependencyHeirarchyParent, Dependency dependency, IProgressMeter progress, Stack<LeafDependency> path) throws DependencyResolutionException {
54          String groupId = dependency.getGroupId() == null ? dependencyHeirarchyParent.getGroupId() : dependency.getGroupId();
55          String artifactId = dependency.getArtifactId();
56          String version = dependency.getVersion() == null ? dependencyHeirarchyParent.getGroupId() : dependency.getGroupId();
57  
58          // resolve scope and exclude if scope is not being shown
59          String scope = dependency.getScope();
60          if (isNullOrEmpty(scope)) {
61              scope = Artifact.DEFAULT_SCOPE;
62          }
63          if (!controller.includeScope(scope)) {
64              return null;
65          }
66          if (controller.getResolveExclusions().getValue()) {
67              if (dependencyHeirarchyParent.isExcluded(groupId, artifactId, new Stack<Artifact>())) {
68                  return null;
69              }
70          }
71  
72          // attempt to locate a pom file for this dependency
73          try {
74              Model model = repository.resolveFile(groupId, artifactId, version);
75              if (model == null) {
76                  // if a corresponsing pom file does not exist then add the
77                  // dependancy as a leaf node
78                  LeafDependency leafDependency = new LeafDependency(dependencyHeirarchyParent, dependency, scope);
79                  duplicatesListManager.add(leafDependency);
80                  return leafDependency;
81              }
82              // if a pom file exists then add as a non-leaf node
83              PomFile pomFile = new PomFile(assembler.assemble(model, repository), dependencyHeirarchyParent, scope);
84              addPomToDuplicatesList(pomFile);
85              if (controller.getResolveExclusions().getValue()) {
86                  addExclusions(pomFile, dependency);
87              }
88              pomFile.resolveDependencies(this, progress, path);
89              return pomFile;
90          } catch (ArtifactInitialisationException e) {
91              throw new DependencyResolutionException(e);
92          } catch (AssemblerException e) {
93              throw new DependencyResolutionException(e);
94          }
95      }
96  
97      public void addPomToDuplicatesList(PomFile pomFile) {
98          duplicatesListManager.add(pomFile);
99      }
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 }