View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
23  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
24  import org.apache.maven.plugin.logging.Log;
25  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  import bsh.EvalError;
29  import bsh.Interpreter;
30  
31  /**
32   * Rule for Maven Enforcer using Beanshell to evaluate a conditional expression.
33   *
34   * @author hugonnem
35   */
36  public class EvaluateBeanshell
37      extends AbstractNonCacheableEnforcerRule
38  {
39  
40      /** Beanshell interpreter. */
41      private static final Interpreter BSH = new Interpreter();
42  
43      /** The condition to be evaluated.
44       *  
45       * @see {@link #setCondition(String)}
46       * @see {@link #getCondition()}
47       * */
48      private String condition;
49  
50      public final void setCondition( String condition )
51      {
52          this.condition = condition;
53      }
54      
55      public final String getCondition()
56      {
57          return condition;
58      }
59  
60      @Override
61      public void execute( EnforcerRuleHelper helper )
62          throws EnforcerRuleException
63      {
64          Log log = helper.getLog();
65  
66          try
67          {
68              log.debug( "Echo condition : " + this.condition );
69              // Evaluate condition within Plexus Container
70              String script = (String) helper.evaluate( this.condition );
71              log.debug( "Echo script : " + script );
72              if ( !evaluateCondition( script, log ) )
73              {
74                  String message = getMessage();
75                  if ( StringUtils.isEmpty( message ) )
76                  {
77                      message = "The expression \"" + condition + "\" is not true.";
78                  }
79                  throw new EnforcerRuleException( message );
80              }
81          }
82          catch ( ExpressionEvaluationException e )
83          {
84              throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition + "'", e );
85          }
86      }
87  
88      /**
89       * Evaluate expression using Beanshell.
90       *
91       * @param script the expression to be evaluated
92       * @param log the logger
93       * @return boolean the evaluation of the expression
94       * @throws EnforcerRuleException if the script could not be evaluated
95       */
96      protected boolean evaluateCondition( String script, Log log )
97          throws EnforcerRuleException
98      {
99          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 }