001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.scheduler;
034:
035: import org.quartz.JobDetail;
036: import org.quartz.Scheduler;
037:
038: import org.quartz.impl.StdSchedulerFactory;
039:
040: import java.io.InputStream;
041:
042: import java.sql.Connection;
043: import java.sql.ResultSet;
044:
045: import java.util.Properties;
046:
047: import javax.naming.InitialContext;
048:
049: import javax.sql.DataSource;
050:
051: public class LibresourceScheduler {
052: private static LibresourceScheduler instance;
053: private Scheduler scheduler;
054:
055: private LibresourceScheduler() throws Exception {
056: initdb();
057:
058: StdSchedulerFactory schedFact = new StdSchedulerFactory();
059: Properties properties = new Properties();
060: properties.put("org.quartz.scheduler.instanceName",
061: "Libresource Scheduler");
062: properties.put("org.quartz.scheduler.instanceId",
063: "LibresourceScheduler");
064: properties.put("org.quartz.threadPool.class",
065: "org.quartz.simpl.SimpleThreadPool");
066: properties.put("org.quartz.threadPool.threadCount", "10");
067: properties.put("org.quartz.threadPool.threadPriority", "5");
068: properties.put("org.quartz.jobStore.misfireThreshold", "60000");
069: properties.put("org.quartz.jobStore.class",
070: "org.quartz.impl.jdbcjobstore.JobStoreCMT");
071: properties.put("org.quartz.jobStore.driverDelegateClass",
072: "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate");
073: properties
074: .put("org.quartz.jobStore.dataSource", "@DATASOURCE@");
075: properties.put("org.quartz.jobStore.nonManagedTXDataSource",
076: "@DATASOURCE@");
077: properties.put("org.quartz.jobStore.tablePrefix", "qrtz_");
078: properties.put("org.quartz.jobStore.isClustered", "false");
079: properties.put("org.quartz.dataSource.libresourceDS.jndiURL",
080: "@DATASOURCE@");
081: properties.put(
082: "org.quartz.dataSource.libresourceDS.jndiAlwaysLookup",
083: "false");
084: schedFact.initialize(properties);
085: scheduler = schedFact.getScheduler();
086: scheduler.start();
087: ThrowMessageJob.class.getClass();
088: }
089:
090: public static LibresourceScheduler getInstance() throws Exception {
091: if (instance == null) {
092: instance = new LibresourceScheduler();
093: }
094:
095: return instance;
096: }
097:
098: public Scheduler getScheduler() {
099: return scheduler;
100: }
101:
102: private void initdb() throws Exception {
103: Connection connection = null;
104:
105: try {
106: DataSource dataSource = (DataSource) new InitialContext()
107: .lookup("@DATASOURCE@");
108: connection = dataSource.getConnection();
109:
110: ResultSet rs = connection.getMetaData().getTables(null,
111: null, "qrtz_job_details", null);
112: boolean bootstrap = false;
113:
114: if (!rs.next()) {
115: bootstrap = true;
116: }
117:
118: rs.close();
119:
120: if (bootstrap) {
121: StringBuffer SQL = new StringBuffer();
122: InputStream is = Thread
123: .currentThread()
124: .getContextClassLoader()
125: .getResourceAsStream(
126: "org/libresource/scheduler/bootstrap.sql");
127:
128: byte[] buffer = new byte[1024];
129: int read = -1;
130:
131: while ((read = is.read(buffer)) > 0) {
132: SQL.append(new String(buffer, 0, read));
133: }
134:
135: is.close();
136: connection.createStatement().executeUpdate(
137: SQL.toString());
138: }
139: } finally {
140: try {
141: connection.close();
142: } catch (Exception e) {
143: }
144: }
145: }
146:
147: public static JobDetail createJobDetail(LibresourceJob job,
148: String description) {
149: JobDetail jobDetail = new JobDetail(job.getName(),
150: Scheduler.DEFAULT_GROUP, ThrowMessageJob.class);
151: jobDetail.getJobDataMap().putAll(job.getMetaData());
152: jobDetail.setDescription(description);
153:
154: return jobDetail;
155: }
156: }
|