View Javadoc
1   package org.apache.maven.repository;
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.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.List;
25  
26  import org.apache.maven.RepositoryUtils;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.settings.Mirror;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  @Component( role = MirrorSelector.class )
33  public class DefaultMirrorSelector
34      implements MirrorSelector
35  {
36  
37      private static final String WILDCARD = "*";
38  
39      private static final String EXTERNAL_WILDCARD = "external:*";
40  
41      public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
42      {
43          String repoId = repository.getId();
44  
45          if ( repoId != null && mirrors != null )
46          {
47              for ( Mirror mirror : mirrors )
48              {
49                  if ( repoId.equals( mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
50                  {
51                      return mirror;
52                  }
53              }
54  
55              for ( Mirror mirror : mirrors )
56              {
57                  if ( matchPattern( repository, mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
58                  {
59                      return mirror;
60                  }
61              }
62          }
63  
64          return null;
65      }
66  
67      /**
68       * This method checks if the pattern matches the originalRepository. Valid patterns: * =
69       * everything external:* = everything not on the localhost and not file based. repo,repo1 = repo
70       * or repo1 *,!repo1 = everything except repo1
71       *
72       * @param originalRepository to compare for a match.
73       * @param pattern used for match. Currently only '*' is supported.
74       * @return true if the repository is a match to this pattern.
75       */
76      static boolean matchPattern( ArtifactRepository originalRepository, String pattern )
77      {
78          boolean result = false;
79          String originalId = originalRepository.getId();
80  
81          // simple checks first to short circuit processing below.
82          if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
83          {
84              result = true;
85          }
86          else
87          {
88              // process the list
89              String[] repos = pattern.split( "," );
90              for ( String repo : repos )
91              {
92                  repo = repo.trim();
93                  // see if this is a negative match
94                  if ( repo.length() > 1 && repo.startsWith( "!" ) )
95                  {
96                      if ( repo.substring( 1 ).equals( originalId ) )
97                      {
98                          // explicitly exclude. Set result and stop processing.
99                          result = false;
100                         break;
101                     }
102                 }
103                 // check for exact match
104                 else if ( repo.equals( originalId ) )
105                 {
106                     result = true;
107                     break;
108                 }
109                 // check for external:*
110                 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) )
111                 {
112                     result = true;
113                     // don't stop processing in case a future segment explicitly excludes this repo
114                 }
115                 else if ( WILDCARD.equals( repo ) )
116                 {
117                     result = true;
118                     // don't stop processing in case a future segment explicitly excludes this repo
119                 }
120             }
121         }
122         return result;
123     }
124 
125     /**
126      * Checks the URL to see if this repository refers to an external repository
127      *
128      * @param originalRepository
129      * @return true if external.
130      */
131     static boolean isExternalRepo( ArtifactRepository originalRepository )
132     {
133         try
134         {
135             URL url = new URL( originalRepository.getUrl() );
136             return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" )
137                             || url.getProtocol().equals( "file" ) );
138         }
139         catch ( MalformedURLException e )
140         {
141             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
142             return false;
143         }
144     }
145 
146     static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
147     {
148         return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
149     }
150 
151     /**
152      * Checks whether the layouts configured for a mirror match with the layout of the repository.
153      *
154      * @param repoLayout The layout of the repository, may be {@code null}.
155      * @param mirrorLayout The layouts supported by the mirror, may be {@code null}.
156      * @return {@code true} if the layouts associated with the mirror match the layout of the original repository,
157      *         {@code false} otherwise.
158      */
159     static boolean matchesLayout( String repoLayout, String mirrorLayout )
160     {
161         boolean result = false;
162 
163         // simple checks first to short circuit processing below.
164         if ( StringUtils.isEmpty( mirrorLayout ) || WILDCARD.equals( mirrorLayout ) )
165         {
166             result = true;
167         }
168         else if ( mirrorLayout.equals( repoLayout ) )
169         {
170             result = true;
171         }
172         else
173         {
174             // process the list
175             String[] layouts = mirrorLayout.split( "," );
176             for ( String layout : layouts )
177             {
178                 // see if this is a negative match
179                 if ( layout.length() > 1 && layout.startsWith( "!" ) )
180                 {
181                     if ( layout.substring( 1 ).equals( repoLayout ) )
182                     {
183                         // explicitly exclude. Set result and stop processing.
184                         result = false;
185                         break;
186                     }
187                 }
188                 // check for exact match
189                 else if ( layout.equals( repoLayout ) )
190                 {
191                     result = true;
192                     break;
193                 }
194                 else if ( WILDCARD.equals( layout ) )
195                 {
196                     result = true;
197                     // don't stop processing in case a future segment explicitly excludes this repo
198                 }
199             }
200         }
201 
202         return result;
203     }
204 
205 }