1 19 package org.lucane.applications.sendmail; 20 21 import org.lucane.common.*; 22 import org.lucane.common.net.ObjectConnection; 23 import org.lucane.server.*; 24 import org.lucane.server.database.*; 25 26 import java.util.*; 27 import java.io.IOException ; 28 import java.sql.*; 29 30 import javax.mail.MessagingException ; 31 32 33 public class SendMailService extends Service 34 { 35 protected static String SMTP_HOST = "localhost"; 36 37 private DatabaseAbstractionLayer layer = null; 38 39 public SendMailService() 40 { 41 } 42 43 public void createTable() 44 { 45 try { 46 String dbDescription = getDirectory() + "db-sendmail.xml"; 47 layer.getTableCreator().createFromXml(dbDescription); 48 49 Connection connection = layer.getConnection(); 50 PreparedStatement insert = connection.prepareStatement( 51 "INSERT INTO smtphost VALUES (?)"); 52 insert.setString(1, SMTP_HOST); 53 insert.execute(); 54 insert.close(); 55 connection.close(); 56 } catch (Exception e) { 57 Logging.getLogger().severe("Unable to install SendMailService !"); 58 e.printStackTrace(); 59 } 60 } 61 62 public void init(Server parent) 63 { 64 layer = parent.getDBLayer(); 65 66 initSmtpHost(); 67 } 68 69 public void install() 70 { 71 createTable(); 72 } 73 74 private void initSmtpHost() 75 { 76 try { 77 if(!layer.hasTable("smtphost")) 78 { 79 Logging.getLogger().warning("No mailhost yet, using default: " + SMTP_HOST); 80 return; 81 } 82 83 Connection connection = layer.getConnection(); 84 PreparedStatement select = connection.prepareStatement( 85 "SELECT hostname FROM smtphost"); 86 ResultSet rs = select.executeQuery(); 87 if(rs.next()) 88 SMTP_HOST = rs.getString(1); 89 rs.close(); 90 select.close(); 91 connection.close(); 92 } catch(SQLException e) { 93 Logging.getLogger().warning("Unable to fetch the mailhost, using default: " + SMTP_HOST); 94 } 95 } 96 97 private void setSmtpHost(String host) 98 throws SQLException 99 { 100 SMTP_HOST = host; 101 102 Connection connection = layer.getConnection(); 103 PreparedStatement update = connection.prepareStatement( 104 "UPDATE smtphost SET hostname=?"); 105 update.setString(1, host); 106 update.execute(); 107 update.close(); 108 connection.close(); 109 } 110 111 private String getSmtpHost() 112 { 113 return SMTP_HOST; 114 } 115 116 public void process(ObjectConnection oc, Message message) 117 { 118 SendMailAction action = (SendMailAction)message.getData(); 119 120 try { 121 switch(action.getAction()) 122 { 123 case SendMailAction.GET_SMTPHOST: 124 oc.write("OK"); 125 oc.write(getSmtpHost()); 126 break; 127 128 case SendMailAction.SET_SMTPHOST: 129 setSmtpHost((String )action.getParam()); 130 oc.write("OK"); 131 break; 132 133 case SendMailAction.SEND_MAIL: 134 sendMail((HashMap)action.getParam()); 135 oc.write("OK"); 136 break; 137 } 138 } 139 catch(Exception e) { 140 try { 141 e.printStackTrace(); 142 oc.write("FAILED " + e); 143 } catch(Exception e2) {} 144 } 145 } 146 147 public void sendMail(HashMap params) throws IOException , MessagingException 148 { 149 String from = (String )params.get("from"); 151 String subject = (String )params.get("subject"); 152 String to = (String )params.get("to"); 153 String cc = (String )params.get("cc"); 154 String bcc = (String )params.get("bcc"); 155 String content = (String )params.get("content"); 156 String contentType = (String )params.get("content-type"); 157 HashMap attach = (HashMap)params.get("attach"); 158 159 if(from == null) 161 from = "SendMailService@" + SMTP_HOST; 162 if(content == null) 163 content = ""; 164 if(contentType == null) 165 contentType = "text/plain"; 166 167 SendableMail sm = new SendableMail(); 169 sm.setFrom(from); 170 sm.setContent(content, contentType); 171 if(subject != null) 172 sm.setSubject(subject); 173 if(to != null) 174 sm.addTo(to); 175 if(cc != null) 176 sm.addCc(cc); 177 if(bcc != null) 178 sm.addBcc(bcc); 179 180 if(attach != null) 182 { 183 Iterator i = attach.keySet().iterator(); 184 while(i.hasNext()) 185 { 186 String key = (String )i.next(); 187 String data = (String )attach.get(key); 188 if(key != null && data != null) 189 sm.attach(key, data); 190 } 191 } 192 193 sm.send(); 195 } 196 197 } 198 | Popular Tags |