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 ThreadLocal<Interpreter> INTERPRETER = new ThreadLocal<Interpreter>()
42 {
43 @Override
44 protected Interpreter initialValue()
45 {
46 return new Interpreter();
47 }
48 };
49
50
51
52
53
54
55 private String condition;
56
57 public final void setCondition( String condition )
58 {
59 this.condition = condition;
60 }
61
62 public final String getCondition()
63 {
64 return condition;
65 }
66
67 @Override
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
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
97
98
99
100
101
102
103 protected boolean evaluateCondition( String script, Log log )
104 throws EnforcerRuleException
105 {
106 Boolean evaluation;
107 try
108 {
109 evaluation = (Boolean) INTERPRETER.get().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 }