KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > jobs > QuartzInitializerServlet


1 /*
2  * Copyright James House (c) 2001-2004
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met: 1.
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer. 2. Redistributions in
10  * binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other
12  * materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */

26
27 package org.infoglue.deliver.jobs;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import javax.servlet.ServletConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.quartz.Scheduler;
39 import org.quartz.impl.StdSchedulerFactory;
40
41 /**
42  * <p>
43  * A Servlet that can be used to initialize Quartz, if configured as a
44  * load-on-startup servlet in a web application.
45  * </p>
46  *
47  * <p>
48  * You'll want to add something like this to your WEB-INF/web.xml file:
49  *
50  * <pre>
51  * &lt;servlet&gt;
52  * &lt;servlet-name&gt;
53  * QuartzInitializer
54  * &lt;/servlet-name&gt;
55  * &lt;display-name&gt;
56  * Quartz Initializer Servlet
57  * &lt;/display-name&gt;
58  * &lt;servlet-class&gt;
59  * org.quartz.ee.servlet.QuartzInitializerServlet
60  * &lt;/servlet-class&gt;
61  * &lt;load-on-startup&gt;
62  * 1
63  * &lt;/load-on-startup&gt;
64  * &lt;init-param&gt;
65  * &lt;param-name&gt;config-file&lt;/param-name&gt;
66  * &lt;param-value&gt;/some/path/my_quartz.properties&lt;/param-value&gt;
67  * &lt;/init-param&gt;
68  * &lt;init-param&gt;
69  * &lt;param-name&gt;shutdown-on-unload&lt;/param-name&gt;
70  * &lt;param-value&gt;true&lt;/param-value&gt;
71  * &lt;/init-param&gt;
72  * &lt;/servlet&gt;
73  * </pre>
74  * </p>
75  * <p>
76  * The init parameter 'config-file' can be used to specify the path (and
77  * filename) of your Quartz properties file. If you leave out this parameter,
78  * the default ("quartz.properties") will be used.
79  * </p>
80  *
81  * <p>
82  * The init parameter 'shutdown-on-unload' can be used to specify whether you
83  * want scheduler.shutdown() called when the servlet is unloaded (usually when
84  * the application server is being shutdown). Possible values are "true" or
85  * "false". The default is "true".
86  * </p>
87  *
88  * @author James House
89  */

90 public class QuartzInitializerServlet extends HttpServlet JavaDoc {
91
92     private boolean performShutdown = true;
93     
94     /*
95      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96      *
97      * Interface.
98      *
99      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100      */

101
102     public void init(ServletConfig JavaDoc cfg) throws javax.servlet.ServletException JavaDoc {
103         super.init(cfg);
104
105         log("Quartz Initializer Servlet loaded, initializing Scheduler...");
106         
107         StdSchedulerFactory factory;
108         try {
109
110             String JavaDoc configFile = cfg.getInitParameter("config-file");
111             String JavaDoc shutdownPref = cfg.getInitParameter("shutdown-on-unload");
112             if(configFile == null)
113             {
114                 String JavaDoc contextRootPath = cfg.getServletContext().getRealPath("/");
115                 if(!contextRootPath.endsWith("/") && !contextRootPath.endsWith("\\"))
116                     contextRootPath = contextRootPath + File.separator;
117
118                 configFile = contextRootPath + "WEB-INF" + File.separator + "classes" + File.separator + "quartz.properties";
119             }
120             
121             if(shutdownPref != null)
122                 performShutdown = Boolean.valueOf(shutdownPref).booleanValue();
123                 
124             // get Properties
125
if (configFile != null)
126             {
127                 factory = new StdSchedulerFactory(configFile);
128             }
129             else
130             {
131                 factory = new StdSchedulerFactory();
132             }
133     
134             Scheduler scheduler = factory.getScheduler();
135             scheduler.start();
136     
137           } catch (Exception JavaDoc e) {
138               e.printStackTrace();
139               log("Quartz Scheduler failed to initialize: " + e.toString());
140               throw new ServletException JavaDoc(e);
141           }
142     }
143
144     public void destroy() {
145         
146         if(!performShutdown)
147             return;
148         
149         try {
150             Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
151
152             if (sched != null) sched.shutdown();
153         } catch (Exception JavaDoc e) {
154             log("Quartz Scheduler failed to shutdown cleanly: " + e.toString());
155             e.printStackTrace();
156         }
157
158         log("Quartz Scheduler successful shutdown.");
159     }
160
161     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
162             throws ServletException JavaDoc, IOException JavaDoc {
163         response.sendError(HttpServletResponse.SC_FORBIDDEN);
164     }
165
166     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
167             throws ServletException JavaDoc, IOException JavaDoc {
168         response.sendError(HttpServletResponse.SC_FORBIDDEN);
169     }
170
171 }
Popular Tags