1 17 18 21 package org.quartz.impl.jdbcjobstore; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.ObjectInputStream ; 28 import java.math.BigDecimal ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 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 48 public class PointbaseDelegate extends StdJDBCDelegate { 49 50 62 public PointbaseDelegate(Log logger, String tablePrefix, String instanceId) { 63 super(logger, tablePrefix, instanceId); 64 } 65 66 76 public PointbaseDelegate(Log logger, String tablePrefix, String instanceId, 77 Boolean useProperties) { 78 super(logger, tablePrefix, instanceId, useProperties); 79 } 80 81 85 98 public int insertJobDetail(Connection conn, JobDetail job) 99 throws IOException , SQLException { 100 ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap()); 102 int len = baos.toByteArray().length; 103 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 104 105 PreparedStatement 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 [] 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 149 public int updateJobDetail(Connection conn, JobDetail job) 150 throws IOException , SQLException { 151 ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap()); 153 int len = baos.toByteArray().length; 154 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 155 156 PreparedStatement 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 [] 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 conn, Trigger trigger, String state, 190 JobDetail jobDetail) throws SQLException , IOException { 191 192 ByteArrayOutputStream baos = serializeJobData(trigger.getJobDataMap()); 193 int len = baos.toByteArray().length; 194 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 195 196 PreparedStatement 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 (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 (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 (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 (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 [] 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 conn, Trigger trigger, String state, 251 JobDetail jobDetail) throws SQLException , IOException { 252 253 ByteArrayOutputStream baos = serializeJobData(trigger.getJobDataMap()); 254 int len = baos.toByteArray().length; 255 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 256 257 PreparedStatement 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 (String.valueOf(nextFireTime))); 274 long prevFireTime = -1; 275 if (trigger.getPreviousFireTime() != null) { 276 prevFireTime = trigger.getPreviousFireTime().getTime(); 277 } 278 ps.setBigDecimal(6, new BigDecimal (String.valueOf(prevFireTime))); 279 ps.setString(7, state); 280 if (trigger.getClass() == SimpleTrigger.class) { 281 ps.setString(8, TTYPE_SIMPLE); 283 } else if (trigger.getClass() == CronTrigger.class) { 284 ps.setString(8, TTYPE_CRON); 286 } else { 287 ps.setString(8, TTYPE_BLOB); 289 } 290 ps.setBigDecimal(9, new BigDecimal (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 (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 [] 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 333 public int updateJobData(Connection conn, JobDetail job) 334 throws IOException , SQLException { 335 ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap()); 337 int len = baos.toByteArray().length; 338 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 339 PreparedStatement 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 357 361 376 public int insertCalendar(Connection conn, String calendarName, 377 Calendar calendar) throws IOException , SQLException { 378 ByteArrayOutputStream baos = serializeObject(calendar); 381 byte buf[] = baos.toByteArray(); 382 ByteArrayInputStream bais = new ByteArrayInputStream (buf); 383 384 PreparedStatement 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 412 public int updateCalendar(Connection conn, String calendarName, 413 Calendar calendar) throws IOException , SQLException { 414 ByteArrayOutputStream baos = serializeObject(calendar); 416 byte buf[] = baos.toByteArray(); 417 ByteArrayInputStream bais = new ByteArrayInputStream (buf); 418 419 PreparedStatement 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 436 453 protected Object getObjectFromBlob(ResultSet rs, String colName) 454 throws ClassNotFoundException , IOException , SQLException { 455 Object obj = null; 457 458 byte binaryData[] = rs.getBytes(colName); 459 460 InputStream binaryInput = new ByteArrayInputStream (binaryData); 461 462 if (null != binaryInput) { 463 ObjectInputStream in = new ObjectInputStream (binaryInput); 464 try { 465 obj = in.readObject(); 466 } finally { 467 in.close(); 468 } 469 } 470 471 return obj; 472 } 473 474 491 protected Object getJobDetailFromBlob(ResultSet rs, String colName) 492 throws ClassNotFoundException , IOException , SQLException { 493 if (canUseProperties()) { 495 byte data[] = rs.getBytes(colName); 496 if(data == null) { 497 return null; 498 } 499 InputStream binaryInput = new ByteArrayInputStream (data); 500 return binaryInput; 501 } 502 503 return getObjectFromBlob(rs, colName); 504 } 505 } 506 507 | Popular Tags |