1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.jes.jobscheduler.impl.groovy;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.codehaus.groovy.control.CompilationFailedException;
16
17 import groovy.lang.Script;
18
19 import java.io.IOException;
20 import java.lang.ref.SoftReference;
21 import java.util.Map;
22 import java.util.regex.Pattern;
23
24 /*** represents a rule that is fired - has a trigger, name, and body of actions to execute
25 Written as a bean, for easy serialization.
26 <p>
27 also caches compiled version of body after first use - for faster execution the next time round.
28 @modified - changed these cached scripts to be soft references - don't want the code blowing up unnecessarily.
29 * @author Noel Winstanley nw@jb.man.ac.uk 26-Jul-2004
30 *
31 */
32 public class Rule {
33 /***
34 * Commons Logger for this class
35 */
36 private static final Log logger = LogFactory.getLog(Rule.class);
37
38 /*** Construct a new Rule
39 *
40 */
41 public Rule() {
42 super();
43 }
44 protected String trigger;
45 protected String name;
46 protected String body;
47
48 /*** cache variable, not persisted */
49 protected transient SoftReference compiledBody;
50
51 /*** execute the body of a rule
52 * @throws IOException
53 * @throws CompilationFailedExceptiont*/
54 public void fire(JesShell shell,ActivityStatusStore statusStore, Map rules) throws CompilationFailedException, IOException {
55 shell.executeBody(this,statusStore,rules);
56 }
57
58 /*** return true if any trigger, env or body action references this key */
59 public boolean matches(Pattern p) {
60
61 return p.matcher(this.trigger).find() || p.matcher(this.body).find();
62 }
63
64 /*** create a copy of this rule, rewritten so that all references to old key reference new key*/
65 public Rule rewriteAs(Pattern regexp, int branchId) {
66 Rule result = new Rule();
67 result.setName(this.name + "-" + branchId);
68 String replacement = "'$1-" + branchId + "'";
69 result.setTrigger(regexp.matcher(this.trigger).replaceAll(replacement));
70 result.setBody(regexp.matcher(this.body).replaceAll(replacement));
71 logger.debug("generated new rule " + result.toString());
72 return result;
73 }
74
75
76
77
78 /***
79 * @return Returns the body.
80 */
81 public String getBody() {
82 return this.body;
83 }
84 /***
85 * @param body The body to set.
86 */
87 public void setBody(String body) {
88 this.body = body.trim();
89 }
90 /***
91 * @return Returns the envId.
92 */
93
94 /***
95 * @return Returns the name.
96 */
97 public String getName() {
98 return this.name;
99 }
100 /***
101 * @param name The name to set.
102 */
103 public void setName(String name) {
104 this.name = name.trim();
105 }
106 /***
107 * @return Returns the trigger.
108 */
109 public String getTrigger() {
110 return this.trigger;
111 }
112 /***
113 * @param trigger The trigger to set.
114 */
115 public void setTrigger(String trigger) {
116 this.trigger = trigger.trim();
117 }
118
119
120 public void setCompiledBody(Script script) {
121 this.compiledBody = new SoftReference(script);
122 }
123
124 public Script getCompiledBody() {
125 if (compiledBody == null) {
126 return null;
127 }
128 return (Script)this.compiledBody.get();
129 }
130
131 public String toString() {
132 StringBuffer buffer = new StringBuffer();
133 buffer.append("[Rule:");
134 buffer.append(" trigger: ");
135 buffer.append(trigger);
136 buffer.append(" name: ");
137 buffer.append(name);
138 buffer.append(" body: ");
139 buffer.append(body);
140 buffer.append("]");
141 return buffer.toString();
142 }
143
144 /***
145 * Returns <code>true</code> if this <code>Rule</code> is the same as the o argument.
146 *
147 * @return <code>true</code> if this <code>Rule</code> is the same as the o argument.
148 */
149 public boolean equals(Object o) {
150 if (this == o) {
151 return true;
152 }
153 if (o == null) {
154 return false;
155 }
156 if (o.getClass() != getClass()) {
157 return false;
158 }
159 Rule castedObj = (Rule) o;
160 return ((this.trigger == null
161 ? castedObj.trigger == null
162 : this.trigger.equals(castedObj.trigger))
163 && (this.name == null ? castedObj.name == null : this.name
164 .equals(castedObj.name))
165 && (this.body == null ? castedObj.body == null : this.body
166 .equals(castedObj.body))) ;
167 }
168 /***
169 * Override hashCode.
170 *
171 * @return the Objects hashcode.
172 */
173 public int hashCode() {
174 int hashCode = 1;
175 hashCode = 31 * hashCode + (logger == null ? 0 : logger.hashCode());
176 hashCode = 31 * hashCode + (trigger == null ? 0 : trigger.hashCode());
177 hashCode = 31 * hashCode + (name == null ? 0 : name.hashCode());
178 hashCode = 31 * hashCode + (body == null ? 0 : body.hashCode());
179
180 return hashCode;
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234