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 /* 061 * (non-Javadoc) 062 * 063 * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper) 064 */ 065 public void execute( EnforcerRuleHelper helper ) 066 throws EnforcerRuleException 067 { 068 Log log = helper.getLog(); 069 070 try 071 { 072 log.debug( "Echo condition : " + this.condition ); 073 // Evaluate condition within Plexus Container 074 String script = (String) helper.evaluate( this.condition ); 075 log.debug( "Echo script : " + script ); 076 if ( !evaluateCondition( script, log ) ) 077 { 078 String message = getMessage(); 079 if ( StringUtils.isEmpty( message ) ) 080 { 081 message = "The expression \"" + condition + "\" is not true."; 082 } 083 throw new EnforcerRuleException( message ); 084 } 085 } 086 catch ( ExpressionEvaluationException e ) 087 { 088 throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition + "'", e ); 089 } 090 } 091 092 /** 093 * Evaluate expression using Beanshell. 094 * 095 * @param script the expression to be evaluated 096 * @param log the logger 097 * @return boolean the evaluation of the expression 098 * @throws EnforcerRuleException if the script could not be evaluated 099 */ 100 protected boolean evaluateCondition( String script, Log log ) 101 throws EnforcerRuleException 102 { 103 Boolean evaluation = Boolean.FALSE; 104 try 105 { 106 evaluation = (Boolean) BSH.eval( script ); 107 log.debug( "Echo evaluating : " + evaluation ); 108 } 109 catch ( EvalError ex ) 110 { 111 throw new EnforcerRuleException( "Couldn't evaluate condition: " + script, ex ); 112 } 113 return evaluation.booleanValue(); 114 } 115}