001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jul 22, 2005
014: * @author William Seyler
015: *
016: */
017: package org.pentaho.plugin.quartz;
018:
019: import java.io.BufferedInputStream;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.util.Properties;
025:
026: import org.pentaho.core.session.IPentahoSession;
027: import org.pentaho.core.system.IPentahoSystemListener;
028: import org.pentaho.core.system.PentahoSystem;
029: import org.pentaho.core.util.DatasourceHelper;
030: import org.pentaho.messages.Messages;
031: import org.pentaho.util.logging.Logger;
032: import org.quartz.Scheduler;
033: import org.quartz.SchedulerException;
034: import org.quartz.SchedulerFactory;
035:
036: public class QuartzSystemListener implements IPentahoSystemListener {
037:
038: protected static Scheduler schedulerInstance = null;
039:
040: private final static boolean debug = PentahoSystem.debug;
041:
042: /*
043: * (non-Javadoc)
044: *
045: * @see org.pentaho.core.system.IPentahoSystemListener#startup()
046: */
047: public boolean startup(IPentahoSession session) {
048: Properties quartzProps = PentahoSystem
049: .getSystemSettings()
050: .getSystemSettingsProperties("quartz/quartz.properties"); //$NON-NLS-1$
051: if (quartzProps == null) {
052: try {
053: quartzProps = findPropertiesInClasspath();
054: } catch (IOException ex) {
055: Logger
056: .error(
057: QuartzSystemListener.class.getName(),
058: Messages
059: .getErrorString("QuartzSystemListener.ERROR_0004_LOAD_PROPERTIES_FROM_CLASSPATH"), ex); //$NON-NLS-1$
060: }
061: }
062: if (quartzProps == null) {
063: return false;
064: }
065: String dsName = quartzProps
066: .getProperty("org.quartz.dataSource.myDS.jndiURL"); //$NON-NLS-1$
067: if (dsName != null) {
068: try {
069: String boundDsName = DatasourceHelper
070: .getDSBoundName(dsName);
071: if (boundDsName != null) {
072: quartzProps
073: .setProperty(
074: "org.quartz.dataSource.myDS.jndiURL", boundDsName); //$NON-NLS-1$
075: }
076: } catch (javax.naming.NamingException ignored) {
077: }
078: }
079:
080: try {
081: SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(
082: quartzProps);
083: schedulerInstance = schedFact.getScheduler();
084: if (debug)
085: Logger.debug(QuartzSystemListener.class.getName(),
086: schedulerInstance.getSchedulerName());
087: schedulerInstance.start();
088: } catch (SchedulerException e) {
089: // TODO Auto-generated catch block
090: e.printStackTrace();
091: // return true because this shouldn't always stop the entire server
092: // from running
093: return true;
094: }
095: return true;
096: }
097:
098: private Properties findPropertiesInClasspath() throws IOException {
099: // Do my best to find the properties file...
100: File propFile = new File("quartz.properties"); //$NON-NLS-1$
101: if (!propFile.exists()) {
102: InputStream in = Thread.currentThread()
103: .getContextClassLoader().getResourceAsStream(
104: "quartz.properties"); //$NON-NLS-1$
105: if (in != null) {
106: Properties props = new Properties();
107: props.load(in);
108: return props;
109: }
110: return null; // Couldn't find properties file.
111: } else {
112: InputStream iStream = new BufferedInputStream(
113: new FileInputStream(propFile));
114: Properties props = new Properties();
115: props.load(iStream);
116: return props;
117: }
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.pentaho.core.system.IPentahoSystemListener#shutdown()
124: */
125: public void shutdown() {
126: try {
127: schedulerInstance.shutdown(true);
128: schedulerInstance = null;
129: } catch (SchedulerException e) {
130: // TODO Auto-generated catch block
131: e.printStackTrace();
132: }
133: }
134:
135: /**
136: * @return Returns the schedulerInstance.
137: * @throws Exception
138: */
139: public static Scheduler getSchedulerInstance() throws Exception {
140: if (schedulerInstance == null) {
141: throw new Exception(
142: Messages
143: .getErrorString("QuartzSystemListener.ERROR_0001_Scheduler_Not_Initialized")); //$NON-NLS-1$
144: }
145: return schedulerInstance;
146: }
147: }
|