View Javadoc

1   package org.apache.maven.settings.validation;
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.List;
23  
24  import org.apache.maven.settings.Mirror;
25  import org.apache.maven.settings.Profile;
26  import org.apache.maven.settings.Repository;
27  import org.apache.maven.settings.Server;
28  import org.apache.maven.settings.Settings;
29  import org.apache.maven.settings.building.SettingsProblem.Severity;
30  import org.apache.maven.settings.building.SettingsProblemCollector;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * @author Milos Kleint
36   */
37  @Component( role = SettingsValidator.class )
38  public class DefaultSettingsValidator
39      implements SettingsValidator
40  {
41  
42      private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+";
43  
44      private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*";
45  
46      private static final String ILLEGAL_REPO_ID_CHARS = ILLEGAL_FS_CHARS;
47  
48      public void validate( Settings settings, SettingsProblemCollector problems )
49      {
50          if ( settings.isUsePluginRegistry() )
51          {
52              addViolation( problems, Severity.WARNING, "usePluginRegistry", null, "is deprecated and has no effect." );
53          }
54  
55          List<String> pluginGroups = settings.getPluginGroups();
56  
57          if ( pluginGroups != null )
58          {
59              for ( int i = 0; i < pluginGroups.size(); i++ )
60              {
61                  String pluginGroup = pluginGroups.get( i ).trim();
62  
63                  if ( StringUtils.isBlank( pluginGroup ) )
64                  {
65                      addViolation( problems, Severity.ERROR, "pluginGroups.pluginGroup[" + i + "]", null,
66                                    "must not be empty" );
67                  }
68                  else if ( !pluginGroup.matches( ID_REGEX ) )
69                  {
70                      addViolation( problems, Severity.ERROR, "pluginGroups.pluginGroup[" + i + "]", null,
71                                    "must denote a valid group id and match the pattern " + ID_REGEX );
72                  }
73              }
74          }
75  
76          List<Server> servers = settings.getServers();
77  
78          if ( servers != null )
79          {
80              for ( int i = 0; i < servers.size(); i++ )
81              {
82                  Server server = servers.get( i );
83  
84                  validateStringNotEmpty( problems, "servers.server[" + i + "].id", server.getId(), null );
85              }
86          }
87  
88          List<Mirror> mirrors = settings.getMirrors();
89  
90          if ( mirrors != null )
91          {
92              for ( Mirror mirror : mirrors )
93              {
94                  validateStringNotEmpty( problems, "mirrors.mirror.id", mirror.getId(), mirror.getUrl() );
95  
96                  validateBannedCharacters( problems, "mirrors.mirror.id", Severity.WARNING, mirror.getId(), null,
97                                            ILLEGAL_REPO_ID_CHARS );
98  
99                  if ( "local".equals( mirror.getId() ) )
100                 {
101                     addViolation( problems, Severity.WARNING, "mirrors.mirror.id", null, "must not be 'local'"
102                         + ", this identifier is reserved for the local repository"
103                         + ", using it for other repositories will corrupt your repository metadata." );
104                 }
105 
106                 validateStringNotEmpty( problems, "mirrors.mirror.url", mirror.getUrl(), mirror.getId() );
107 
108                 validateStringNotEmpty( problems, "mirrors.mirror.mirrorOf", mirror.getMirrorOf(), mirror.getId() );
109             }
110         }
111 
112         List<Profile> profiles = settings.getProfiles();
113 
114         if ( profiles != null )
115         {
116             for ( Profile profile : profiles )
117             {
118                 validateRepositories( problems, profile.getRepositories(), "repositories.repository" );
119                 validateRepositories( problems, profile.getPluginRepositories(),
120                                       "pluginRepositories.pluginRepository" );
121             }
122         }
123     }
124 
125     private void validateRepositories( SettingsProblemCollector problems, List<Repository> repositories, String prefix )
126     {
127         for ( Repository repository : repositories )
128         {
129             validateStringNotEmpty( problems, prefix + ".id", repository.getId(), repository.getUrl() );
130 
131             validateBannedCharacters( problems, prefix + ".id", Severity.WARNING, repository.getId(), null,
132                                       ILLEGAL_REPO_ID_CHARS );
133 
134             if ( "local".equals( repository.getId() ) )
135             {
136                 addViolation( problems, Severity.WARNING, prefix + ".id", null, "must not be 'local'"
137                     + ", this identifier is reserved for the local repository"
138                     + ", using it for other repositories will corrupt your repository metadata." );
139             }
140 
141             validateStringNotEmpty( problems, prefix + ".url", repository.getUrl(), repository.getId() );
142 
143             if ( "legacy".equals( repository.getLayout() ) )
144             {
145                 addViolation( problems, Severity.WARNING, prefix + ".layout", repository.getId(),
146                               "uses the unsupported value 'legacy', artifact resolution might fail." );
147             }
148         }
149     }
150 
151     // ----------------------------------------------------------------------
152     // Field validation
153     // ----------------------------------------------------------------------
154 
155     /**
156      * Asserts:
157      * <p/>
158      * <ul>
159      * <li><code>string.length != null</code>
160      * <li><code>string.length > 0</code>
161      * </ul>
162      */
163     private boolean validateStringNotEmpty( SettingsProblemCollector problems, String fieldName, String string,
164                                             String sourceHint )
165     {
166         if ( !validateNotNull( problems, fieldName, string, sourceHint ) )
167         {
168             return false;
169         }
170 
171         if ( string.length() > 0 )
172         {
173             return true;
174         }
175 
176         addViolation( problems, Severity.ERROR, fieldName, sourceHint, "is missing" );
177 
178         return false;
179     }
180 
181     /**
182      * Asserts:
183      * <p/>
184      * <ul>
185      * <li><code>string != null</code>
186      * </ul>
187      */
188     private boolean validateNotNull( SettingsProblemCollector problems, String fieldName, Object object,
189                                      String sourceHint )
190     {
191         if ( object != null )
192         {
193             return true;
194         }
195 
196         addViolation( problems, Severity.ERROR, fieldName, sourceHint, "is missing" );
197 
198         return false;
199     }
200 
201     private boolean validateBannedCharacters( SettingsProblemCollector problems, String fieldName, Severity severity,
202                                               String string, String sourceHint, String banned )
203     {
204         if ( string != null )
205         {
206             for ( int i = string.length() - 1; i >= 0; i-- )
207             {
208                 if ( banned.indexOf( string.charAt( i ) ) >= 0 )
209                 {
210                     addViolation( problems, severity, fieldName, sourceHint,
211                                   "must not contain any of these characters " + banned + " but found "
212                                       + string.charAt( i ) );
213                     return false;
214                 }
215             }
216         }
217 
218         return true;
219     }
220 
221     private void addViolation( SettingsProblemCollector problems, Severity severity, String fieldName,
222                                String sourceHint, String message )
223     {
224         StringBuilder buffer = new StringBuilder( 256 );
225         buffer.append( '\'' ).append( fieldName ).append( '\'' );
226 
227         if ( sourceHint != null )
228         {
229             buffer.append( " for " ).append( sourceHint );
230         }
231 
232         buffer.append( ' ' ).append( message );
233 
234         problems.add( severity, buffer.toString(), -1, -1, null );
235     }
236 
237 }