001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.enforcer.rules;
020
021import javax.inject.Named;
022
023import java.util.List;
024
025import org.apache.commons.lang3.SystemUtils;
026import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
027
028/**
029 * This rule checks that the Java vendor is allowed.
030 * Rule will fail is it matches any of the excludes or doesn't match any include in case it was set.
031 *
032 * @author Tim Sijstermans
033 * @since 3.0.0
034 */
035@Named("requireJavaVendor")
036public final class RequireJavaVendor extends AbstractStandardEnforcerRule {
037    /**
038     * Java vendors to include. If none is defined, all are included.
039     *
040     */
041    private List<String> includes;
042
043    /**
044     * Java vendors to exclude.
045     */
046    private List<String> excludes;
047
048    /**
049     * The Java Vendor not changed during one Maven session,
050     * so can be cached.
051     *
052     * @return a cache id
053     */
054    @Override
055    public String getCacheId() {
056        String result = "";
057
058        if (includes != null) {
059            result += "" + includes.hashCode();
060        }
061
062        if (excludes != null) {
063            result += "" + excludes.hashCode();
064        }
065
066        if (getMessage() != null) {
067            result += "" + getMessage().hashCode();
068        }
069
070        return result;
071    }
072
073    @Override
074    public void execute() throws EnforcerRuleException {
075        if (excludes != null && excludes.contains(SystemUtils.JAVA_VENDOR)) {
076            String message = getMessage();
077            if (message == null) {
078                message = String.format(
079                        "%s is an excluded Required Java Vendor (JAVA_HOME=%s)",
080                        SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
081            }
082            throw new EnforcerRuleException(message);
083        } else if (includes != null && !includes.contains(SystemUtils.JAVA_VENDOR)) {
084            String message = getMessage();
085            if (message == null) {
086                message = String.format(
087                        "%s is not an included Required Java Vendor (JAVA_HOME=%s)",
088                        SystemUtils.JAVA_VENDOR, SystemUtils.JAVA_HOME);
089            }
090            throw new EnforcerRuleException(message);
091        }
092    }
093
094    /**
095     * Specify the banned vendors. This should be an exact match of the System Property
096     * java.vendor, which you can also see with mvn --version. <br>
097     * Excludes override the include rules.
098     *
099     * @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}