View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.genesis.dependency;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.ArtifactUtils;
30  
31  import org.apache.maven.project.MavenProject;
32  
33  import org.apache.geronimo.genesis.dependency.DependencyTree.Node;
34  
35  //
36  // NOTE: Lifetd from the maven-project-info-plugin
37  //
38  
39  /**
40   * ???
41   *
42   * @version $Rev: 454266 $ $Date: 2006-10-08 20:20:03 -0700 (Sun, 08 Oct 2006) $
43   */
44  public class Dependencies
45  {
46      private List projectDependencies;
47  
48      private DependencyResolutionListener resolvedDependencies;
49  
50      public Dependencies(final MavenProject project, final DependencyResolutionListener listener) {
51          assert project != null;
52          assert listener != null;
53  
54          this.projectDependencies = listener.getDependencyTree().getRootNode().getChildren();
55          this.resolvedDependencies = listener;
56  
57          //
58          // Workaround to ensure proper File objects in the Artifacts from the DependencyResolutionListener
59          //
60          Map projectMap = new HashMap();
61          Iterator iter = project.getArtifacts().iterator();
62  
63          while (iter.hasNext()) {
64              Artifact artifact = (Artifact) iter.next();
65              projectMap.put(ArtifactUtils.versionlessKey(artifact), artifact);
66          }
67  
68          mapArtifactFiles(listener.getDependencyTree().getRootNode(), projectMap);
69      }
70  
71      private void mapArtifactFiles(final Node node, final Map projectMap) {
72          assert node != null;
73          assert projectMap != null;
74  
75          List childs = node.getChildren();
76          if ((childs == null) || childs.isEmpty()) {
77              return;
78          }
79  
80          Iterator iter = childs.iterator();
81          while (iter.hasNext()) {
82              Node anode = (Node) iter.next();
83              String key = ArtifactUtils.versionlessKey(anode.getArtifact());
84              Artifact projartifact = (Artifact) projectMap.get(key);
85              if (projartifact != null) {
86                  anode.getArtifact().setFile(projartifact.getFile());
87              }
88  
89              mapArtifactFiles(anode, projectMap);
90          }
91      }
92  
93      public boolean hasDependencies() {
94          return (projectDependencies != null) && (!this.projectDependencies.isEmpty());
95      }
96  
97      public List getProjectDependencies() {
98          return new ArrayList(projectDependencies);
99      }
100 
101     public List getTransitiveDependencies() {
102         List deps = new ArrayList(resolvedDependencies.getArtifacts());
103         deps.removeAll(projectDependencies);
104         return deps;
105     }
106 
107     public List getAllDependencies() {
108         List deps = new ArrayList();
109 
110         for (Iterator iter = resolvedDependencies.getArtifacts().iterator(); iter.hasNext();) {
111             Node node = (Node) iter.next();
112             Artifact artifact = node.getArtifact();
113             deps.add(artifact);
114         }
115         return deps;
116     }
117 
118     public Map getDependenciesByScope() {
119         Map dependenciesByScope = new HashMap();
120         for (Iterator i = getAllDependencies().iterator(); i.hasNext();) {
121             Artifact artifact = (Artifact) i.next();
122 
123             List multiValue = (List) dependenciesByScope.get(artifact.getScope());
124             if (multiValue == null) {
125                 multiValue = new ArrayList();
126             }
127             multiValue.add(artifact);
128             dependenciesByScope.put(artifact.getScope(), multiValue);
129         }
130         return dependenciesByScope;
131     }
132 
133     public Node getResolvedRoot() {
134         return resolvedDependencies.getDependencyTree().getRootNode();
135     }
136 }