1 17 package org.pentaho.plugin.quartz; 18 19 import java.io.BufferedInputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Properties ; 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 47 public boolean startup(IPentahoSession session) { 48 Properties quartzProps = PentahoSystem.getSystemSettings().getSystemSettingsProperties("quartz/quartz.properties"); if (quartzProps == null) { 50 try { 51 quartzProps = findPropertiesInClasspath(); 52 } catch (IOException ex) { 53 Logger.error(QuartzSystemListener.class.getName(), Messages.getErrorString("QuartzSystemListener.ERROR_0004_LOAD_PROPERTIES_FROM_CLASSPATH"), ex); } 55 } 56 if (quartzProps == null) { 57 return false; 58 } 59 String dsName = quartzProps.getProperty("org.quartz.dataSource.myDS.jndiURL"); if (dsName != null) { 61 try { 62 String boundDsName = DatasourceHelper.getDSBoundName(dsName); 63 if (boundDsName != null) { 64 quartzProps.setProperty("org.quartz.dataSource.myDS.jndiURL", boundDsName); } 66 } catch (javax.naming.NamingException 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 e.printStackTrace(); 79 return true; 82 } 83 return true; 84 } 85 86 private Properties findPropertiesInClasspath() throws IOException { 87 File propFile = new File ("quartz.properties"); if (!propFile.exists()) { 90 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("quartz.properties"); if (in != null) { 92 Properties props = new Properties (); 93 props.load(in); 94 return props; 95 } 96 return null; } else { 98 InputStream iStream = new BufferedInputStream (new FileInputStream (propFile)); 99 Properties props = new Properties (); 100 props.load(iStream); 101 return props; 102 } 103 } 104 105 110 public void shutdown() { 111 try { 112 schedulerInstance.shutdown(true); 113 schedulerInstance = null; 114 } catch (SchedulerException e) { 115 e.printStackTrace(); 117 } 118 } 119 120 124 public static Scheduler getSchedulerInstance() throws Exception { 125 if (schedulerInstance == null) { 126 throw new Exception (Messages.getErrorString("QuartzSystemListener.ERROR_0001_Scheduler_Not_Initialized")); } 128 return schedulerInstance; 129 } 130 } 131 | Popular Tags |