View Javadoc

1   package org.apache.maven.plugin.ear.util;
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.Set;
23  import java.util.TreeSet;
24  
25  import org.apache.maven.artifact.Artifact;
26  
27  /**
28   * An artifact repository used to resolve {@link org.apache.maven.plugin.ear.EarModule}.
29   *
30   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
31   * @version $Id: ArtifactRepository.java 1228846 2012-01-08 13:53:34Z rfscholte $
32   */
33  public class ArtifactRepository
34  {
35      private final Set<Artifact> artifacts;
36  
37      private final String mainArtifactId;
38  
39      private final ArtifactTypeMappingService artifactTypeMappingService;
40  
41      /**
42       * Creates a new repository wih the specified artifacts.
43       *
44       * @param artifacts                  the artifacts
45       * @param mainArtifactId             the id to use for the main artifact (no classifier)
46       * @param artifactTypeMappingService
47       */
48      public ArtifactRepository( Set<Artifact> artifacts, String mainArtifactId,
49                                 ArtifactTypeMappingService artifactTypeMappingService )
50      {
51          this.artifacts = artifacts;
52          this.mainArtifactId = mainArtifactId;
53          this.artifactTypeMappingService = artifactTypeMappingService;
54      }
55  
56      /**
57       * Returns the artifact with the specified parameters.
58       * <p/>
59       * If the artifact is classified and is the only one with the specified
60       * groupI, artifactId and type, it will be returned.
61       * <p/>
62       * If the artifact is classified and is not the only one with the specified
63       * groupI, artifactId and type, it returns null.
64       * <p/>
65       * If the artifact is not found, it returns null.
66       *
67       * @param groupId    the group id
68       * @param artifactId the artifact id
69       * @param type       the type
70       * @param classifier the classifier
71       * @return the artifact or null if no artifact were found
72       */
73      public Artifact getUniqueArtifact( String groupId, String artifactId, String type, String classifier )
74      {
75          final Set<Artifact> candidates = getArtifacts( groupId, artifactId, type );
76          if ( candidates.size() == 0 )
77          {
78              return null;
79          }
80          else if ( candidates.size() == 1 && classifier == null )
81          {
82              return candidates.iterator().next();
83          }
84          else if ( classifier != null )
85          {
86              for ( Artifact a : candidates )
87              {
88                  if ( a.getClassifier() == null && classifier.equals( mainArtifactId ) )
89                  {
90                      return a;
91                  }
92                  else if ( classifier.equals( a.getClassifier() ) )
93                  {
94                      return a;
95                  }
96              }
97          }
98          // All other cases, classifier is null and more than one candidate ; artifact not found
99          return null;
100     }
101 
102     /**
103      * Returns the artifact with the specified parameters.
104      * <p/>
105      * If the artifact is classified and is the only one with the specified
106      * groupI, artifactId and type, it will be returned.
107      * <p/>
108      * If the artifact is classified and is not the only one with the specified
109      * groupI, artifactId and type, it returns null.
110      * <p/>
111      * If the artifact is not found, it returns null.
112      *
113      * @param groupId    the group id
114      * @param artifactId the artifact id
115      * @param type       the type
116      * @return the artifact or null if no artifact were found
117      */
118     public Artifact getUniqueArtifact( String groupId, String artifactId, String type )
119     {
120         return getUniqueArtifact( groupId, artifactId, type, null );
121     }
122 
123     /**
124      * Returns the artifacts with the specified parameters.
125      *
126      * @param groupId    the group id
127      * @param artifactId the artifact id
128      * @param type       the type
129      * @return the artifacts or an empty set if no artifact were found
130      */
131     public Set<Artifact> getArtifacts( String groupId, String artifactId, String type )
132     {
133         final Set<Artifact> result = new TreeSet<Artifact>();
134         for ( Artifact a : artifacts )
135         {
136             // If the groupId, the artifactId and if the
137             // artifact's type is known, then we have found a candidate.
138             if ( a.getGroupId().equals( groupId ) && a.getArtifactId().equals( artifactId )
139                 && artifactTypeMappingService.isMappedToType( type, a.getType() ) )
140             {
141                 result.add( a );
142 
143             }
144         }
145         return result;
146     }
147 }