KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > ee > jmx > jboss > QuartzService


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

18
19 /*
20  * Previously Copyright (c) 2001-2004 James House
21  */

22 package org.quartz.ee.jmx.jboss;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import org.quartz.Scheduler;
34 import org.quartz.SchedulerConfigException;
35 import org.quartz.SchedulerException;
36 import org.quartz.impl.StdSchedulerFactory;
37
38 import org.jboss.naming.NonSerializableFactory;
39 import org.jboss.system.ServiceMBeanSupport;
40
41 /**
42  * JBoss specific MBean implementation for configuring, starting, and
43  * binding to JNDI a Quartz Scheduler instance.
44  *
45  * <p>
46  * Sample MBean deployment descriptor:
47  * <a HREF="doc-files/quartz-service.xml" type="text/plain">quartz-service.xml</a>
48  * </p>
49  *
50  * <p>
51  * <b>Note:</b> The Scheduler instance bound to JNDI is not Serializable, so
52  * you will get a null reference back if you try to retrieve it from outside
53  * the JBoss server in which it was bound. If you have a need for remote
54  * access to a Scheduler instance you may want to consider using Quartz's RMI
55  * support instead.
56  * </p>
57  *
58  * @see org.quartz.ee.jmx.jboss.QuartzServiceMBean
59  *
60  * @author Andrew Collins
61  */

