Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 1 | package org.apache.maven.shared.dependency.tree; |
| 2 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 3 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 4 | /* |
| 5 | * Licensed to the Apache Software Foundation (ASF) under one |
| 6 | * or more contributor license agreements. See the NOTICE file |
| 7 | * distributed with this work for additional information |
| 8 | * regarding copyright ownership. The ASF licenses this file |
| 9 | * to you under the Apache License, Version 2.0 (the |
| 10 | * "License"); you may not use this file except in compliance |
| 11 | * with the License. You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, |
| 16 | * software distributed under the License is distributed on an |
| 17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 | * KIND, either express or implied. See the License for the |
| 19 | * specific language governing permissions and limitations |
| 20 | * under the License. |
| 21 | */ |
| 22 | |
| 23 | import java.util.Collection; |
| 24 | import java.util.Collections; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.IdentityHashMap; |
| 27 | import java.util.Iterator; |
| 28 | import java.util.Map; |
| 29 | import java.util.Stack; |
| 30 | |
| 31 | import org.apache.maven.artifact.Artifact; |
| 32 | import org.apache.maven.artifact.resolver.ResolutionListener; |
| 33 | import org.apache.maven.artifact.resolver.ResolutionListenerForDepMgmt; |
| 34 | import org.apache.maven.artifact.versioning.VersionRange; |
| 35 | import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor; |
| 36 | import org.codehaus.plexus.logging.Logger; |
| 37 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 38 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 39 | /** |
| 40 | * An artifact resolution listener that constructs a dependency tree. |
| 41 | * |
| 42 | * @author Edwin Punzalan |
| 43 | * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a> |
| 44 | * @version $Id: DependencyTreeResolutionListener.java 661727 2008-05-30 14:21:49Z bentmann $ |
| 45 | */ |
| 46 | public class DependencyTreeResolutionListener implements ResolutionListener, ResolutionListenerForDepMgmt |
| 47 | { |
| 48 | // fields ----------------------------------------------------------------- |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 49 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 50 | /** |
| 51 | * The log to write debug messages to. |
| 52 | */ |
| 53 | private final Logger logger; |
| 54 | |
| 55 | /** |
| 56 | * The parent dependency nodes of the current dependency node. |
| 57 | */ |
| 58 | private final Stack parentNodes; |
| 59 | |
| 60 | /** |
| 61 | * A map of dependency nodes by their attached artifact. |
| 62 | */ |
| 63 | private final Map nodesByArtifact; |
| 64 | |
| 65 | /** |
| 66 | * The root dependency node of the computed dependency tree. |
| 67 | */ |
| 68 | private DependencyNode rootNode; |
| 69 | |
| 70 | /** |
| 71 | * The dependency node currently being processed by this listener. |
| 72 | */ |
| 73 | private DependencyNode currentNode; |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 74 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Map < String replacementId, String premanaged version > |
| 77 | */ |
| 78 | private Map managedVersions = new HashMap(); |
| 79 | |
| 80 | /** |
| 81 | * Map < String replacementId, String premanaged scope > |
| 82 | */ |
| 83 | private Map managedScopes = new HashMap(); |
| 84 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 85 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 86 | // constructors ----------------------------------------------------------- |
| 87 | |
| 88 | /** |
| 89 | * Creates a new dependency tree resolution listener that writes to the specified log. |
| 90 | * |
| 91 | * @param logger |
| 92 | * the log to write debug messages to |
| 93 | */ |
| 94 | public DependencyTreeResolutionListener( Logger logger ) |
| 95 | { |
| 96 | this.logger = logger; |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 97 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 98 | parentNodes = new Stack(); |
| 99 | nodesByArtifact = new IdentityHashMap(); |
| 100 | rootNode = null; |
| 101 | currentNode = null; |
| 102 | } |
| 103 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 104 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 105 | // ResolutionListener methods --------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * {@inheritDoc} |
| 109 | */ |
| 110 | public void testArtifact( Artifact artifact ) |
| 111 | { |
| 112 | log( "testArtifact: artifact=" + artifact ); |
| 113 | } |
| 114 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 115 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 116 | /** |
| 117 | * {@inheritDoc} |
| 118 | */ |
| 119 | public void startProcessChildren( Artifact artifact ) |
| 120 | { |
| 121 | log( "startProcessChildren: artifact=" + artifact ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 122 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 123 | if ( !currentNode.getArtifact().equals( artifact ) ) |
| 124 | { |
| 125 | throw new IllegalStateException( "Artifact was expected to be " + currentNode.getArtifact() + " but was " |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 126 | + artifact ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | parentNodes.push( currentNode ); |
| 130 | } |
| 131 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 132 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 133 | /** |
| 134 | * {@inheritDoc} |
| 135 | */ |
| 136 | public void endProcessChildren( Artifact artifact ) |
| 137 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 138 | DependencyNode node = ( DependencyNode ) parentNodes.pop(); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 139 | |
| 140 | log( "endProcessChildren: artifact=" + artifact ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 141 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 142 | if ( node == null ) |
| 143 | { |
| 144 | throw new IllegalStateException( "Parent dependency node was null" ); |
| 145 | } |
| 146 | |
| 147 | if ( !node.getArtifact().equals( artifact ) ) |
| 148 | { |
| 149 | throw new IllegalStateException( "Parent dependency node artifact was expected to be " + node.getArtifact() |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 150 | + " but was " + artifact ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 154 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 155 | /** |
| 156 | * {@inheritDoc} |
| 157 | */ |
| 158 | public void includeArtifact( Artifact artifact ) |
| 159 | { |
| 160 | log( "includeArtifact: artifact=" + artifact ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 161 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 162 | DependencyNode existingNode = getNode( artifact ); |
| 163 | |
| 164 | /* |
| 165 | * Ignore duplicate includeArtifact calls since omitForNearer can be called prior to includeArtifact on the same |
| 166 | * artifact, and we don't wish to include it twice. |
| 167 | */ |
| 168 | if ( existingNode == null && isCurrentNodeIncluded() ) |
| 169 | { |
| 170 | DependencyNode node = addNode( artifact ); |
| 171 | |
| 172 | /* |
| 173 | * Add the dependency management information cached in any prior manageArtifact calls, since includeArtifact |
| 174 | * is always called after manageArtifact. |
| 175 | */ |
| 176 | flushDependencyManagement( node ); |
| 177 | } |
| 178 | } |
| 179 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 180 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 181 | /** |
| 182 | * {@inheritDoc} |
| 183 | */ |
| 184 | public void omitForNearer( Artifact omitted, Artifact kept ) |
| 185 | { |
| 186 | log( "omitForNearer: omitted=" + omitted + " kept=" + kept ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 187 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 188 | if ( !omitted.getDependencyConflictId().equals( kept.getDependencyConflictId() ) ) |
| 189 | { |
| 190 | throw new IllegalArgumentException( "Omitted artifact dependency conflict id " |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 191 | + omitted.getDependencyConflictId() + " differs from kept artifact dependency conflict id " |
| 192 | + kept.getDependencyConflictId() ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if ( isCurrentNodeIncluded() ) |
| 196 | { |
| 197 | DependencyNode omittedNode = getNode( omitted ); |
| 198 | |
| 199 | if ( omittedNode != null ) |
| 200 | { |
| 201 | removeNode( omitted ); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | omittedNode = createNode( omitted ); |
| 206 | |
| 207 | currentNode = omittedNode; |
| 208 | } |
| 209 | |
| 210 | omittedNode.omitForConflict( kept ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 211 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 212 | /* |
| 213 | * Add the dependency management information cached in any prior manageArtifact calls, since omitForNearer |
| 214 | * is always called after manageArtifact. |
| 215 | */ |
| 216 | flushDependencyManagement( omittedNode ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 217 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 218 | DependencyNode keptNode = getNode( kept ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 219 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 220 | if ( keptNode == null ) |
| 221 | { |
| 222 | addNode( kept ); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 227 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 228 | /** |
| 229 | * {@inheritDoc} |
| 230 | */ |
| 231 | public void updateScope( Artifact artifact, String scope ) |
| 232 | { |
| 233 | log( "updateScope: artifact=" + artifact + ", scope=" + scope ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 234 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 235 | DependencyNode node = getNode( artifact ); |
| 236 | |
| 237 | if ( node == null ) |
| 238 | { |
| 239 | // updateScope events can be received prior to includeArtifact events |
| 240 | node = addNode( artifact ); |
| 241 | } |
| 242 | |
| 243 | node.setOriginalScope( artifact.getScope() ); |
| 244 | } |
| 245 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 246 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 247 | /** |
| 248 | * {@inheritDoc} |
| 249 | */ |
| 250 | public void manageArtifact( Artifact artifact, Artifact replacement ) |
| 251 | { |
| 252 | // TODO: remove when ResolutionListenerForDepMgmt merged into ResolutionListener |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 253 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 254 | log( "manageArtifact: artifact=" + artifact + ", replacement=" + replacement ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 255 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 256 | if ( replacement.getVersion() != null ) |
| 257 | { |
| 258 | manageArtifactVersion( artifact, replacement ); |
| 259 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 260 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 261 | if ( replacement.getScope() != null ) |
| 262 | { |
| 263 | manageArtifactScope( artifact, replacement ); |
| 264 | } |
| 265 | } |
| 266 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 267 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 268 | /** |
| 269 | * {@inheritDoc} |
| 270 | */ |
| 271 | public void omitForCycle( Artifact artifact ) |
| 272 | { |
| 273 | log( "omitForCycle: artifact=" + artifact ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 274 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 275 | if ( isCurrentNodeIncluded() ) |
| 276 | { |
| 277 | DependencyNode node = createNode( artifact ); |
| 278 | |
| 279 | node.omitForCycle(); |
| 280 | } |
| 281 | } |
| 282 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 283 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 284 | /** |
| 285 | * {@inheritDoc} |
| 286 | */ |
| 287 | public void updateScopeCurrentPom( Artifact artifact, String scopeIgnored ) |
| 288 | { |
| 289 | log( "updateScopeCurrentPom: artifact=" + artifact + ", scopeIgnored=" + scopeIgnored ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 290 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 291 | DependencyNode node = getNode( artifact ); |
| 292 | |
| 293 | if ( node == null ) |
| 294 | { |
| 295 | // updateScopeCurrentPom events can be received prior to includeArtifact events |
| 296 | node = addNode( artifact ); |
| 297 | // TODO remove the node that tried to impose its scope and add some info |
| 298 | } |
| 299 | |
| 300 | node.setFailedUpdateScope( scopeIgnored ); |
| 301 | } |
| 302 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 303 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 304 | /** |
| 305 | * {@inheritDoc} |
| 306 | */ |
| 307 | public void selectVersionFromRange( Artifact artifact ) |
| 308 | { |
| 309 | log( "selectVersionFromRange: artifact=" + artifact ); |
| 310 | |
| 311 | DependencyNode node = getNode( artifact ); |
| 312 | |
| 313 | /* |
| 314 | * selectVersionFromRange is called before includeArtifact |
| 315 | */ |
| 316 | if ( node == null && isCurrentNodeIncluded() ) |
| 317 | { |
| 318 | node = addNode( artifact ); |
| 319 | } |
| 320 | |
| 321 | node.setVersionSelectedFromRange( artifact.getVersionRange() ); |
| 322 | node.setAvailableVersions( artifact.getAvailableVersions() ); |
| 323 | } |
| 324 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 325 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 326 | /** |
| 327 | * {@inheritDoc} |
| 328 | */ |
| 329 | public void restrictRange( Artifact artifact, Artifact replacement, VersionRange versionRange ) |
| 330 | { |
| 331 | log( "restrictRange: artifact=" + artifact + ", replacement=" + replacement + ", versionRange=" + versionRange ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 332 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 333 | // TODO: track range restriction in node (MNG-3093) |
| 334 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 335 | |
| 336 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 337 | // ResolutionListenerForDepMgmt methods ----------------------------------- |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 338 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 339 | /** |
| 340 | * {@inheritDoc} |
| 341 | */ |
| 342 | public void manageArtifactVersion( Artifact artifact, Artifact replacement ) |
| 343 | { |
| 344 | log( "manageArtifactVersion: artifact=" + artifact + ", replacement=" + replacement ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 345 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 346 | /* |
| 347 | * DefaultArtifactCollector calls manageArtifact twice: first with the change; then subsequently with no change. |
| 348 | * We ignore the second call when the versions are equal. |
| 349 | */ |
| 350 | if ( isCurrentNodeIncluded() && !replacement.getVersion().equals( artifact.getVersion() ) ) |
| 351 | { |
| 352 | /* |
| 353 | * Cache management information and apply in includeArtifact, since DefaultArtifactCollector mutates the |
| 354 | * artifact and then calls includeArtifact after manageArtifact. |
| 355 | */ |
Stuart McCulloch | f3f38b2 | 2009-09-02 18:57:05 +0000 | [diff] [blame] | 356 | managedVersions.put( getRangeId( replacement ), artifact.getVersion() ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 360 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 361 | /** |
| 362 | * {@inheritDoc} |
| 363 | */ |
| 364 | public void manageArtifactScope( Artifact artifact, Artifact replacement ) |
| 365 | { |
| 366 | log( "manageArtifactScope: artifact=" + artifact + ", replacement=" + replacement ); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 367 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 368 | /* |
| 369 | * DefaultArtifactCollector calls manageArtifact twice: first with the change; then subsequently with no change. |
| 370 | * We ignore the second call when the scopes are equal. |
| 371 | */ |
| 372 | if ( isCurrentNodeIncluded() && !replacement.getScope().equals( artifact.getScope() ) ) |
| 373 | { |
| 374 | /* |
| 375 | * Cache management information and apply in includeArtifact, since DefaultArtifactCollector mutates the |
| 376 | * artifact and then calls includeArtifact after manageArtifact. |
| 377 | */ |
Stuart McCulloch | f3f38b2 | 2009-09-02 18:57:05 +0000 | [diff] [blame] | 378 | managedScopes.put( getRangeId( replacement ), artifact.getScope() ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 381 | |
| 382 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 383 | // public methods --------------------------------------------------------- |
| 384 | |
| 385 | /** |
| 386 | * Gets a list of all dependency nodes in the computed dependency tree. |
| 387 | * |
| 388 | * @return a list of dependency nodes |
| 389 | * @deprecated As of 1.1, use a {@link CollectingDependencyNodeVisitor} on the root dependency node |
| 390 | */ |
| 391 | public Collection getNodes() |
| 392 | { |
| 393 | return Collections.unmodifiableCollection( nodesByArtifact.values() ); |
| 394 | } |
| 395 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 396 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 397 | /** |
| 398 | * Gets the root dependency node of the computed dependency tree. |
| 399 | * |
| 400 | * @return the root node |
| 401 | */ |
| 402 | public DependencyNode getRootNode() |
| 403 | { |
| 404 | return rootNode; |
| 405 | } |
| 406 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 407 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 408 | // private methods -------------------------------------------------------- |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 409 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 410 | /** |
| 411 | * Writes the specified message to the log at debug level with indentation for the current node's depth. |
| 412 | * |
| 413 | * @param message |
| 414 | * the message to write to the log |
| 415 | */ |
| 416 | private void log( String message ) |
| 417 | { |
| 418 | int depth = parentNodes.size(); |
| 419 | |
| 420 | StringBuffer buffer = new StringBuffer(); |
| 421 | |
| 422 | for ( int i = 0; i < depth; i++ ) |
| 423 | { |
| 424 | buffer.append( " " ); |
| 425 | } |
| 426 | |
| 427 | buffer.append( message ); |
| 428 | |
| 429 | logger.debug( buffer.toString() ); |
| 430 | } |
| 431 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 432 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 433 | /** |
| 434 | * Creates a new dependency node for the specified artifact and appends it to the current parent dependency node. |
| 435 | * |
| 436 | * @param artifact |
| 437 | * the attached artifact for the new dependency node |
| 438 | * @return the new dependency node |
| 439 | */ |
| 440 | private DependencyNode createNode( Artifact artifact ) |
| 441 | { |
| 442 | DependencyNode node = new DependencyNode( artifact ); |
| 443 | |
| 444 | if ( !parentNodes.isEmpty() ) |
| 445 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 446 | DependencyNode parent = ( DependencyNode ) parentNodes.peek(); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 447 | |
| 448 | parent.addChild( node ); |
| 449 | } |
| 450 | |
| 451 | return node; |
| 452 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 453 | |
| 454 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 455 | /** |
| 456 | * Creates a new dependency node for the specified artifact, appends it to the current parent dependency node and |
| 457 | * puts it into the dependency node cache. |
| 458 | * |
| 459 | * @param artifact |
| 460 | * the attached artifact for the new dependency node |
| 461 | * @return the new dependency node |
| 462 | */ |
| 463 | // package protected for unit test |
| 464 | DependencyNode addNode( Artifact artifact ) |
| 465 | { |
| 466 | DependencyNode node = createNode( artifact ); |
| 467 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 468 | DependencyNode previousNode = ( DependencyNode ) nodesByArtifact.put( node.getArtifact(), node ); |
| 469 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 470 | if ( previousNode != null ) |
| 471 | { |
| 472 | throw new IllegalStateException( "Duplicate node registered for artifact: " + node.getArtifact() ); |
| 473 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 474 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 475 | if ( rootNode == null ) |
| 476 | { |
| 477 | rootNode = node; |
| 478 | } |
| 479 | |
| 480 | currentNode = node; |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 481 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 482 | return node; |
| 483 | } |
| 484 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 485 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 486 | /** |
| 487 | * Gets the dependency node for the specified artifact from the dependency node cache. |
| 488 | * |
| 489 | * @param artifact |
| 490 | * the artifact to find the dependency node for |
| 491 | * @return the dependency node, or <code>null</code> if the specified artifact has no corresponding dependency |
| 492 | * node |
| 493 | */ |
| 494 | private DependencyNode getNode( Artifact artifact ) |
| 495 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 496 | return ( DependencyNode ) nodesByArtifact.get( artifact ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 499 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 500 | /** |
| 501 | * Removes the dependency node for the specified artifact from the dependency node cache. |
| 502 | * |
| 503 | * @param artifact |
| 504 | * the artifact to remove the dependency node for |
| 505 | */ |
| 506 | private void removeNode( Artifact artifact ) |
| 507 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 508 | DependencyNode node = ( DependencyNode ) nodesByArtifact.remove( artifact ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 509 | |
| 510 | if ( !artifact.equals( node.getArtifact() ) ) |
| 511 | { |
| 512 | throw new IllegalStateException( "Removed dependency node artifact was expected to be " + artifact |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 513 | + " but was " + node.getArtifact() ); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 517 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 518 | /** |
| 519 | * Gets whether the all the ancestors of the dependency node currently being processed by this listener have an |
| 520 | * included state. |
| 521 | * |
| 522 | * @return <code>true</code> if all the ancestors of the current dependency node have a state of |
| 523 | * <code>INCLUDED</code> |
| 524 | */ |
| 525 | private boolean isCurrentNodeIncluded() |
| 526 | { |
| 527 | boolean included = true; |
| 528 | |
| 529 | for ( Iterator iterator = parentNodes.iterator(); included && iterator.hasNext(); ) |
| 530 | { |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 531 | DependencyNode node = ( DependencyNode ) iterator.next(); |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 532 | |
| 533 | if ( node.getState() != DependencyNode.INCLUDED ) |
| 534 | { |
| 535 | included = false; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return included; |
| 540 | } |
| 541 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 542 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 543 | /** |
| 544 | * Updates the specified node with any dependency management information cached in prior <code>manageArtifact</code> |
| 545 | * calls. |
| 546 | * |
| 547 | * @param node |
| 548 | * the node to update |
| 549 | */ |
| 550 | private void flushDependencyManagement( DependencyNode node ) |
| 551 | { |
| 552 | Artifact artifact = node.getArtifact(); |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 553 | String premanagedVersion = ( String ) managedVersions.get( getRangeId( artifact ) ); |
| 554 | String premanagedScope = ( String ) managedScopes.get( getRangeId( artifact ) ); |
| 555 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 556 | if ( premanagedVersion != null || premanagedScope != null ) |
| 557 | { |
| 558 | if ( premanagedVersion != null ) |
| 559 | { |
| 560 | node.setPremanagedVersion( premanagedVersion ); |
| 561 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 562 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 563 | if ( premanagedScope != null ) |
| 564 | { |
| 565 | node.setPremanagedScope( premanagedScope ); |
| 566 | } |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 567 | |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 568 | premanagedVersion = null; |
| 569 | premanagedScope = null; |
| 570 | } |
| 571 | } |
Stuart McCulloch | f3f38b2 | 2009-09-02 18:57:05 +0000 | [diff] [blame] | 572 | |
Stuart McCulloch | d98b02e | 2011-06-28 23:20:37 +0000 | [diff] [blame] | 573 | |
Stuart McCulloch | f3f38b2 | 2009-09-02 18:57:05 +0000 | [diff] [blame] | 574 | private static String getRangeId( Artifact artifact ) |
| 575 | { |
| 576 | return artifact.getDependencyConflictId() + ":" + artifact.getVersionRange(); |
| 577 | } |
Stuart McCulloch | 150ffa7 | 2012-02-11 14:47:23 +0000 | [diff] [blame] | 578 | |
| 579 | |
| 580 | public void manageArtifactSystemPath( Artifact artifact, Artifact replacement ) |
| 581 | { |
| 582 | // NO-OP |
| 583 | } |
Stuart McCulloch | acef948 | 2009-09-02 18:33:41 +0000 | [diff] [blame] | 584 | } |