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.Collection;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Stack;
26  
27  import org.apache.maven.artifact.Artifact;
28  
29  import org.apache.geronimo.genesis.dependency.DependencyTree.Node;
30  
31  //
32  // NOTE: Lifetd from the maven-project-info-plugin
33  //
34  
35  /**
36   * ???
37   * 
38   * @version $Rev: 454266 $ $Date: 2006-10-08 20:20:03 -0700 (Sun, 08 Oct 2006) $
39   */
40  public class DependencyResolutionListener
41      extends ResolutionListenerAdapter
42  {
43      private DependencyTree tree = new DependencyTree();
44  
45      private int currentDepth = 0;
46  
47      private Stack parents = new Stack();
48  
49      private Map artifacts = new HashMap();
50  
51      public DependencyTree getDependencyTree() {
52          return tree;
53      }
54  
55      public Collection getArtifacts() {
56          return artifacts.values();
57      }
58      
59      //
60      // ResolutionListener
61      //
62  
63      public void startProcessChildren(final Artifact artifact) {
64          Node node = (Node) artifacts.get(artifact.getDependencyConflictId());
65  
66          node.depth = currentDepth++;
67          if (parents.isEmpty()) {
68              tree.rootNode = node;
69          }
70  
71          parents.push(node);
72      }
73  
74      public void endProcessChildren(final Artifact artifact) {
75          Node node = (Node) parents.pop();
76          assert artifact.equals(node.artifact);
77          currentDepth--;
78      }
79  
80      public void omitForNearer(final Artifact omitted, final Artifact kept) {
81          assert omitted.getDependencyConflictId().equals(kept.getDependencyConflictId());
82  
83          Node prev = (Node) artifacts.get(omitted.getDependencyConflictId());
84          if (prev != null) {
85              if (prev.parent != null) {
86                  prev.parent.children.remove(prev);
87              }
88              artifacts.remove(omitted.getDependencyConflictId());
89          }
90  
91          includeArtifact(kept);
92      }
93  
94      public void omitForCycle(final Artifact artifact) {
95          // intentionally blank
96      }
97  
98      public void includeArtifact(final Artifact artifact) {
99          if (artifacts.containsKey(artifact.getDependencyConflictId())) {
100             Node prev = (Node) artifacts.get(artifact.getDependencyConflictId());
101             if (prev.parent != null) {
102                 prev.parent.children.remove(prev);
103             }
104             artifacts.remove(artifact.getDependencyConflictId());
105         }
106 
107         Node node = new Node();
108         node.artifact = artifact;
109         if (!parents.isEmpty()) {
110             node.parent = (Node) parents.peek();
111             node.parent.children.add(node);
112             node.depth = currentDepth;
113         }
114         artifacts.put(artifact.getDependencyConflictId(), node);
115     }
116 
117     public void updateScope(final Artifact artifact, final String scope) {
118         Node node = (Node) artifacts.get(artifact.getDependencyConflictId());
119         node.artifact.setScope(scope);
120     }
121 
122     public void manageArtifact(final Artifact artifact, final Artifact replacement) {
123         Node node = (Node) artifacts.get(artifact.getDependencyConflictId());
124 
125         if (node != null) {
126             if (replacement.getVersion() != null) {
127                 node.artifact.setVersion(replacement.getVersion());
128             }
129             if (replacement.getScope() != null) {
130                 node.artifact.setScope(replacement.getScope());
131             }
132         }
133     }
134 }