001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 023import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 024import org.apache.maven.plugin.logging.Log; 025import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 026import org.codehaus.plexus.util.StringUtils; 027 028import bsh.EvalError; 029import bsh.Interpreter; 030 031/** 032 * Rule for Maven Enforcer using Beanshell to evaluate a conditional expression. 033 * 034 * @author hugonnem 035 */ 036public class EvaluateBeanshell 037 extends AbstractNonCacheableEnforcerRule 038{ 039 040 /** Beanshell interpreter. */ 041 private static final Interpreter BSH = new Interpreter(); 042 043 /** The condition to be evaluated. 044 * 045 * @see {@link #setCondition(String)} 046 * @see {@link #getCondition()} 047 * */ 048 private String condition; 049 050 public final void setCondition( String condition ) 051 { 052 this.condition = condition; 053 } 054 055 public final String getCondition() 056 { 057 return condition; 058 } 059 060 @Override 061 public void execute( EnforcerRuleHelper helper ) 062 throws EnforcerRuleException 063 { 064 Log log = helper.getLog(); 065 066 try 067 { 068 log.debug( "Echo condition : " + this.condition ); 069 // Evaluate condition within Plexus Container 070 String script = (String) helper.evaluate( this.condition ); 071 log.debug( "Echo script : " + script ); 072 if ( !evaluateCondition( script, log ) ) 073 { 074 String message = getMessage(); 075 if ( StringUtils.isEmpty( message ) ) 076 { 077 message = "The expression \"" + condition + "\" is not true."; 078 } 079 throw new EnforcerRuleException( message ); 080 } 081 } 082 catch ( ExpressionEvaluationException e ) 083 { 084 throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition + "'", e ); 085 } 086 } 087 088 /** 089 * Evaluate expression using Beanshell. 090 * 091 * @param script the expression to be evaluated 092 * @param log the logger 093 * @return boolean the evaluation of the expression 094 * @throws EnforcerRuleException if the script could not be evaluated 095 */ 096 protected boolean evaluateCondition( String script, Log log ) 097 throws EnforcerRuleException 098 { 099 Boolean evaluation = Boolean.FALSE; 100 try 101 { 102 evaluation = (Boolean) BSH.eval( script ); 103 log.debug( "Echo evaluating : " + evaluation ); 104 } 105 catch ( EvalError ex ) 106 { 107 throw new EnforcerRuleException( "Couldn't evaluate condition: " + script, ex ); 108 } 109 return evaluation.booleanValue(); 110 } 111}