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