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;
031import org.codehaus.plexus.util.StringUtils;
032
033/**
034 * Rule for Maven Enforcer using Beanshell to evaluate a conditional expression.
035 *
036 * @author hugonnem
037 */
038@Named("evaluateBeanshell")
039public final class EvaluateBeanshell extends AbstractStandardEnforcerRule {
040
041    /** Beanshell interpreter. */
042    private final Interpreter interpreter = new Interpreter();
043
044    /** The condition to be evaluated.
045     * */
046    private String condition;
047
048    private final ExpressionEvaluator evaluator;
049
050    @Inject
051    public EvaluateBeanshell(ExpressionEvaluator evaluator) {
052        this.evaluator = Objects.requireNonNull(evaluator);
053    }
054
055    public void setCondition(String condition) {
056        this.condition = condition;
057    }
058
059    public String getCondition() {
060        return condition;
061    }
062
063    @Override
064    public void execute() throws EnforcerRuleException {
065
066        try {
067            getLog().debug("Echo condition : " + condition);
068            // Evaluate condition within Plexus Container
069            String script = (String) evaluator.evaluate(condition);
070            getLog().debug("Echo script : " + script);
071            if (!evaluateCondition(script)) {
072                String message = getMessage();
073                if (StringUtils.isEmpty(message)) {
074                    message = "The expression \"" + condition + "\" is not true.";
075                }
076                throw new EnforcerRuleException(message);
077            }
078        } catch (ExpressionEvaluationException e) {
079            throw new EnforcerRuleException("Unable to evaluate an expression '" + condition + "'", e);
080        }
081    }
082
083    /**
084     * Evaluate expression using Beanshell.
085     *
086     * @param script the expression to be evaluated
087     * @return boolean the evaluation of the expression
088     * @throws EnforcerRuleException if the script could not be evaluated
089     */
090    private boolean evaluateCondition(String script) throws EnforcerRuleException {
091        Boolean evaluation;
092        try {
093            evaluation = (Boolean) interpreter.eval(script);
094            getLog().debug("Echo evaluating : " + evaluation);
095        } catch (EvalError ex) {
096            throw new EnforcerRuleException("Couldn't evaluate condition: " + script, ex);
097        }
098        return evaluation;
099    }
100
101    @Override
102    public String toString() {
103        return String.format("EvaluateBeanshell[message=%s, condition=%s]", getMessage(), condition);
104    }
105}