KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > impl > jdbcjobstore > PointbaseDelegate


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.impl.jdbcjobstore;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.math.BigDecimal JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.quartz.Calendar;
36 import org.quartz.CronTrigger;
37 import org.quartz.JobDetail;
38 import org.quartz.SimpleTrigger;
39 import org.quartz.Trigger;
40
41 /**
42  * <p>
43  * This is a driver delegate for the Pointbase JDBC driver.
44  * </p>
45  *
46  * @author Gregg Freeman
47  */

48 public class PointbaseDelegate extends StdJDBCDelegate {
49
50     //private static Category log =
51
// Category.getInstance(PointbaseJDBCDelegate.class);
52
/**
53      * <p>
54      * Create new PointbaseJDBCDelegate instance.
55      * </p>
56      *
57      * @param logger
58      * the logger to use during execution
59      * @param tablePrefix
60      * the prefix of all table names
61      */

62     public PointbaseDelegate(Log logger, String JavaDoc tablePrefix, String JavaDoc instanceId) {
63         super(logger, tablePrefix, instanceId);
64     }
65
66     /**
67      * <p>
68      * Create new PointbaseJDBCDelegate instance.
69      * </p>
70      *
71      * @param logger
72      * the logger to use during execution
73      * @param tablePrefix
74      * the prefix of all table names
75      */

76     public PointbaseDelegate(Log logger, String JavaDoc tablePrefix, String JavaDoc instanceId,
77             Boolean JavaDoc useProperties) {
78         super(logger, tablePrefix, instanceId, useProperties);
79     }
80
81     //---------------------------------------------------------------------------
82
// jobs
83
//---------------------------------------------------------------------------
84

85     /**
86      * <p>
87      * Insert the job detail record.
88      * </p>
89      *
90      * @param conn
91      * the DB Connection
92      * @param job
93      * the job to insert
94      * @return number of rows inserted
95      * @throws IOException
96      * if there were problems serializing the JobDataMap
97      */

98     public int insertJobDetail(Connection JavaDoc conn, JobDetail job)
99         throws IOException JavaDoc, SQLException JavaDoc {
100         //log.debug( "Inserting JobDetail " + job );
101
ByteArrayOutputStream JavaDoc baos = serializeJobData(job.getJobDataMap());
102         int len = baos.toByteArray().length;
103         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
104
105         PreparedStatement JavaDoc ps = null;
106
107         int insertResult = 0;
108
109         try {
110             ps = conn.prepareStatement(rtp(INSERT_JOB_DETAIL));
111             ps.setString(1, job.getName());
112             ps.setString(2, job.getGroup());
113             ps.setString(3, job.getDescription());
114             ps.setString(4, job.getJobClass().getName());
115             setBoolean(ps, 5, job.isDurable());
116             setBoolean(ps, 6, job.isVolatile());
117             setBoolean(ps, 7, job.isStateful());
118             setBoolean(ps, 8, job.requestsRecovery());
119             ps.setBinaryStream(9, bais, len);
120
121             insertResult = ps.executeUpdate();
122         } finally {
123             closeStatement(ps);
124         }
125
126         if (insertResult > 0) {
127             String JavaDoc[] jobListeners = job.getJobListenerNames();
128             for (int i = 0; jobListeners != null && i < jobListeners.length; i++) {
129                 insertJobListener(conn, job, jobListeners[i]);
130             }
131         }
132
133         return insertResult;
134     }
135
136     /**
137      * <p>
138      * Update the job detail record.
139      * </p>
140      *
141      * @param conn
142      * the DB Connection
143      * @param job
144      * the job to update
145      * @return number of rows updated
146      * @throws IOException
147      * if there were problems serializing the JobDataMap
148      */

149     public int updateJobDetail(Connection JavaDoc conn, JobDetail job)
150         throws IOException JavaDoc, SQLException JavaDoc {
151         //log.debug( "Updating job detail " + job );
152
ByteArrayOutputStream JavaDoc baos = serializeJobData(job.getJobDataMap());
153         int len = baos.toByteArray().length;
154         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
155
156         PreparedStatement JavaDoc ps = null;
157
158         int insertResult = 0;
159
160         try {
161             ps = conn.prepareStatement(rtp(UPDATE_JOB_DETAIL));
162             ps.setString(1, job.getDescription());
163             ps.setString(2, job.getJobClass().getName());
164             setBoolean(ps, 3, job.isDurable());
165             setBoolean(ps, 4, job.isVolatile());
166             setBoolean(ps, 5, job.isStateful());
167             setBoolean(ps, 6, job.requestsRecovery());
168             ps.setBinaryStream(7, bais, len);
169             ps.setString(8, job.getName());
170             ps.setString(9, job.getGroup());
171
172             insertResult = ps.executeUpdate();
173         } finally {
174             closeStatement(ps);
175         }
176
177         if (insertResult > 0) {
178             deleteJobListeners(conn, job.getName(), job.getGroup());
179
180             String JavaDoc[] jobListeners = job.getJobListenerNames();
181             for (int i = 0; jobListeners != null && i < jobListeners.length; i++) {
182                 insertJobListener(conn, job, jobListeners[i]);
183             }
184         }
185
186         return insertResult;
187     }
188
189     public int insertTrigger(Connection JavaDoc conn, Trigger trigger, String JavaDoc state,
190             JobDetail jobDetail) throws SQLException JavaDoc, IOException JavaDoc {
191
192         ByteArrayOutputStream JavaDoc baos = serializeJobData(trigger.getJobDataMap());
193         int len = baos.toByteArray().length;
194         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
195         
196         PreparedStatement JavaDoc ps = null;
197
198         int insertResult = 0;
199
200         try {
201             ps = conn.prepareStatement(rtp(INSERT_TRIGGER));
202             ps.setString(1, trigger.getName());
203             ps.setString(2, trigger.getGroup());
204             ps.setString(3, trigger.getJobName());
205             ps.setString(4, trigger.getJobGroup());
206             setBoolean(ps, 5, trigger.isVolatile());
207             ps.setString(6, trigger.getDescription());
208             ps.setBigDecimal(7, new BigDecimal JavaDoc(String.valueOf(trigger
209                     .getNextFireTime().getTime())));
210             long prevFireTime = -1;
211             if (trigger.getPreviousFireTime() != null) {
212                 prevFireTime = trigger.getPreviousFireTime().getTime();
213             }
214             ps.setBigDecimal(8, new BigDecimal JavaDoc(String.valueOf(prevFireTime)));
215             ps.setString(9, state);
216             if (trigger.getClass() == SimpleTrigger.class) {
217                 ps.setString(10, TTYPE_SIMPLE);
218             } else if (trigger.getClass() == CronTrigger.class) {
219                 ps.setString(10, TTYPE_CRON);
220             } else {
221                 ps.setString(10, TTYPE_BLOB);
222             }
223             ps.setBigDecimal(11, new BigDecimal JavaDoc(String.valueOf(trigger
224                     .getStartTime().getTime())));
225             long endTime = 0;
226             if (trigger.getEndTime() != null) {
227                 endTime = trigger.getEndTime().getTime();
228             }
229             ps.setBigDecimal(12, new BigDecimal JavaDoc(String.valueOf(endTime)));
230             ps.setString(13, trigger.getCalendarName());
231             ps.setInt(14, trigger.getMisfireInstruction());
232             ps.setBinaryStream(15, bais, len);
233             ps.setInt(16, trigger.getPriority());
234             
235             insertResult = ps.executeUpdate();
236         } finally {
237             closeStatement(ps);
238         }
239
240         if (insertResult > 0) {
241             String JavaDoc[] trigListeners = trigger.getTriggerListenerNames();
242             for (int i = 0; trigListeners != null && i < trigListeners.length; i++) {
243                 insertTriggerListener(conn, trigger, trigListeners[i]);
244             }
245         }
246
247         return insertResult;
248     }
249     
250     public int updateTrigger(Connection JavaDoc conn, Trigger trigger, String JavaDoc state,
251             JobDetail jobDetail) throws SQLException JavaDoc, IOException JavaDoc {
252
253         ByteArrayOutputStream JavaDoc baos = serializeJobData(trigger.getJobDataMap());
254         int len = baos.toByteArray().length;
255         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
256                 
257         PreparedStatement JavaDoc ps = null;
258
259         int insertResult = 0;
260
261
262         try {
263             ps = conn.prepareStatement(rtp(UPDATE_TRIGGER));
264                 
265             ps.setString(1, trigger.getJobName());
266             ps.setString(2, trigger.getJobGroup());
267             setBoolean(ps, 3, trigger.isVolatile());
268             ps.setString(4, trigger.getDescription());
269             long nextFireTime = -1;
270             if (trigger.getNextFireTime() != null) {
271                 nextFireTime = trigger.getNextFireTime().getTime();
272             }
273             ps.setBigDecimal(5, new BigDecimal JavaDoc(String.valueOf(nextFireTime)));
274             long prevFireTime = -1;
275             if (trigger.getPreviousFireTime() != null) {
276                 prevFireTime = trigger.getPreviousFireTime().getTime();
277             }
278             ps.setBigDecimal(6, new BigDecimal JavaDoc(String.valueOf(prevFireTime)));
279             ps.setString(7, state);
280             if (trigger.getClass() == SimpleTrigger.class) {
281                 // updateSimpleTrigger(conn, (SimpleTrigger)trigger);
282
ps.setString(8, TTYPE_SIMPLE);
283             } else if (trigger.getClass() == CronTrigger.class) {
284                 // updateCronTrigger(conn, (CronTrigger)trigger);
285
ps.setString(8, TTYPE_CRON);
286             } else {
287                 // updateBlobTrigger(conn, trigger);
288
ps.setString(8, TTYPE_BLOB);
289             }
290             ps.setBigDecimal(9, new BigDecimal JavaDoc(String.valueOf(trigger
291                     .getStartTime().getTime())));
292             long endTime = 0;
293             if (trigger.getEndTime() != null) {
294                 endTime = trigger.getEndTime().getTime();
295             }
296             ps.setBigDecimal(10, new BigDecimal JavaDoc(String.valueOf(endTime)));
297             ps.setString(11, trigger.getCalendarName());
298             ps.setInt(12, trigger.getMisfireInstruction());
299             
300             ps.setInt(13, trigger.getPriority());
301             ps.setBinaryStream(14, bais, len);
302             ps.setString(15, trigger.getName());
303             ps.setString(16, trigger.getGroup());
304
305             insertResult = ps.executeUpdate();
306         } finally {
307             closeStatement(ps);
308         }
309
310         if (insertResult > 0) {
311             deleteTriggerListeners(conn, trigger.getName(), trigger.getGroup());
312
313             String JavaDoc[] trigListeners = trigger.getTriggerListenerNames();
314             for (int i = 0; trigListeners != null && i < trigListeners.length; i++) {
315                 insertTriggerListener(conn, trigger, trigListeners[i]);
316             }
317         }
318
319         return insertResult;
320     }
321
322     /**
323      * <p>
324      * Update the job data map for the given job.
325      * </p>
326      *
327      * @param conn
328      * the DB Connection
329      * @param job
330      * the job to update
331      * @return the number of rows updated
332      */

333     public int updateJobData(Connection JavaDoc conn, JobDetail job)
334         throws IOException JavaDoc, SQLException JavaDoc {
335         //log.debug( "Updating Job Data for Job " + job );
336
ByteArrayOutputStream JavaDoc baos = serializeJobData(job.getJobDataMap());
337         int len = baos.toByteArray().length;
338         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
339         PreparedStatement JavaDoc ps = null;
340
341         try {
342             ps = conn.prepareStatement(rtp(UPDATE_JOB_DATA));
343             ps.setBinaryStream(1, bais, len);
344             ps.setString(2, job.getName());
345             ps.setString(3, job.getGroup());
346
347             return ps.executeUpdate();
348         } finally {
349             closeStatement(ps);
350         }
351     }
352
353     //---------------------------------------------------------------------------
354
// triggers
355
//---------------------------------------------------------------------------
356

357     //---------------------------------------------------------------------------
358
// calendars
359
//---------------------------------------------------------------------------
360

361     /**
362      * <p>
363      * Insert a new calendar.
364      * </p>
365      *
366      * @param conn
367      * the DB Connection
368      * @param calendarName
369      * the name for the new calendar
370      * @param calendar
371      * the calendar
372      * @return the number of rows inserted
373      * @throws IOException
374      * if there were problems serializing the calendar
375      */

376     public int insertCalendar(Connection JavaDoc conn, String JavaDoc calendarName,
377             Calendar calendar) throws IOException JavaDoc, SQLException JavaDoc {
378         //log.debug( "Inserting Calendar " + calendarName + " : " + calendar
379
// );
380
ByteArrayOutputStream JavaDoc baos = serializeObject(calendar);
381         byte buf[] = baos.toByteArray();
382         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(buf);
383
384         PreparedStatement JavaDoc ps = null;
385
386         try {
387             ps = conn.prepareStatement(rtp(INSERT_CALENDAR));
388             ps.setString(1, calendarName);
389             ps.setBinaryStream(2, bais, buf.length);
390
391             return ps.executeUpdate();
392         } finally {
393             closeStatement(ps);
394         }
395     }
396
397     /**
398      * <p>
399      * Update a calendar.
400      * </p>
401      *
402      * @param conn
403      * the DB Connection
404      * @param calendarName
405      * the name for the new calendar
406      * @param calendar
407      * the calendar
408      * @return the number of rows updated
409      * @throws IOException
410      * if there were problems serializing the calendar
411      */

412     public int updateCalendar(Connection JavaDoc conn, String JavaDoc calendarName,
413             Calendar calendar) throws IOException JavaDoc, SQLException JavaDoc {
414         //log.debug( "Updating calendar " + calendarName + " : " + calendar );
415
ByteArrayOutputStream JavaDoc baos = serializeObject(calendar);
416         byte buf[] = baos.toByteArray();
417         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(buf);
418
419         PreparedStatement JavaDoc ps = null;
420
421         try {
422             ps = conn.prepareStatement(rtp(UPDATE_CALENDAR));
423             ps.setBinaryStream(1, bais, buf.length);
424             ps.setString(2, calendarName);
425
426             return ps.executeUpdate();
427         } finally {
428             closeStatement(ps);
429         }
430     }
431
432     //---------------------------------------------------------------------------
433
// protected methods that can be overridden by subclasses
434
//---------------------------------------------------------------------------
435

436     /**
437      * <p>
438      * This method should be overridden by any delegate subclasses that need
439      * special handling for BLOBs. The default implementation uses standard
440      * JDBC <code>java.sql.Blob</code> operations.
441      * </p>
442      *
443      * @param rs
444      * the result set, already queued to the correct row
445      * @param colName
446      * the column name for the BLOB
447      * @return the deserialized Object from the ResultSet BLOB
448      * @throws ClassNotFoundException
449      * if a class found during deserialization cannot be found
450      * @throws IOException
451      * if deserialization causes an error
452      */

453     protected Object JavaDoc getObjectFromBlob(ResultSet JavaDoc rs, String JavaDoc colName)
454         throws ClassNotFoundException JavaDoc, IOException JavaDoc, SQLException JavaDoc {
455         //log.debug( "Getting blob from column: " + colName );
456
Object JavaDoc obj = null;
457
458         byte binaryData[] = rs.getBytes(colName);
459
460         InputStream JavaDoc binaryInput = new ByteArrayInputStream JavaDoc(binaryData);
461
462         if (null != binaryInput) {
463             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(binaryInput);
464             try {
465                 obj = in.readObject();
466             } finally {
467                 in.close();
468             }
469         }
470
471         return obj;
472     }
473
474     /**
475      * <p>
476      * This method should be overridden by any delegate subclasses that need
477      * special handling for BLOBs for job details. The default implementation
478      * uses standard JDBC <code>java.sql.Blob</code> operations.
479      * </p>
480      *
481      * @param rs
482      * the result set, already queued to the correct row
483      * @param colName
484      * the column name for the BLOB
485      * @return the deserialized Object from the ResultSet BLOB
486      * @throws ClassNotFoundException
487      * if a class found during deserialization cannot be found
488      * @throws IOException
489      * if deserialization causes an error
490      */

491     protected Object JavaDoc getJobDetailFromBlob(ResultSet JavaDoc rs, String JavaDoc colName)
492         throws ClassNotFoundException JavaDoc, IOException JavaDoc, SQLException JavaDoc {
493         //log.debug( "Getting Job details from blob in col " + colName );
494
if (canUseProperties()) {
495             byte data[] = rs.getBytes(colName);
496             if(data == null) {
497                 return null;
498             }
499             InputStream JavaDoc binaryInput = new ByteArrayInputStream JavaDoc(data);
500             return binaryInput;
501         }
502
503         return getObjectFromBlob(rs, colName);
504     }
505 }
506
507 // EOF
508
Popular Tags