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