1 package com.Yasna.forum.Tasks; 2 3 import com.Yasna.forum.*; 4 import com.Yasna.forum.database.DbConnectionManager; 5 import com.Yasna.util.MailSender; 6 7 import java.util.LinkedList ; 8 import java.sql.Connection ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 13 65 66 71 public class EmailWatchManager { 72 private LinkedList newMessages; 73 private Thread worker; 75 private ForumFactory factory; 76 public EmailWatchManager(ForumFactory factory){ 77 this.factory = factory; 79 newMessages = new LinkedList (); 80 worker = new Thread (new EmailWatchWorker(this,factory)); 82 worker.setDaemon(true); 83 worker.start(); 84 } 85 public synchronized void addMessage(ForumMessage message){ 86 newMessages.addLast(message); 87 notify(); 89 } 90 public synchronized ForumMessage getNextMessage(){ 91 if(newMessages.isEmpty()){ 92 try{ 93 System.out.println("waiting for a new message"); 94 wait(); 95 }catch (InterruptedException ie) {} 97 } 98 return (ForumMessage)newMessages.removeFirst(); 99 } 100 101 public class EmailWatchWorker implements Runnable { 102 private static final String GET_USERLIST= 103 "select distinct userID from yazdUserProp where (name=? or name=?) and propValue=?"; 104 105 private EmailWatchManager manager; 106 private ForumFactory factory; 107 public EmailWatchWorker(EmailWatchManager m,ForumFactory factory){ 108 this.manager=m; 109 this.factory = factory; 110 } 111 public void run(){ 112 while(true){ 113 sendEmailsForMessage(manager.getNextMessage()); 114 } 115 } 116 private void sendEmailsForMessage(ForumMessage message){ 117 if(PropertyManager.getProperty("yazdMailSMTPServer")==null || "".equals(PropertyManager.getProperty("yazdMailSMTPServer"))) { 119 return; 120 } 122 int ForumID = message.getForumThread().getForum().getID(); 123 int ThreadID = message.getForumThread().getID(); 124 int OriginalUserID = message.getUser().getID(); 125 Connection con = null; 126 PreparedStatement pstmt = null; 127 try { 128 con = DbConnectionManager.getConnection(); 129 pstmt = con.prepareStatement(GET_USERLIST); 130 pstmt.setString(1,"WatchForum"+Integer.toString(ForumID)); 131 pstmt.setString(2,"WatchThread"+Integer.toString(ThreadID)); 132 pstmt.setString(3, "true"); 133 ResultSet rs = pstmt.executeQuery(); 134 135 while( rs.next() ) { 136 User user = factory.getProfileManager().getUser(rs.getInt("userID")); 137 if (user.getEmail()!=null && !"".equals(user.getEmail()) && user.getID()!=OriginalUserID){ 138 String emailBody = PropertyManager.getProperty("yazdThreadWatch.MailBody") + 140 " \n\r"+PropertyManager.getProperty("yazdUrl")+ 141 "viewThread.jsp?forum="+ForumID+"&thread="+ThreadID; 142 MailSender.send(PropertyManager.getProperty("yazdMailSMTPServer"), 143 PropertyManager.getProperty("yazdMailFrom"), 144 user.getEmail(), 145 PropertyManager.getProperty("yazdThreadWatch.MailSubject"), 146 emailBody); 147 } 148 149 } 150 } 151 catch( SQLException sqle ) { 152 System.err.println("EmailWatchException (394) Exception:"+sqle.getMessage()); 153 sqle.printStackTrace(); 154 } 155 catch (Exception e) { 156 System.err.println("EmailWatchManager (3847) Exception:"+e.getMessage()); 157 } 158 finally { 159 try { pstmt.close(); } 160 catch (Exception e) { e.printStackTrace(); } 161 try { con.close(); } 162 catch (Exception e) { e.printStackTrace(); } 163 } 164 165 166 } 167 168 } 169 } 170 | Popular Tags |