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
45
46
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
61
62
63
64
65 public void execute( EnforcerRuleHelper helper )
66 throws EnforcerRuleException
67 {
68 Log log = helper.getLog();
69
70 try
71 {
72 log.debug( "Echo condition : " + this.condition );
73
74 String script = (String) helper.evaluate( this.condition );
75 log.debug( "Echo script : " + script );
76 if ( !evaluateCondition( script, log ) )
77 {
78 String message = getMessage();
79 if ( StringUtils.isEmpty( message ) )
80 {
81 message = "The expression \"" + condition + "\" is not true.";
82 }
83 throw new EnforcerRuleException( message );
84 }
85 }
86 catch ( ExpressionEvaluationException e )
87 {
88 throw new EnforcerRuleException( "Unable to evaluate an expression '" + condition + "'", e );
89 }
90 }
91
92
93
94
95
96
97
98
99
100 protected boolean evaluateCondition( String script, Log log )
101 throws EnforcerRuleException
102 {
103 Boolean evaluation = Boolean.FALSE;
104 try
105 {
106 evaluation = (Boolean) BSH.eval( script );
107 log.debug( "Echo evaluating : " + evaluation );
108 }
109 catch ( EvalError ex )
110 {
111 throw new EnforcerRuleException( "Couldn't evaluate condition: " + script, ex );
112 }
113 return evaluation.booleanValue();
114 }
115 }