001package org.apache.maven.plugins.enforcer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.lang3.SystemUtils;
023import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
024import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
025
026import java.util.List;
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 */
035public class RequireJavaVendor extends AbstractNonCacheableEnforcerRule
036{
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    @Override
049    public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException
050    {
051        if ( excludes != null && excludes.contains( SystemUtils.JAVA_VENDOR ) )
052        {
053            String message = getMessage();
054            if ( message == null ) 
055            {
056                message = String.format( "%s is an excluded Required Java Vendor", SystemUtils.JAVA_VENDOR );
057            }
058            throw new EnforcerRuleException( message );
059        }
060        else if ( includes != null && !includes.contains( SystemUtils.JAVA_VENDOR ) )
061        {
062            String message = getMessage();
063            if ( message == null ) 
064            {
065                message = String.format( "%s is not an included Required Java Vendor", SystemUtils.JAVA_VENDOR );
066            }
067            throw new EnforcerRuleException( message );
068        }
069    }
070
071    /**
072     * Specify the banned vendors. This should be an exact match of the System Property
073     * java.vendor, which you can also see with mvn --version. <br>
074     * Excludes override the include rules.
075     *
076     * @param theExcludes the vendor to to exclude from the include list.
077     */
078    public void setExcludes( List<String> theExcludes )
079    {
080        this.excludes = theExcludes;
081    }
082
083    /**
084     * Specify the allowed vendor names. This should be an exact match of the System Property
085     * java.vendor, which you can also see with mvn --version. <br>
086     * The rule will fail if vendor name matches any exclude, unless it also matches an
087     * include rule.
088     *
089     * Some examples are:
090     * <ul>
091     * <li><code>AdoptOpenJDK</code> prohibits vendor name AdoptOpenJDK </li>
092     * <li><code>Amazon</code> prohibits vendor name Amazon </li>
093     * </ul>
094     * 
095     * @param theIncludes the list of required vendors.
096     * 
097     * @see #setExcludes(List)
098     */
099    public void setIncludes( List<String> theIncludes )
100    {
101        this.includes = theIncludes;
102    }
103}