001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.genesis.dependency;
021    
022    import java.util.Collection;
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.Stack;
026    
027    import org.apache.maven.artifact.Artifact;
028    
029    import org.apache.geronimo.genesis.dependency.DependencyTree.Node;
030    
031    //
032    // NOTE: Lifetd from the maven-project-info-plugin
033    //
034    
035    /**
036     * ???
037     * 
038     * @version $Rev: 454266 $ $Date: 2006-10-08 20:20:03 -0700 (Sun, 08 Oct 2006) $
039     */
040    public class DependencyResolutionListener
041        extends ResolutionListenerAdapter
042    {
043        private DependencyTree tree = new DependencyTree();
044    
045        private int currentDepth = 0;
046    
047        private Stack parents = new Stack();
048    
049        private Map artifacts = new HashMap();
050    
051        public DependencyTree getDependencyTree() {
052            return tree;
053        }
054    
055        public Collection getArtifacts() {
056            return artifacts.values();
057        }
058        
059        //
060        // ResolutionListener
061        //
062    
063        public void startProcessChildren(final Artifact artifact) {
064            Node node = (Node) artifacts.get(artifact.getDependencyConflictId());
065    
066            node.depth = currentDepth++;
067            if (parents.isEmpty()) {
068                tree.rootNode = node;
069            }
070    
071            parents.push(node);
072        }
073    
074        public void endProcessChildren(final Artifact artifact) {
075            Node node = (Node) parents.pop();
076            assert artifact.equals(node.artifact);
077            currentDepth--;
078        }
079    
080        public void omitForNearer(final Artifact omitted, final Artifact kept) {
081            assert omitted.getDependencyConflictId().equals(kept.getDependencyConflictId());
082    
083            Node prev = (Node) artifacts.get(omitted.getDependencyConflictId());
084            if (prev != null) {
085                if (prev.parent != null) {
086                    prev.parent.children.remove(prev);
087                }
088                artifacts.remove(omitted.getDependencyConflictId());
089            }
090    
091            includeArtifact(kept);
092        }
093    
094        public void omitForCycle(final Artifact artifact) {
095            // intentionally blank
096        }
097    
098        public void includeArtifact(final Artifact artifact) {
099            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    }