View Javadoc
1   package org.apache.maven.plugin.doap.options;
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.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.model.Developer;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  /**
32   * Utility class for {@link ASFExtOptions} class.
33   *
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   * @version $Id: ASFExtOptionsUtil.java 1642247 2014-11-27 23:17:01Z hboutemy $
36   * @since 1.1
37   */
38  public class ASFExtOptionsUtil
39  {
40      /** Apache domain name, i.e. apache.org */
41      private static final String APACHE_DOMAIN_NAME = "apache.org";
42  
43      /**
44       * The ASF category resource.
45       *
46       * @see <a href="http://projects.apache.org/guidelines.html">http://projects.apache.org/guidelines.html</a>
47       */
48      public static final String CATEGORY_RESOURCE = "http://projects.apache.org/category/";
49  
50      /** Projects related to building/maintaining source code/websites. */
51      public static final String BUILD_MANAGEMENT_CATEGORY = "build-management";
52  
53      /** Projects related to databases. */
54      public static final String DATABASE_CATEGORY = "database";
55  
56      /** Related to the HyperText Transfer Protocol. */
57      public static final String HTTP_CATEGORY = "http";
58  
59      /** Modules designed for use by the Apache HTTP Server. */
60      public static final String HTTP_MODULES_CATEGORY = "httpd-modules";
61  
62      /** A library meant to be used by other applications. */
63      public static final String LIBRARY_CATEGORY = "library";
64  
65      /** Servers or applications related to internet mail protocols. */
66      public static final String MAIL_CATEGORY = "mail";
67  
68      /** Anything that acts as a client across a network. */
69      public static final String NETWORK_CLIENT_CATEGORY = "network-client";
70  
71      /** Anything that acts as a server across a network. */
72      public static final String NETWORK_SERVER_CATEGORY = "network-server";
73  
74      /** Software designed to test or verify other software. */
75      public static final String TESTING_CATEGORY = "testing";
76  
77      /** Unifying frameworks for website development. */
78      public static final String WEB_FRAMEWORK_CATEGORY = "web-framework";
79  
80      /** Software based on XML technologies. */
81      public static final String XML_CATEGORY = "xml";
82  
83      /** All categories supported by ASF */
84      public static final String[] CATEGORIES = { BUILD_MANAGEMENT_CATEGORY, DATABASE_CATEGORY, HTTP_CATEGORY,
85          HTTP_MODULES_CATEGORY, LIBRARY_CATEGORY, MAIL_CATEGORY, NETWORK_CLIENT_CATEGORY, NETWORK_SERVER_CATEGORY,
86          TESTING_CATEGORY, WEB_FRAMEWORK_CATEGORY, XML_CATEGORY };
87  
88      /** C or C++ Programming Language. */
89      public static final String C_PROGRAMMING_LANGUAGE = "C";
90  
91      /** Java Programming Language and all its components. */
92      public static final String JAVA_PROGRAMMING_LANGUAGE = "Java";
93  
94      /** Perl Programming Language. */
95      public static final String PERL_PROGRAMMING_LANGUAGE = "Perl";
96  
97      /** Python Programming Language. */
98      public static final String PYTHON_PROGRAMMING_LANGUAGE = "Python";
99  
100     /** Scalable Vector Graphic Programming Language. */
101     public static final String SVG_PROGRAMMING_LANGUAGE = "SVG";
102 
103     /** Tcl Programming Language. */
104     public static final String TCL_PROGRAMMING_LANGUAGE = "Tcl";
105 
106     /** All Programming Languages supported by ASF */
107     public static final String[] PROGRAMMING_LANGUAGES = { C_PROGRAMMING_LANGUAGE, JAVA_PROGRAMMING_LANGUAGE,
108         PERL_PROGRAMMING_LANGUAGE, PYTHON_PROGRAMMING_LANGUAGE, SVG_PROGRAMMING_LANGUAGE, TCL_PROGRAMMING_LANGUAGE };
109 
110     /**
111      * @param category not null
112      * @return if the given category is supported by ASF (correctly formatted) or <code>null</code> if not found.
113      * @see <a href="http://projects.apache.org/categories.html">http://projects.apache.org/categories.html</a>
114      * @see #CATEGORIES
115      */
116     public static String getCategorySupportedByASF( String category )
117     {
118         for ( String supportedCategory : CATEGORIES )
119         {
120             if ( supportedCategory.equalsIgnoreCase( category ) )
121             {
122                 return supportedCategory;
123             }
124         }
125 
126         return null;
127     }
128 
129     /**
130      * @param programmingLanguage not null
131      * @return the given programming language supported by ASF (correctly formatted) or <code>null</code> if not found.
132      * @see <a href="http://projects.apache.org/languages.html">http://projects.apache.org/languages.html</a>
133      * @see #PROGRAMMING_LANGUAGES
134      */
135     public static String getProgrammingLanguageSupportedByASF( String programmingLanguage )
136     {
137         for ( String supportedProgrammingLanguage : PROGRAMMING_LANGUAGES )
138         {
139             if ( supportedProgrammingLanguage.equalsIgnoreCase( programmingLanguage ) )
140             {
141                 return supportedProgrammingLanguage;
142             }
143         }
144 
145         return null;
146     }
147 
148     /**
149      * Find the chair man of the project. The role of the developer should contain <code>chair</code>.
150      *
151      * @param developers list of <code>{@link Developer}</code>
152      * @return a Developer or null if not found.
153      */
154     public static Developer findChair( List<Developer> developers )
155     {
156         if ( developers == null || developers.isEmpty() )
157         {
158             return null;
159         }
160 
161         for ( Developer developer : developers )
162         {
163             List<String> roles = developer.getRoles();
164 
165             for ( String role : roles )
166             {
167                 if ( role.toLowerCase().contains( "chair" ) )
168                 {
169                     return developer;
170                 }
171             }
172         }
173 
174         return null;
175     }
176 
177     /**
178      * Find the list of PMC members of the project. The role of each developer should contain <code>pmc</code>.
179      *
180      * @param developers list of <code>{@link Developer}</code>
181      * @return a not null list of Developer.
182      */
183     public static List<Developer> findPMCMembers( List<Developer> developers )
184     {
185         if ( developers == null || developers.isEmpty() )
186         {
187             return null;
188         }
189 
190         List<Developer> pmcs = new ArrayList<Developer>();
191         for ( Developer developer : developers )
192         {
193             List<String> roles = developer.getRoles();
194 
195             for ( String role : roles )
196             {
197                 if ( role.toLowerCase().contains( "pmc" ) )
198                 {
199                     pmcs.add( developer );
200                 }
201             }
202         }
203 
204         return pmcs;
205     }
206 
207     /**
208      * Try to find if the given project is hosted at Apache.
209      *
210      * @param project not null
211      * @return <code>true</code> if the SCM url, distribution management url, project url or organization url is hosted
212      *         in the Apache domain name, <code>false</code> otherwise.
213      * @see #APACHE_DOMAIN_NAME
214      * @since 1.1
215      */
216     public static boolean isASFProject( MavenProject project )
217     {
218         if ( project == null )
219         {
220             throw new IllegalArgumentException( "project is required" );
221         }
222 
223         // check organization name
224         if ( project.getOrganization() != null && StringUtils.isNotEmpty( project.getOrganization().getName() )
225             && project.getOrganization().getName().trim().equals( "The Apache Software Foundation" ) )
226             // see org.apache:apache artifact
227         {
228             return true;
229         }
230 
231         // check domain name
232         if ( project.getOrganization() != null && isHostedAtASF( project.getOrganization().getUrl() ) )
233         {
234             return true;
235         }
236 
237         if ( isHostedAtASF( project.getUrl() ) )
238         {
239             return true;
240         }
241 
242         if ( project.getScm() != null )
243         {
244             if ( StringUtils.isNotEmpty( project.getScm().getUrl() )
245                 && project.getScm().getUrl().contains( APACHE_DOMAIN_NAME ) )
246             {
247                 return true;
248             }
249 
250             if ( StringUtils.isNotEmpty( project.getScm().getConnection() )
251                 && project.getScm().getConnection().contains( APACHE_DOMAIN_NAME ) )
252             {
253                 return true;
254             }
255 
256             if ( StringUtils.isNotEmpty( project.getScm().getDeveloperConnection() )
257                 && project.getScm().getDeveloperConnection().contains( APACHE_DOMAIN_NAME ) )
258             {
259                 return true;
260             }
261         }
262 
263         if ( project.getDistributionManagement() != null )
264         {
265             if ( isHostedAtASF( project.getDistributionManagement().getDownloadUrl() ) )
266             {
267                 return true;
268             }
269 
270             if ( project.getDistributionManagement().getRepository() != null
271                 && isHostedAtASF( project.getDistributionManagement().getRepository().getUrl() ) )
272             {
273                 return true;
274             }
275 
276             if ( project.getDistributionManagement().getSnapshotRepository() != null
277                 && isHostedAtASF( project.getDistributionManagement().getSnapshotRepository().getUrl() ) )
278             {
279                 return true;
280             }
281 
282             if ( project.getDistributionManagement().getSite() != null
283                 && isHostedAtASF( project.getDistributionManagement().getSite().getUrl() ) )
284             {
285                 return true;
286             }
287         }
288 
289         return false;
290     }
291 
292     /**
293      * @param str an url could be null
294      * @return <code>true</code> if the str is hosted by ASF.
295      * @see #APACHE_DOMAIN_NAME
296      */
297     private static boolean isHostedAtASF( String str )
298     {
299         if ( StringUtils.isEmpty( str ) )
300         {
301             return false;
302         }
303 
304         str = str.trim();
305         try
306         {
307             URL url = new URL( str );
308             if ( url.getHost().endsWith( APACHE_DOMAIN_NAME ) )
309             {
310                 return true;
311             }
312         }
313         catch ( MalformedURLException e )
314         {
315             // ignore
316         }
317 
318         return false;
319     }
320 }