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.model;
19  
20  import org.apache.maven.model.Dependency;
21  import org.jadira.dependencynavigator.assembler.AssembledModel;
22  import org.jadira.dependencynavigator.controller.ArtifactResolver;
23  import org.jadira.dependencynavigator.implementations.IProgressMeter;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Stack;
28  
29  public class PomFile extends Artifact {
30  
31      protected List<Artifact> dependencies;
32      protected AssembledModel model;
33  
34      public PomFile(AssembledModel model, Artifact parent, String scope) throws ArtifactInitialisationException {
35          
36          super(parent, 
37                  model.getAssembledModel().getGroupId() == null ? model.getAssembledModel().getParent().getGroupId() : model.getAssembledModel().getGroupId(), 
38                  model.getAssembledModel().getArtifactId(), 
39                  model.getAssembledModel().getVersion() == null ? model.getAssembledModel().getParent().getVersion() : model.getAssembledModel().getVersion(), 
40                  scope);
41          
42          this.model = model;
43      }
44  
45      public void resolveDependencies(ArtifactResolver resolver, IProgressMeter progress, Stack<LeafDependency> path) {
46          progress.resolving(this);
47          dependencies = new ArrayList<Artifact>();
48          // add the dependencies
49          for (Object o : model.getAssembledModel().getDependencies()) {
50              Dependency dependency = (Dependency) o;
51              /*
52               * Check to see if dependency contains in path
53               */
54              try {
55                  LeafDependency leafDependency = new LeafDependency(this, dependency, dependency.getScope());
56                  /**
57                   * Prevent circular dependencies
58                   */
59                  if (!path.contains(leafDependency)) {
60                      path.push(leafDependency);
61                      Artifact artifact = resolver.resolveArtifact(this, dependency, progress, path);
62                      if (artifact != null) {
63                          dependencies.add(artifact);
64                      }
65                  }
66              } catch (ArtifactInitialisationException e) {
67                  e.printStackTrace();
68              }
69          }
70      }
71  
72      public AssembledModel getModel() {
73          return model;
74      }
75  
76      @Override
77      public boolean isLeaf() {
78          return dependencies.size() == 0;
79      }
80  
81      @Override
82      public List<Artifact> getDependencies() {
83          return dependencies;
84      }
85  
86      @Override
87      public int dependencyCount() {
88          if (isLeaf()) {
89              return 1;
90          }
91          int dependencyCount = 0;
92          for (Artifact artifact : dependencies) {
93              dependencyCount += artifact.dependencyCount();
94          }
95          return dependencyCount;
96      }
97  
98  }