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
021import javax.annotation.Nonnull;
022import javax.annotation.Nullable;
023
024/**
025 * Interface to be implemented by any rules executed by the enforcer.
026 *
027 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
028 * @deprecated Please see
029 *         <a href="https://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html">Writing a custom rule</a>
030 */
031@Deprecated
032public interface EnforcerRule extends EnforcerRuleBase {
033
034    /**
035     * This is the interface into the rule. This method should throw an exception
036     * containing a reason message if the rule fails the check. The plugin will
037     * then decide based on the fail flag if it should stop or just log the
038     * message as a warning.
039     *
040     * @param helper The helper provides access to the log, MavenSession and has
041     *               helpers to get common components. It is also able to lookup components
042     *               by class name.
043     * @throws EnforcerRuleException the enforcer rule exception
044     */
045    void execute(@Nonnull EnforcerRuleHelper helper) throws EnforcerRuleException;
046
047    /**
048     * This method tells the enforcer if the rule results may be cached. If the result is true,
049     * the results will be remembered for future executions in the same build (ie children). Subsequent
050     * iterations of the rule will be queried to see if they are also cacheable. This will allow the rule to be
051     * uncached further down the tree if needed.
052     *
053     * @return <code>true</code> if rule is cacheable
054     */
055    boolean isCacheable();
056
057    /**
058     * If the rule is cacheable and the same id is found in the cache, the stored results are passed to this method to
059     * allow double checking of the results. Most of the time this can be done by generating unique ids, but sometimes
060     * the results of objects returned by the helper need to be queried. You may for example, store certain objects in
061     * your rule and then query them later.
062     *
063     * @param cachedRule the last cached instance of the rule. This is to be used by the rule to
064     *                   potentially determine if the results are still valid (ie if the configuration has been
065     *                   overridden)
066     * @return <code>true</code> if the stored results are valid for the same id.
067     */
068    boolean isResultValid(@Nonnull EnforcerRule cachedRule);
069
070    /**
071     * If the rule is to be cached, this id is used as part of the key. This can allow rules to take parameters
072     * that allow multiple results of the same rule to be cached.
073     *
074     * @return id to be used by the enforcer to determine uniqueness of cache results. The ids only need to be unique
075     *         within a given rule implementation as the full key will be [classname]-[id]
076     */
077    @Nullable
078    String getCacheId();
079}