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.Inject;
022import javax.inject.Named;
023
024import java.util.Objects;
025
026import bsh.EvalError;
027import bsh.Interpreter;
028import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
029import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
030import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
031
032/**
033 * Rule for Maven Enforcer using Beanshell to evaluate a conditional expression.
034 *
035 * @author hugonnem
036 * @deprecated BeanShell is being retired across Maven; the rule will be removed in a future major version. Express
037 *             the condition with one of the built-in rules such as {@code requireProperty}, or write a custom rule.
038 */
039@Deprecated
040@Named("evaluateBeanshell")
041public final class EvaluateBeanshell extends AbstractStandardEnforcerRule {
042
043    /** Beanshell interpreter. */
044    private final Interpreter interpreter = new Interpreter();
045
046    /** The condition to be evaluated.
047     * */
048    private String condition;
049
050    private final ExpressionEvaluator evaluator;
051
052    @Inject
053    public EvaluateBeanshell(ExpressionEvaluator evaluator) {
054        this.evaluator = Objects.requireNonNull(evaluator);
055    }
056
057    public void setCondition(String condition) {
058        this.condition = condition;
059    }
060
061    public String getCondition() {
062        return condition;
063    }
064
065    @Override
066    public void execute() throws EnforcerRuleException {
067        getLog().warn("The evaluateBeanshell rule is deprecated and will be removed in a future major version;"
068                + " express the condition with a built-in rule such as requireProperty, or write a custom rule.");
069
070        try {
071            getLog().debug("Echo condition : " + condition);
072            // Evaluate condition within Plexus Container
073            String script = (String) evaluator.evaluate(condition);
074            getLog().debug("Echo script : " + script);
075            if (!evaluateCondition(script)) {
076                String message = getMessage();
077                if (message == null || message.isEmpty()) {
078                    message = "The expression \"" + condition + "\" is not true.";
079                }
080                throw new EnforcerRuleException(message);
081            }
082        } catch (ExpressionEvaluationException e) {
083            throw new EnforcerRuleException("Unable to evaluate an expression '" + condition + "'", e);
084        }
085    }
086
087    /**
088     * Evaluate expression using Beanshell.
089     *
090     * @param script the expression to be evaluated
091     * @return boolean the evaluation of the expression
092     * @throws EnforcerRuleException if the script could not be evaluated
093     */
094    private boolean evaluateCondition(String script) throws EnforcerRuleException {
095        Boolean evaluation;
096        try {
097            evaluation = (Boolean) interpreter.eval(script);
098            getLog().debug("Echo evaluating : " + evaluation);
099        } catch (EvalError ex) {
100            throw new EnforcerRuleException("Couldn't evaluate condition: " + script, ex);
101        }
102        return evaluation;
103    }
104
105    @Override
106    public String toString() {
107        return String.format("EvaluateBeanshell[message=%s, condition=%s]", getMessage(), condition);
108    }
109}