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.assembler;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.factory.ArtifactFactory;
29  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  import org.apache.maven.model.Dependency;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.Parent;
34  import org.apache.maven.project.ModelUtils;
35  import org.apache.maven.project.inheritance.ModelInheritanceAssembler;
36  import org.apache.maven.project.interpolation.ModelInterpolationException;
37  import org.apache.maven.project.interpolation.ModelInterpolator;
38  import org.jadira.dependencynavigator.implementations.Repository;
39  
40  /**
41   * Provides pom inheritance & properties support
42   */
43  public class Assembler {
44      
45      public static final String ROLE = "org.jadira.dependencynavigator.assembler.Assembler";
46  
47      private ModelInheritanceAssembler assembler;
48      private ModelInterpolator interpolator;
49      private ArtifactFactory artifactFactory;
50  
51      /**
52       * create a list of pom parents starting with the raw pom
53       */
54      private void lookupLineage(Model child, Repository repository, List<Model> lineage) {
55          if (child == null) {
56              return;
57          }
58          lineage.add(child);
59          Parent parentStub = child.getParent();
60          if (parentStub == null) {
61              return;
62          }
63          Model parent = repository.resolveFile(parentStub.getGroupId(), parentStub.getArtifactId(), parentStub.getVersion());
64          lookupLineage(parent, repository, lineage);
65      }
66  
67      /**
68       * work down the pom parents, merging the the parent into the child
69       */
70      private Model assemble(Model parent, List<Model> lineage, Repository repository) {
71          if (lineage.size() == 0) {
72              return parent;
73          }
74          Model child = (Model) lineage.remove(0);
75          assembler.assembleModelInheritance(child, parent);
76          return assemble(child, lineage, repository);
77      }
78  
79      private Model dependencyManagement(Model interpolated) throws AssemblerException {
80          try {
81              if (interpolated.getDependencyManagement() == null) {
82                  return interpolated;
83              }
84              Model managed = ModelUtils.cloneModel(interpolated);
85              Map<String, Dependency> managees = new HashMap<String, Dependency>();
86              for (Iterator<Dependency> iter = managed.getDependencies().iterator(); iter.hasNext();) {
87                  Dependency dependency = (Dependency) iter.next();
88                  managees.put(dependency.getGroupId() + ":" + dependency.getArtifactId(), dependency);
89              }
90              for (Iterator<Dependency> iter = interpolated.getDependencyManagement().getDependencies().iterator(); iter.hasNext();) {
91                  Dependency manager = (Dependency) iter.next();
92                  VersionRange versionRange = VersionRange.createFromVersionSpec(manager.getVersion());
93                  Artifact artifact = artifactFactory.createDependencyArtifact(manager.getGroupId(), manager.getArtifactId(), versionRange, manager.getType(), manager.getClassifier(), manager
94                          .getScope(), manager.isOptional());
95                  Dependency managee = (Dependency) managees.get(manager.getGroupId() + ":" + manager.getArtifactId());
96                  if (managee != null) {
97                      managee.setVersion(artifact.getVersion());
98                  }
99              }
100             return managed;
101         } catch (InvalidVersionSpecificationException e) {
102             throw new AssemblerException(e);
103         }
104     }
105 
106     public AssembledModel assemble(Model raw, Repository repository) throws AssemblerException {
107         try {
108             List<Model> lineage = new ArrayList<Model>();
109             lookupLineage(raw, repository, lineage);
110             Collections.reverse(lineage);
111             Model root = (Model) lineage.remove(0);
112             Model assembled = assemble(root, lineage, repository);
113             Model interpolated = interpolator.interpolate(assembled, new HashMap<String, Object>());
114             Model managed = dependencyManagement(interpolated);
115             return new AssembledModel(managed);
116         } catch (ModelInterpolationException e) {
117             throw new AssemblerException(e);
118         }
119     }
120 }