1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36 public class EvaluateBeanshell
37 extends AbstractNonCacheableEnforcerRule
38 {
39
40
41 private static final Interpreter bsh = new Interpreter();
42
43
44 public String condition;
45
46
47
48
49
50
51 public void execute( EnforcerRuleHelper helper )
52 throws EnforcerRuleException
53 {
54 Log log = helper.getLog();
55
56 try
57 {
58 log.debug( "Echo condition : " + this.condition );
59
60 String script = (String) helper.evaluate( this.condition );
61 log.debug( "Echo script : " + script );
62 if ( !evaluateCondition( script, log ) )
63 {
64 if ( StringUtils.isEmpty( message ) )
65 {
66 message = "The expression \"" + condition + "\" is not true.";
67 }
68 throw new EnforcerRuleException( this.message );
69 }
70 }
71 catch ( ExpressionEvaluationException e )
72 {
73 throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition + "'", e );
74 }
75 }
76
77
78
79
80
81
82
83
84
85 protected boolean evaluateCondition( String script, Log log )
86 throws EnforcerRuleException
87 {
88 Boolean evaluation = Boolean.FALSE;
89 try
90 {
91 evaluation = (Boolean) bsh.eval( script );
92 log.debug( "Echo evaluating : " + evaluation );
93 }
94 catch ( EvalError ex )
95 {
96 throw new EnforcerRuleException( "Couldn't evaluate condition: " + script, ex );
97 }
98 return evaluation.booleanValue();
99 }
100 }