KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > quartz > QuartzSystemListener


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 22, 2005
14  * @author William Seyler
15  *
16  */

17 package org.pentaho.plugin.quartz;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.pentaho.core.session.IPentahoSession;
27 import org.pentaho.core.system.IPentahoSystemListener;
28 import org.pentaho.core.system.PentahoSystem;
29 import org.pentaho.core.util.DatasourceHelper;
30 import org.pentaho.messages.Messages;
31 import org.pentaho.util.logging.Logger;
32 import org.quartz.Scheduler;
33 import org.quartz.SchedulerException;
34 import org.quartz.SchedulerFactory;
35
36 public class QuartzSystemListener implements IPentahoSystemListener {
37
38     protected static Scheduler schedulerInstance = null;
39
40     private final static boolean debug = PentahoSystem.debug;
41
42     /*
43      * (non-Javadoc)
44      *
45      * @see org.pentaho.core.system.IPentahoSystemListener#startup()
46      */

47     public boolean startup(IPentahoSession session) {
48         Properties JavaDoc quartzProps = PentahoSystem.getSystemSettings().getSystemSettingsProperties("quartz/quartz.properties"); //$NON-NLS-1$
49
if (quartzProps == null) {
50             try {
51                 quartzProps = findPropertiesInClasspath();
52             } catch (IOException JavaDoc ex) {
53                 Logger.error(QuartzSystemListener.class.getName(), Messages.getErrorString("QuartzSystemListener.ERROR_0004_LOAD_PROPERTIES_FROM_CLASSPATH"), ex); //$NON-NLS-1$
54
}
55         }
56         if (quartzProps == null) {
57             return false;
58         }
59         String JavaDoc dsName = quartzProps.getProperty("org.quartz.dataSource.myDS.jndiURL"); //$NON-NLS-1$
60
if (dsName != null) {
61             try {
62                 String JavaDoc boundDsName = DatasourceHelper.getDSBoundName(dsName);
63                 if (boundDsName != null) {
64                     quartzProps.setProperty("org.quartz.dataSource.myDS.jndiURL", boundDsName); //$NON-NLS-1$
65
}
66             } catch (javax.naming.NamingException JavaDoc ignored) {
67             }
68         }
69
70         try {
71             SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(quartzProps);
72             schedulerInstance = schedFact.getScheduler();
73             if (debug)
74                 Logger.debug(QuartzSystemListener.class.getName(), schedulerInstance.getSchedulerName());
75             schedulerInstance.start();
76         } catch (SchedulerException e) {
77             // TODO Auto-generated catch block
78
e.printStackTrace();
79             // return true because this shouldn't always stop the entire server
80
// from running
81
return true;
82         }
83         return true;
84     }
85
86     private Properties JavaDoc findPropertiesInClasspath() throws IOException JavaDoc {
87         // Do my best to find the properties file...
88
File JavaDoc propFile = new File JavaDoc("quartz.properties"); //$NON-NLS-1$
89
if (!propFile.exists()) {
90             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResourceAsStream("quartz.properties"); //$NON-NLS-1$
91
if (in != null) {
92                 Properties JavaDoc props = new Properties JavaDoc();
93                 props.load(in);
94                 return props;
95             }
96             return null; // Couldn't find properties file.
97
} else {
98             InputStream JavaDoc iStream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(propFile));
99             Properties JavaDoc props = new Properties JavaDoc();
100             props.load(iStream);
101             return props;
102         }
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.pentaho.core.system.IPentahoSystemListener#shutdown()
109      */

110     public void shutdown() {
111         try {
112             schedulerInstance.shutdown(true);
113             schedulerInstance = null;
114         } catch (SchedulerException e) {
115             // TODO Auto-generated catch block
116
e.printStackTrace();
117         }
118     }
119
120     /**
121      * @return Returns the schedulerInstance.
122      * @throws Exception
123      */

124     public static Scheduler getSchedulerInstance() throws Exception JavaDoc {
125         if (schedulerInstance == null) {
126             throw new Exception JavaDoc(Messages.getErrorString("QuartzSystemListener.ERROR_0001_Scheduler_Not_Initialized")); //$NON-NLS-1$
127
}
128         return schedulerInstance;
129     }
130 }
131
Popular Tags