View Javadoc
1   package org.apache.maven.shared.transfer.dependencies.collect.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.RepositoryUtils;
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollectorException;
29  import org.apache.maven.shared.transfer.graph.DependencyNode;
30  import org.apache.maven.shared.transfer.graph.DependencyVisitor;
31  import org.eclipse.aether.repository.RemoteRepository;
32  
33  /**
34   * DependencyCollectorNode wrapper around {@link org.eclipse.aether.graph.DependencyNode}
35   *
36   * @author Pim Moerenhout
37   *
38   */
39  class Maven31DependencyNodeAdapter implements DependencyNode
40  {
41  
42      private org.eclipse.aether.graph.DependencyNode dependencyNode;
43  
44      /**
45       * @param dependencyNode {@link org.eclipse.aether.graph.DependencyNode}
46       */
47      Maven31DependencyNodeAdapter( org.eclipse.aether.graph.DependencyNode dependencyNode )
48      {
49          this.dependencyNode = dependencyNode;
50      }
51  
52      @Override
53      public Artifact getArtifact()
54      {
55          return getArtifact( dependencyNode.getArtifact() );
56      }
57  
58      @Override
59      public List<DependencyNode> getChildren()
60      {
61          List<org.eclipse.aether.graph.DependencyNode> aetherChildren = dependencyNode.getChildren();
62          List<DependencyNode> children = new ArrayList<>( aetherChildren.size() );
63          for ( org.eclipse.aether.graph.DependencyNode aetherChild : aetherChildren )
64          {
65              children.add( new Maven31DependencyNodeAdapter( aetherChild ) );
66          }
67          return children;
68      }
69  
70      @Override
71      public List<ArtifactRepository> getRemoteRepositories()
72      {
73          List<RemoteRepository> aetherRepositories = dependencyNode.getRepositories();
74          List<ArtifactRepository> mavenRepositories = new ArrayList<>( aetherRepositories.size() );
75  
76          for ( RemoteRepository aetherRepository : aetherRepositories )
77          {
78              mavenRepositories.add( new Maven31ArtifactRepositoryAdapter( aetherRepository ) );
79          }
80  
81          return mavenRepositories;
82      }
83  
84      @Override
85      public String getScope()
86      {
87          return dependencyNode.getDependency().getScope();
88      }
89  
90      @Override
91      public Boolean getOptional()
92      {
93          return dependencyNode.getDependency().getOptional();
94      }
95  
96      @Override
97      public boolean accept( DependencyVisitor visitor )
98      {
99          if ( visitor.visitEnter( this ) )
100         {
101             for ( org.eclipse.aether.graph.DependencyNode child : dependencyNode.getChildren() )
102             {
103                 DependencyNode node = new Maven31DependencyNodeAdapter( child );
104                 if ( !node.accept( visitor ) )
105                 {
106                     break;
107                 }
108             }
109         }
110 
111         return visitor.visitLeave( this );
112     }
113 
114     @Override
115     public int hashCode()
116     {
117         return dependencyNode.hashCode();
118     }
119 
120     @Override
121     public boolean equals( Object obj )
122     {
123         if ( this == obj )
124         {
125             return true;
126         }
127         if ( obj == null )
128         {
129             return false;
130         }
131         if ( getClass() != obj.getClass() )
132         {
133             return false;
134         }
135 
136         Maven31DependencyNodeAdapter other = (Maven31DependencyNodeAdapter) obj;
137         if ( dependencyNode == null )
138         {
139             if ( other.dependencyNode != null )
140             {
141                 return false;
142             }
143         }
144         else if ( !dependencyNode.equals( other.dependencyNode ) )
145         {
146             return false;
147         }
148         return true;
149     }
150 
151     private Artifact getArtifact( org.eclipse.aether.artifact.Artifact aetherArtifact )
152     {
153         try
154         {
155             return Invoker.invoke( RepositoryUtils.class, "toArtifact",
156                 org.eclipse.aether.artifact.Artifact.class, aetherArtifact );
157         }
158         catch ( DependencyCollectorException e )
159         {
160             throw new RuntimeException( e );
161         }
162     }
163 }