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