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 java.util.List;
23  
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
26  import org.apache.maven.plugin.logging.Log;
27  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import bsh.EvalError;
31  import bsh.Interpreter;
32  
33  /**
34   * Rule for Maven Enforcer using Beanshell to evaluate a conditional expression.
35   *
36   * @author hugonnem
37   */
38  public class EvaluateBeanshell
39      extends AbstractNonCacheableEnforcerRule
40  {
41  
42      /** Beanshell interpreter. */
43      private static final Interpreter bsh = new Interpreter();
44  
45      /** The condition to be evaluated.
46       *  
47       * @deprecated the visibility will be reduced to private with the next major version
48       * @see {@link #setCondition(String)}
49       * @see {@link #getCondition()}
50       * */
51      public String condition;
52  
53      public final void setCondition( String condition )
54      {
55          this.condition = condition;
56      }
57      
58      public final String getCondition()
59      {
60          return condition;
61      }
62  
63      /*
64       * (non-Javadoc)
65       *
66       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
67       */
68      public void execute( EnforcerRuleHelper helper )
69          throws EnforcerRuleException
70      {
71          Log log = helper.getLog();
72  
73          try
74          {
75              log.debug( "Echo condition : " + this.condition );
76              // Evaluate condition within Plexus Container
77              String script = (String) helper.evaluate( this.condition );
78              log.debug( "Echo script : " + script );
79              if ( !evaluateCondition( script, log ) )
80              {
81                  String message = getMessage();
82                  if ( StringUtils.isEmpty( message ) )
83                  {
84                      message = "The expression \"" + condition + "\" is not true.";
85                  }
86                  throw new EnforcerRuleException( message );
87              }
88          }
89          catch ( ExpressionEvaluationException e )
90          {
91              throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition + "'", e );
92          }
93      }
94  
95      /**
96       * Evaluate expression using Beanshell.
97       *
98       * @param script the expression to be evaluated
99       * @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 = Boolean.FALSE;
107         try
108         {
109             evaluation = (Boolean) bsh.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 }