1 package org.apache.maven.bridge;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.util.StringUtils;
30
31 public class MirrorSelector
32 {
33 private static final String WILDCARD = "*";
34
35 private static final String EXTERNAL_WILDCARD = "external:*";
36
37 public static Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
38 {
39 String repoId = repository.getId();
40
41 if ( repoId != null && mirrors != null )
42 {
43 for ( Mirror mirror : mirrors )
44 {
45 if ( repoId.equals( mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
46 {
47 return mirror;
48 }
49 }
50
51 for ( Mirror mirror : mirrors )
52 {
53 if ( matchPattern( repository, mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
54 {
55 return mirror;
56 }
57 }
58 }
59
60 return null;
61 }
62
63
64
65
66
67
68
69
70
71 static boolean matchPattern( ArtifactRepository originalRepository, String pattern )
72 {
73 boolean result = false;
74 String originalId = originalRepository.getId();
75
76
77 if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
78 {
79 result = true;
80 }
81 else
82 {
83
84 String[] repos = pattern.split( "," );
85 for ( String repo : repos )
86 {
87
88 if ( repo.length() > 1 && repo.startsWith( "!" ) )
89 {
90 if ( repo.substring( 1 ).equals( originalId ) )
91 {
92
93 result = false;
94 break;
95 }
96 }
97
98 else if ( repo.equals( originalId ) )
99 {
100 result = true;
101 break;
102 }
103
104 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) )
105 {
106 result = true;
107
108 }
109 else if ( WILDCARD.equals( repo ) )
110 {
111 result = true;
112
113 }
114 }
115 }
116 return result;
117 }
118
119
120
121
122
123
124
125 static boolean isExternalRepo( ArtifactRepository originalRepository )
126 {
127 try
128 {
129 URL url = new URL( originalRepository.getUrl() );
130 return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" ) || url.getProtocol().equals( "file" ) );
131 }
132 catch ( MalformedURLException e )
133 {
134
135 return false;
136 }
137 }
138
139 static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
140 {
141 return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
142 }
143
144
145
146
147
148
149
150
151
152 static boolean matchesLayout( String repoLayout, String mirrorLayout )
153 {
154 boolean result = false;
155
156
157 if ( StringUtils.isEmpty( mirrorLayout ) || WILDCARD.equals( mirrorLayout ) )
158 {
159 result = true;
160 }
161 else if ( mirrorLayout.equals( repoLayout ) )
162 {
163 result = true;
164 }
165 else
166 {
167
168 String[] layouts = mirrorLayout.split( "," );
169 for ( String layout : layouts )
170 {
171
172 if ( layout.length() > 1 && layout.startsWith( "!" ) )
173 {
174 if ( layout.substring( 1 ).equals( repoLayout ) )
175 {
176
177 result = false;
178 break;
179 }
180 }
181
182 else if ( layout.equals( repoLayout ) )
183 {
184 result = true;
185 break;
186 }
187 else if ( WILDCARD.equals( layout ) )
188 {
189 result = true;
190
191 }
192 }
193 }
194
195 return result;
196 }
197 }