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.ArrayList; 023 import java.util.HashMap; 024 import java.util.Iterator; 025 import java.util.List; 026 import java.util.Map; 027 028 import org.apache.maven.artifact.Artifact; 029 import org.apache.maven.artifact.ArtifactUtils; 030 031 import org.apache.maven.project.MavenProject; 032 033 import org.apache.geronimo.genesis.dependency.DependencyTree.Node; 034 035 // 036 // NOTE: Lifetd from the maven-project-info-plugin 037 // 038 039 /** 040 * ??? 041 * 042 * @version $Rev: 454266 $ $Date: 2006-10-08 20:20:03 -0700 (Sun, 08 Oct 2006) $ 043 */ 044 public class Dependencies 045 { 046 private List projectDependencies; 047 048 private DependencyResolutionListener resolvedDependencies; 049 050 public Dependencies(final MavenProject project, final DependencyResolutionListener listener) { 051 assert project != null; 052 assert listener != null; 053 054 this.projectDependencies = listener.getDependencyTree().getRootNode().getChildren(); 055 this.resolvedDependencies = listener; 056 057 // 058 // Workaround to ensure proper File objects in the Artifacts from the DependencyResolutionListener 059 // 060 Map projectMap = new HashMap(); 061 Iterator iter = project.getArtifacts().iterator(); 062 063 while (iter.hasNext()) { 064 Artifact artifact = (Artifact) iter.next(); 065 projectMap.put(ArtifactUtils.versionlessKey(artifact), artifact); 066 } 067 068 mapArtifactFiles(listener.getDependencyTree().getRootNode(), projectMap); 069 } 070 071 private void mapArtifactFiles(final Node node, final Map projectMap) { 072 assert node != null; 073 assert projectMap != null; 074 075 List childs = node.getChildren(); 076 if ((childs == null) || childs.isEmpty()) { 077 return; 078 } 079 080 Iterator iter = childs.iterator(); 081 while (iter.hasNext()) { 082 Node anode = (Node) iter.next(); 083 String key = ArtifactUtils.versionlessKey(anode.getArtifact()); 084 Artifact projartifact = (Artifact) projectMap.get(key); 085 if (projartifact != null) { 086 anode.getArtifact().setFile(projartifact.getFile()); 087 } 088 089 mapArtifactFiles(anode, projectMap); 090 } 091 } 092 093 public boolean hasDependencies() { 094 return (projectDependencies != null) && (!this.projectDependencies.isEmpty()); 095 } 096 097 public List getProjectDependencies() { 098 return new ArrayList(projectDependencies); 099 } 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 }