KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > listeners > FilterAndBroadcastJobListener


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.listeners;
17
18 import java.util.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.quartz.JobListener;
23 import org.quartz.JobExecutionContext;
24 import org.quartz.JobExecutionException;
25 import org.quartz.JobDetail;
26
27 /**
28  * Holds a List of references to JobListener instances and broadcasts all
29  * events to them (in order) - if the event is not excluded via filtering
30  * (read on).
31  *
32  * <p>The broadcasting behavior of this listener to delegate listeners may be
33  * more convenient than registering all of the listeners directly with the
34  * Trigger, and provides the flexibility of easily changing which listeners
35  * get notified.</p>
36  *
37  * <p>You may also register a number of Regular Expression patterns to match
38  * the events against. If one or more patterns are registered, the broadcast
39  * will only take place if the event applies to a job who's name/group
40  * matches one or more of the patterns.</p>
41  *
42  * @see #addListener(org.quartz.JobListener)
43  * @see #removeListener(org.quartz.JobListener)
44  * @see #removeListener(String)
45  * @see #addJobNamePattern(String)
46  * @see #addJobGroupPattern(String)
47  *
48  * @author James House (jhouse AT revolition DOT net)
49  */

50 public class FilterAndBroadcastJobListener implements JobListener {
51
52     private String JavaDoc name;
53     private List JavaDoc listeners;
54     private List JavaDoc namePatterns = new LinkedList JavaDoc();
55     private List JavaDoc groupPatterns = new LinkedList JavaDoc();
56
57     /**
58      * Construct an instance with the given name.
59      *
60      * (Remember to add some delegate listeners!)
61      *
62      * @param name the name of this instance
63      */

64     public FilterAndBroadcastJobListener(String JavaDoc name) {
65         if(name == null)
66             throw new IllegalArgumentException JavaDoc("Listener name cannot be null!");
67         this.name = name;
68         listeners = new LinkedList JavaDoc();
69     }
70
71     /**
72      * Construct an instance with the given name, and List of listeners.
73      *
74      * @param name the name of this instance
75      * @param listeners the initial List of JobListeners to broadcast to.
76      */

77     public FilterAndBroadcastJobListener(String JavaDoc name, List JavaDoc listeners) {
78         this(name);
79         this.listeners.addAll(listeners);
80     }
81
82     public String JavaDoc getName() {
83         return name;
84     }
85
86     public void addListener(JobListener listener) {
87         listeners.add(listener);
88     }
89
90     public boolean removeListener(JobListener listener) {
91         return listeners.remove(listener);
92     }
93
94     public boolean removeListener(String JavaDoc listenerName) {
95         Iterator JavaDoc itr = listeners.iterator();
96         while(itr.hasNext()) {
97             JobListener jl = (JobListener) itr.next();
98             if(jl.getName().equals(listenerName)) {
99                 itr.remove();
100                 return true;
101             }
102         }
103         return false;
104     }
105
106     public List JavaDoc getListeners() {
107         return java.util.Collections.unmodifiableList(listeners);
108     }
109
110     /**
111      * If one or more name patterns are specified, only events relating to
112      * jobs who's name matches the given regular expression pattern
113      * will be dispatched to the delegate listeners.
114      *
115      * @param regularExpression
116      */

117     public void addJobNamePattern(String JavaDoc regularExpression) {
118         if(regularExpression == null)
119             throw new IllegalArgumentException JavaDoc("Expression cannot be null!");
120
121         namePatterns.add(regularExpression);
122     }
123
124     public List JavaDoc getJobNamePatterns() {
125         return namePatterns;
126     }
127
128     /**
129      * If one or more group patterns are specified, only events relating to
130      * jobs who's group matches the given regular expression pattern
131      * will be dispatched to the delegate listeners.
132      *
133      * @param regularExpression
134      */

135     public void addJobGroupPattern(String JavaDoc regularExpression) {
136         if(regularExpression == null)
137             throw new IllegalArgumentException JavaDoc("Expression cannot be null!");
138
139         groupPatterns.add(regularExpression);
140     }
141
142     public List JavaDoc getJobGroupPatterns() {
143         return namePatterns;
144     }
145
146     protected boolean shouldDispatch(JobExecutionContext context) {
147         JobDetail job = context.getJobDetail();
148
149         if(namePatterns.size() == 0 && groupPatterns.size() == 0)
150             return true;
151
152         Iterator JavaDoc itr = groupPatterns.iterator();
153         while(itr.hasNext()) {
154             String JavaDoc pat = (String JavaDoc) itr.next();
155             if(job.getGroup().matches(pat))
156                 return true;
157         }
158
159         itr = namePatterns.iterator();
160         while(itr.hasNext()) {
161             String JavaDoc pat = (String JavaDoc) itr.next();
162             if(job.getName().matches(pat))
163                 return true;
164         }
165
166         return false;
167     }
168
169     public void jobToBeExecuted(JobExecutionContext context) {
170
171         if(!shouldDispatch(context))
172             return;
173
174         Iterator JavaDoc itr = listeners.iterator();
175         while(itr.hasNext()) {
176             JobListener jl = (JobListener) itr.next();
177             jl.jobToBeExecuted(context);
178         }
179     }
180
181     public void jobExecutionVetoed(JobExecutionContext context) {
182
183         if(!shouldDispatch(context))
184             return;
185
186         Iterator JavaDoc itr = listeners.iterator();
187         while(itr.hasNext()) {
188             JobListener jl = (JobListener) itr.next();
189             jl.jobExecutionVetoed(context);
190         }
191     }
192
193     public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
194
195         if(!shouldDispatch(context))
196             return;
197
198         Iterator JavaDoc itr = listeners.iterator();
199         while(itr.hasNext()) {
200             JobListener jl = (JobListener) itr.next();
201             jl.jobWasExecuted(context, jobException);
202         }
203     }
204
205
206 }
207
Popular Tags