KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > scheduler > SchedulerServiceImpl


1 package org.jahia.services.scheduler;
2
3 import org.jahia.settings.SettingsBean;
4 import org.jahia.exceptions.JahiaInitializationException;
5 import org.quartz.Scheduler;
6 import org.quartz.SchedulerException;
7 import org.jahia.exceptions.JahiaException;
8 import org.quartz.JobDetail;
9 import org.quartz.Trigger;
10 import java.io.File JavaDoc;
11
12 public class SchedulerServiceImpl extends SchedulerService {
13
14     private static org.apache.log4j.Logger logger =
15             org.apache.log4j.Logger.getLogger(SchedulerServiceImpl.class);
16
17     private static SchedulerServiceImpl singletonInstance;
18
19     private Scheduler scheduler = null;
20     private boolean schedulerRunning = false;
21
22     private SchedulerServiceImpl () {
23     }
24
25     /**
26      * Return the unique service instance. If the instance does not exist,
27      * a new instance is created.
28      *
29      * @return The unique service instance.
30      */

31     public synchronized static SchedulerServiceImpl getInstance () {
32         if (singletonInstance == null) {
33             singletonInstance = new SchedulerServiceImpl ();
34         }
35         return singletonInstance;
36     }
37
38     /**
39      * Initializes the servlet dispatching service with parameters loaded
40      * from the Jahia configuration file.
41      * @param jSettings private settings object that contains Jahia
42      * configuration parameters
43      * @throws JahiaInitializationException thrown in the case of an error
44      * during this initialization, that will be treated as a critical error
45      * in Jahia and probably stop execution of Jahia once and for all.
46      */

47     public void init( SettingsBean jSettings )
48         throws JahiaInitializationException {
49
50         try {
51             org.quartz.impl.StdSchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
52             File JavaDoc schedulerConfigFile = new File JavaDoc(jSettings.getSchedulerConfigFile());
53             if (schedulerConfigFile.exists()) {
54                 schedFact.initialize(schedulerConfigFile.getAbsolutePath());
55             }
56             scheduler = schedFact.getScheduler();
57             logger.debug("Starting scheduler...");
58             scheduler.start();
59             schedulerRunning = true;
60         } catch (SchedulerException se) {
61             if (se.getUnderlyingException() != null) {
62                 throw new JahiaInitializationException(
63                     "Error while initializing scheduler service",
64                     se.getUnderlyingException());
65             } else {
66                 throw new JahiaInitializationException(
67                     "Error while initializing scheduler service",
68                     se);
69             }
70         }
71
72     }
73
74     public synchronized void shutdown ()
75         throws JahiaException {
76         try {
77             logger.debug("Shutting down scheduler...");
78             scheduler.shutdown();
79             scheduler = null;
80             schedulerRunning = false;
81         } catch (SchedulerException se) {
82             throw getJahiaException(se);
83         }
84     }
85
86     public void scheduleJob(JobDetail jobDetail, Trigger trigger)
87         throws JahiaException {
88         if (!schedulerRunning) {
89             return;
90         }
91         try {
92             scheduler.scheduleJob(jobDetail, trigger);
93         } catch (SchedulerException se) {
94             throw getJahiaException(se);
95         }
96     }
97
98     public void unscheduleJob(String JavaDoc triggerName, String JavaDoc groupName)
99         throws JahiaException {
100         if (!schedulerRunning) {
101             return;
102         }
103         try {
104             scheduler.unscheduleJob(triggerName, groupName);
105         } catch (SchedulerException se) {
106             throw getJahiaException(se);
107         }
108     }
109
110     private JahiaException getJahiaException(SchedulerException se) {
111         if (se.getUnderlyingException() != null) {
112             return new JahiaException("Error while shutting down scheduler service",
113                                       "Error while shutting down scheduler service",
114                                       JahiaException.SERVICE_ERROR,
115                                       JahiaException.ERROR_SEVERITY,
116                                       se.getUnderlyingException());
117         } else {
118             return new JahiaException("Error while shutting down scheduler service",
119                                       "Error while shutting down scheduler service",
120                                       JahiaException.SERVICE_ERROR,
121                                       JahiaException.ERROR_SEVERITY,
122                                       se);
123         }
124     }
125
126 }
127
Popular Tags