1 package org.springframework.samples.imagedb.scheduling; 2 3 import java.util.List ; 4 import java.util.TimerTask ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 9 import org.springframework.mail.MailSender; 10 import org.springframework.mail.SimpleMailMessage; 11 import org.springframework.samples.imagedb.ImageDatabase; 12 import org.springframework.samples.imagedb.ImageDescriptor; 13 import org.springframework.util.StringUtils; 14 15 32 public class ListImagesTimerTask extends TimerTask { 33 34 private final Log logger = LogFactory.getLog(getClass()); 35 36 private ImageDatabase imageDatabase; 37 38 private MailSender mailSender; 39 40 private String mailFrom; 41 42 private String mailTo; 43 44 public void setImageDatabase(ImageDatabase imageDatabase) { 45 this.imageDatabase = imageDatabase; 46 } 47 48 public void setMailSender(MailSender mailSender) { 49 this.mailSender = mailSender; 50 } 51 52 public void setMailFrom(String mailFrom) { 53 this.mailFrom = mailFrom; 54 } 55 56 public void setMailTo(String mailTo) { 57 this.mailTo = mailTo; 58 } 59 60 public void run() { 61 logger.info("Listing images in image database, scheduled by Timer"); 62 List images = this.imageDatabase.getImages(); 63 String [] imageNames = new String [images.size()]; 64 for (int i = 0; i < images.size(); i++) { 65 ImageDescriptor image = (ImageDescriptor) images.get(i); 66 imageNames[i] = image.getName(); 67 } 68 69 String text = "Images in image database: " + StringUtils.arrayToDelimitedString(imageNames, ", "); 70 logger.info(text); 71 72 if (!"".equals(this.mailTo)) { 73 logger.info("Sending image list mail to: " + this.mailTo); 74 SimpleMailMessage message = new SimpleMailMessage(); 75 message.setFrom(this.mailFrom); 76 message.setTo(this.mailTo); 77 message.setSubject("Image list"); 78 message.setText(text); 79 this.mailSender.send(message); 80 } 81 else { 82 logger.info("Not sending image list mail - specify mail settings in 'WEB-INF/mail.properties'"); 83 } 84 85 } 86 87 } 88 | Popular Tags |