View Javadoc

1   /*$Id: Rule.java,v 1.8 2004/11/05 16:52:42 jdt Exp $
2    * Created on 26-Jul-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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      // getters and setters
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 $Log: Rule.java,v $
187 Revision 1.8  2004/11/05 16:52:42  jdt
188 Merges from branch nww-itn07-scratchspace
189 
190 Revision 1.7.26.1  2004/11/05 16:04:56  nw
191 removed cached trigger
192 cached body in soft reference.
193 updated rewrite() to work with new rulestore (map-based)
194 
195 Revision 1.7  2004/09/06 16:47:04  nw
196 javadoc
197 
198 Revision 1.6  2004/08/18 21:50:15  nw
199 improved error propagation and reporting.
200 messages are now logged to workflow document
201 
202 Revision 1.5  2004/08/13 09:10:30  nw
203 tidied imports
204 
205 Revision 1.4  2004/08/09 17:32:53  nw
206 optimized string handling - all done via regexps now.
207 
208 Revision 1.3  2004/08/03 16:32:26  nw
209 remove unnecessary envId attrib from rules
210 implemented variable propagation into parameter values.
211 
212 Revision 1.2  2004/07/30 15:42:34  nw
213 merged in branch nww-itn06-bz#441 (groovy scripting)
214 
215 Revision 1.1.2.4  2004/07/30 14:00:10  nw
216 first working draft
217 
218 Revision 1.1.2.3  2004/07/28 16:24:23  nw
219 finished groovy beans.
220 moved useful tests from old python package.
221 removed python implemntation
222 
223 Revision 1.1.2.2  2004/07/27 23:37:59  nw
224 refactoed framework.
225 experimented with betwixt - can't get it to work.
226 got XStream working in 5 mins.
227 about to remove betwixt code.
228 
229 Revision 1.1.2.1  2004/07/26 15:51:19  nw
230 first stab at a groovy scheduler.
231 transcribed all the classes in the python prototype, and took copies of the
232 classes in the 'scripting' package.
233  
234 */