View Javadoc
1   package org.apache.maven.plugins.enforcer;
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.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * This rule checks that this project's maven session whether have banned repositories.
35   * 
36   * @author <a href="mailto:wangyf2010@gmail.com">Simon Wang</a>
37   */
38  public class BannedRepositories
39      extends AbstractNonCacheableEnforcerRule
40  {
41  
42      // ----------------------------------------------------------------------
43      // Mojo parameters
44      // ----------------------------------------------------------------------
45  
46      /**
47       * Specify explicitly banned non-plugin repositories. This is a list of repository url patterns. Support wildcard
48       * "*".
49       * 
50       * @see {@link #setBannedRepositories(List)}
51       */
52      private List<String> bannedRepositories = Collections.emptyList();
53  
54      /**
55       * Specify explicitly banned plugin repositories. This is a list of repository url patterns. Support wildcard "*".
56       * 
57       * @see {@link #setBannedPluginRepositories(List)}
58       */
59      private List<String> bannedPluginRepositories = Collections.emptyList();
60  
61      /**
62       * Specify explicitly allowed non-plugin repositories, then all others repositories would be banned. This is a list
63       * of repository url patterns. Support wildcard "*".
64       * 
65       * @see {@link #setAllowedRepositories(List)}
66       */
67      private List<String> allowedRepositories = Collections.emptyList();
68  
69      /**
70       * Specify explicitly allowed plugin repositories, then all others repositories would be banned. This is a list of
71       * repository url patterns. Support wildcard "*".
72       * 
73       * @see {@link #setAllowedPluginRepositories(List)}
74       */
75      private List<String> allowedPluginRepositories = Collections.emptyList();
76  
77      // ----------------------------------------------------------------------
78      // Public methods
79      // ----------------------------------------------------------------------
80  
81      @Override
82      public void execute( EnforcerRuleHelper helper )
83          throws EnforcerRuleException
84      {
85          MavenProject project;
86          try
87          {
88              project = (MavenProject) helper.evaluate( "${project}" );
89  
90              List<ArtifactRepository> resultBannedRepos =
91                  checkRepositories( project.getRemoteArtifactRepositories(), this.allowedRepositories,
92                                     this.bannedRepositories );
93  
94              List<ArtifactRepository> resultBannedPluginRepos =
95                  checkRepositories( project.getPluginArtifactRepositories(), this.allowedPluginRepositories,
96                                     this.bannedPluginRepositories );
97  
98              String repoErrMsg = populateErrorMessage( resultBannedRepos, " " );
99              String pluginRepoErrMsg = populateErrorMessage( resultBannedPluginRepos, " plugin " );
100 
101             String errMsg = repoErrMsg + pluginRepoErrMsg;
102 
103             if ( errMsg != null && !StringUtils.isEmpty( errMsg.toString() ) )
104             {
105                 throw new EnforcerRuleException( errMsg.toString() );
106             }
107 
108         }
109         catch ( ExpressionEvaluationException e )
110         {
111             throw new EnforcerRuleException( e.getLocalizedMessage() );
112         }
113     }
114 
115     // ----------------------------------------------------------------------
116     // Protected methods
117     // ----------------------------------------------------------------------
118 
119     protected void setBannedRepositories( List<String> bannedRepositories )
120     {
121         this.bannedRepositories = bannedRepositories;
122     }
123 
124     protected void setBannedPluginRepositories( List<String> bannedPluginRepositories )
125     {
126         this.bannedPluginRepositories = bannedPluginRepositories;
127     }
128 
129     protected void setAllowedRepositories( List<String> allowedRepositories )
130     {
131         this.allowedRepositories = allowedRepositories;
132     }
133 
134     protected void setAllowedPluginRepositories( List<String> allowedPluginRepositories )
135     {
136         this.allowedPluginRepositories = allowedPluginRepositories;
137     }
138 
139     // ----------------------------------------------------------------------
140     // Private methods
141     // ----------------------------------------------------------------------
142 
143     /**
144      * Check whether specified repositories have banned repositories.
145      * 
146      * @param repositories: candidate repositories.
147      * @param includes : 'include' patterns.
148      * @param excludes : 'exclude' patterns.
149      * @return Banned repositories.
150      */
151     private List<ArtifactRepository> checkRepositories( List<ArtifactRepository> repositories, List<String> includes,
152                                                         List<String> excludes )
153     {
154         List<ArtifactRepository> bannedRepos = new ArrayList<ArtifactRepository>();
155 
156         for ( ArtifactRepository repo : repositories )
157         {
158             String url = repo.getUrl().trim();
159             if ( includes.size() > 0 && !match( url, includes ) )
160             {
161                 bannedRepos.add( repo );
162                 continue;
163             }
164 
165             if ( excludes.size() > 0 && match( url, excludes ) )
166             {
167                 bannedRepos.add( repo );
168             }
169 
170         }
171 
172         return bannedRepos;
173     }
174 
175     private boolean match( String url, List<String> patterns )
176     {
177         for ( String pattern : patterns )
178         {
179             if ( this.match( url, pattern ) )
180             {
181                 return true;
182             }
183         }
184 
185         return false;
186     }
187 
188     private boolean match( String text, String pattern )
189     {
190         return text.matches( pattern.replace( "?", ".?" ).replace( "*", ".*?" ) );
191     }
192 
193     private String populateErrorMessage( List<ArtifactRepository> resultBannedRepos, String errorMessagePrefix )
194     {
195         StringBuffer errMsg = new StringBuffer( "" );
196         if ( !resultBannedRepos.isEmpty() )
197         {
198             errMsg.append( "Current maven session contains banned" + errorMessagePrefix
199                 + "repository urls, please double check your pom or settings.xml:\n"
200                 + getRepositoryUrlString( resultBannedRepos ) + "\n\n" );
201         }
202 
203         return errMsg.toString();
204     }
205 
206     private String getRepositoryUrlString( List<ArtifactRepository> resultBannedRepos )
207     {
208         StringBuffer urls = new StringBuffer( "" );
209         for ( ArtifactRepository repo : resultBannedRepos )
210         {
211             urls.append( repo.getId() + " - " + repo.getUrl() + "\n" );
212         }
213         return urls.toString();
214     }
215 
216 }