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 ThreadLocal<Interpreter> INTERPRETER = new ThreadLocal<Interpreter>()
042    {
043        @Override
044        protected Interpreter initialValue()
045        {
046            return new Interpreter();
047        }
048    };
049
050    /** The condition to be evaluated.
051     *  
052     * @see {@link #setCondition(String)}
053     * @see {@link #getCondition()}
054     * */
055    private String condition;
056
057    public final void setCondition( String condition )
058    {
059        this.condition = condition;
060    }
061    
062    public final String getCondition()
063    {
064        return condition;
065    }
066
067    @Override
068    public void execute( EnforcerRuleHelper helper )
069        throws EnforcerRuleException
070    {
071        Log log = helper.getLog();
072
073        try
074        {
075            log.debug( "Echo condition : " + this.condition );
076            // Evaluate condition within Plexus Container
077            String script = (String) helper.evaluate( this.condition );
078            log.debug( "Echo script : " + script );
079            if ( !evaluateCondition( script, log ) )
080            {
081                String message = getMessage();
082                if ( StringUtils.isEmpty( message ) )
083                {
084                    message = "The expression \"" + condition + "\" is not true.";
085                }
086                throw new EnforcerRuleException( message );
087            }
088        }
089        catch ( ExpressionEvaluationException e )
090        {
091            throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition + "'", e );
092        }
093    }
094
095    /**
096     * Evaluate expression using Beanshell.
097     *
098     * @param script the expression to be evaluated
099     * @param log the logger
100     * @return boolean the evaluation of the expression
101     * @throws EnforcerRuleException if the script could not be evaluated
102     */
103    protected boolean evaluateCondition( String script, Log log )
104        throws EnforcerRuleException
105    {
106        Boolean evaluation;
107        try
108        {
109            evaluation = (Boolean) INTERPRETER.get().eval( script );
110            log.debug( "Echo evaluating : " + evaluation );
111        }
112        catch ( EvalError ex )
113        {
114            throw new EnforcerRuleException( "Couldn't evaluate condition: " + script, ex );
115        }
116        return evaluation.booleanValue();
117    }
118}