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.enforcer.rules;
20  
21  import javax.inject.Named;
22  
23  import java.util.List;
24  
25  import org.apache.commons.lang3.SystemUtils;
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  
28  /**
29   * This rule checks that the Java vendor is allowed.
30   * Rule will fail is it matches any of the excludes or doesn't match any include in case it was set.
31   *
32   * @author Tim Sijstermans
33   * @since 3.0.0
34   */
35  @Named("requireJavaVendor")
36  public final class RequireJavaVendor extends AbstractStandardEnforcerRule {
37      /**
38       * Java vendors to include. If none is defined, all are included.
39       *
40       */
41      private List<String> includes;
42  
43      /**
44       * Java vendors to exclude.
45       */
46      private List<String> excludes;
47  
48      /**
49       * The Java Vendor not changed during one Maven session,
50       * so can be cached.
51       *
52       * @return a cache id
53       */
54      @Override
55      public String getCacheId() {
56          String result = "";
57  
58          if (includes != null) {
59              result += "" + includes.hashCode();
60          }
61  
62          if (excludes != null) {
63              result += "" + excludes.hashCode();
64          }
65  
66          if (getMessage() != null) {
67              result += "" + getMessage().hashCode();
68          }
69  
70          return result;
71      }
72  
73      @Override
74      public void execute() throws EnforcerRuleException {
75          if (excludes != null && excludes.contains(SystemUtils.JAVA_VENDOR)) {
76              String message = getMessage();
77              if (message == null) {
78                  message = String.format(
79                          "%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
80                          SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
81              }
82              throw new EnforcerRuleException(message);
83          } else if (includes != null && !includes.contains(SystemUtils.JAVA_VENDOR)) {
84              String message = getMessage();
85              if (message == null) {
86                  message = String.format(
87                          "%s is not an included Required Java Vendor (JAVA_HOME=%s)",
88                          SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
89              }
90              throw new EnforcerRuleException(message);
91          }
92      }
93  
94      /**
95       * Specify the banned vendors. This should be an exact match of the System Property
96       * java.vendor, which you can also see with mvn --version. <br>
97       * Excludes override the include rules.
98       *
99       * @param theExcludes the vendor to to exclude from the include list.
100      */
101     public void setExcludes(List<String> theExcludes) {
102         this.excludes = theExcludes;
103     }
104 
105     /**
106      * Specify the allowed vendor names. This should be an exact match of the System Property
107      * java.vendor, which you can also see with mvn --version. <br>
108      * The rule will fail if vendor name matches any exclude, unless it also matches an
109      * include rule.
110      *
111      * Some examples are:
112      * <ul>
113      * <li><code>AdoptOpenJDK</code> prohibits vendor name AdoptOpenJDK </li>
114      * <li><code>Amazon</code> prohibits vendor name Amazon </li>
115      * </ul>
116      *
117      * @param theIncludes the list of required vendors.
118      *
119      * @see #setExcludes(List)
120      */
121     public void setIncludes(List<String> theIncludes) {
122         this.includes = theIncludes;
123     }
124 
125     @Override
126     public String toString() {
127         return String.format(
128                 "RequireJavaVendor[message=%s, includes=%s, excludes=%s]", getMessage(), includes, excludes);
129     }
130 }