62 public class QuartzService extends ServiceMBeanSupport implements
63         QuartzServiceMBean {
64
65     /*
66      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67      *
68      * Data members.
69      *
70      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71      */

72
73     private Properties JavaDoc properties;
74
75     private StdSchedulerFactory schedulerFactory;
76
77     private String JavaDoc jndiName;
78
79     private String JavaDoc propertiesFile;
80
81     private boolean error;
82
83     private boolean useProperties;
84
85     private boolean usePropertiesFile;
86
87     /*
88     * If true, the scheduler will be started. If false, the scheduler is initailized
89     * (and available) but start() is not called - it will not execute jobs.
90     */

91     private boolean startScheduler = true;
92     
93     /*
94      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95      *
96      * Constructors.
97      *
98      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99      */

100
101     public QuartzService() {
102         // flag initialization errors
103
error = false;
104
105         // use PropertiesFile attribute
106
usePropertiesFile = false;
107         propertiesFile = "";
108
109         // use Properties attribute
110
useProperties = false;
111         properties = new Properties JavaDoc();
112
113         // default JNDI name for Scheduler
114
jndiName = "Quartz";
115     }
116
117     /*
118      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119      *
120      * Interface.
121      *
122      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123      */

124
125     public void setJndiName(String JavaDoc jndiName) throws Exception JavaDoc {
126         String JavaDoc oldName = this.jndiName;
127         this.jndiName = jndiName;
128
129         if (super.getState() == STARTED) {
130             unbind(oldName);
131
132             try {
133                 rebind();
134             } catch (NamingException JavaDoc ne) {
135                 log.error("Failed to rebind Scheduler", ne);
136
137                 throw new SchedulerConfigException(
138                         "Failed to rebind Scheduler - ", ne);
139             }
140         }
141     }
142
143     public String JavaDoc getJndiName() {
144         return jndiName;
145     }
146
147     public String JavaDoc getName() {
148         return "QuartzService(" + jndiName + ")";
149     }
150
151     public void setProperties(String JavaDoc properties) {
152         if (usePropertiesFile) {
153             log
154                     .error("Must specify only one of 'Properties' or 'PropertiesFile'");
155
156             error = true;
157
158             return;
159         }
160
161         useProperties = true;
162
163         try {
164             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(properties
165                     .getBytes());
166             this.properties = new Properties JavaDoc();
167             this.properties.load(bais);
168         } catch (IOException JavaDoc ioe) {
169             // should not happen
170
}
171     }
172
173     public String JavaDoc getProperties() {
174         try {
175             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
176             properties.store(baos, "");
177
178             return new String JavaDoc(baos.toByteArray());
179         } catch (IOException JavaDoc ioe) {
180             // should not happen
181
return "";
182         }
183     }
184
185     public void setPropertiesFile(String JavaDoc propertiesFile) {
186         if (useProperties) {
187             log
188                     .error("Must specify only one of 'Properties' or 'PropertiesFile'");
189
190             error = true;
191
192             return;
193         }
194
195         usePropertiesFile = true;
196
197         this.propertiesFile = propertiesFile;
198     }
199
200     public String JavaDoc getPropertiesFile() {
201         return propertiesFile;
202     }
203
204     public void setStartScheduler(boolean startScheduler) {
205         this.startScheduler = startScheduler;
206     }
207     
208     public boolean getStartScheduler() {
209         return startScheduler;
210     }
211     
212     public void createService() throws Exception JavaDoc {
213         log.info("Create QuartzService(" + jndiName + ")...");
214
215         if (error) {
216             log
217                     .error("Must specify only one of 'Properties' or 'PropertiesFile'");
218
219             throw new Exception JavaDoc(
220                     "Must specify only one of 'Properties' or 'PropertiesFile'");
221         }
222
223         schedulerFactory = new StdSchedulerFactory();
224
225         try {
226             if (useProperties) {
227                 schedulerFactory.initialize(properties);
228             }
229
230             if (usePropertiesFile) {
231                 schedulerFactory.initialize(propertiesFile);
232             }
233         } catch (Exception JavaDoc e) {
234             log.error("Failed to initialize Scheduler", e);
235
236             throw new SchedulerConfigException(
237                     "Failed to initialize Scheduler - ", e);
238         }
239
240         log.info("QuartzService(" + jndiName + ") created.");
241     }
242
243     public void destroyService() throws Exception JavaDoc {
244         log.info("Destroy QuartzService(" + jndiName + ")...");
245
246         schedulerFactory = null;
247
248         log.info("QuartzService(" + jndiName + ") destroyed.");
249     }
250
251     public void startService() throws Exception JavaDoc {
252         log.info("Start QuartzService(" + jndiName + ")...");
253
254         try {
255             rebind();
256         } catch (NamingException JavaDoc ne) {
257             log.error("Failed to rebind Scheduler", ne);
258
259             throw new SchedulerConfigException("Failed to rebind Scheduler - ",
260                     ne);
261         }
262
263         try {
264             Scheduler scheduler = schedulerFactory.getScheduler();
265
266             if (startScheduler) {
267                 scheduler.start();
268             } else {
269                 log.info("Skipping starting the scheduler (will not run jobs).");
270             }
271         } catch (Exception JavaDoc e) {
272             log.error("Failed to start Scheduler", e);
273
274             throw new SchedulerConfigException("Failed to start Scheduler - ",
275                     e);
276         }
277
278         log.info("QuartzService(" + jndiName + ") started.");
279     }
280
281     public void stopService() throws Exception JavaDoc {
282         log.info("Stop QuartzService(" + jndiName + ")...");
283
284         try {
285             Scheduler scheduler = schedulerFactory.getScheduler();
286
287             scheduler.shutdown();
288         } catch (Exception JavaDoc e) {
289             log.error("Failed to shutdown Scheduler", e);
290
291             throw new SchedulerConfigException(
292                     "Failed to shutdown Scheduler - ", e);
293         }
294
295         unbind(jndiName);
296
297         log.info("QuartzService(" + jndiName + ") stopped.");
298     }
299
300     private void rebind() throws NamingException JavaDoc, SchedulerException {
301         InitialContext JavaDoc rootCtx = null;
302         try {
303             rootCtx = new InitialContext JavaDoc();
304             Name JavaDoc fullName = rootCtx.getNameParser("").parse(jndiName);
305             Scheduler scheduler = schedulerFactory.getScheduler();
306             NonSerializableFactory.rebind(fullName, scheduler, true);
307         } finally {
308             if (rootCtx != null) {
309                 try {
310                     rootCtx.close();
311                 } catch (NamingException JavaDoc ignore) {}
312             }
313         }
314     }
315     
316     private void unbind(String JavaDoc jndiName) {
317         InitialContext JavaDoc rootCtx = null;
318         try {
319             rootCtx = new InitialContext JavaDoc();
320             rootCtx.unbind(jndiName);
321             NonSerializableFactory.unbind(jndiName);
322         } catch (NamingException JavaDoc e) {
323             log.warn("Failed to unbind scheduler with jndiName: " + jndiName, e);
324         } finally {
325             if (rootCtx != null) {
326                 try {
327                     rootCtx.close();
328                 } catch (NamingException JavaDoc ignore) {}
329             }
330         }
331     }
332 }
333
Popular Tags