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.rule.api;
020
021/**
022 * Entry point for custom {@code Enforcer Rule}.
023 * <p>
024 * Please see
025 * <a href="https://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html">Writing a custom rule</a>
026 *
027 * @author Slawomir Jaranowski
028 * @since 3.2.1
029 */
030public abstract class AbstractEnforcerRule extends AbstractEnforcerRuleBase {
031
032    /**
033     * Enforcer Rule execution level
034     */
035    private EnforcerLevel level = EnforcerLevel.ERROR;
036
037    /**
038     * Rule name for current rule instance.
039     */
040    private String ruleName;
041
042    /**
043     * Current Enforcer execution level
044     *
045     * @return an Enforcer execution level
046     */
047    @Override
048    public EnforcerLevel getLevel() {
049        return level;
050    }
051
052    /**
053     * Rule name for current rule instance.
054     *
055     * @return a rule name.
056     * @since 3.6.0
057     */
058    @Override
059    public String getRuleName() {
060        return ruleName;
061    }
062
063    /**
064     * If the rule is to be cached during session scope, whole executing of Maven build,
065     * this id is used as part of the key.
066     * <p>
067     * Rule of the same class and the same cache id will be executed once.
068     *
069     * @return id to be used by the Enforcer to determine uniqueness of cache results.
070     *         Return {@code null} disable cache of rule executing.
071     */
072    public String getCacheId() {
073        return null;
074    }
075
076    /**
077     * This is the interface into the rule. This method should throw an exception
078     * containing a reason message if the rule fails the check. The plugin will
079     * then decide based on the fail flag and rule level if it should stop or just log the
080     * message as a warning.
081     *
082     * @throws EnforcerRuleException the enforcer rule exception
083     * @throws EnforcerRuleError in order to brake a build immediately
084     */
085    public abstract void execute() throws EnforcerRuleException;
086}