1 package com.protomatter.syslog; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 import java.text.*; 57 58 import com.protomatter.util.*; 59 import com.protomatter.pool.*; 60 61 66 public class MailLog 67 extends BasicLogger 68 { 69 private SMTPMailTransport transport = null; 70 private String smtpServer = null; 71 private int port = 25; 72 private String fromName = "Syslog"; 73 private String fromAddress = null; 74 private Vector to = new Vector(); 75 private Vector cc = new Vector(); 76 private Vector bcc = new Vector(); 77 private boolean html = false; 78 private String workQueue = null; 79 80 private SyslogMailSubjectFormatter 81 subjectFormat = new SimpleSyslogMailSubjectFormatter(); 82 83 87 public MailLog(String workQueueName, String smtpServer) 88 { 89 this(workQueueName, smtpServer, 25); 90 } 91 92 96 public MailLog(String workQueueName, String smtpServer, int port) 97 { 98 this(); 99 transport = new SMTPMailTransport(smtpServer, port); 100 setWorkQueue(workQueueName); 101 } 102 103 106 public MailLog() 107 { 108 super(); 109 } 110 111 114 public void setTransportAgent(String agentName) 115 { 116 transport.setTransportAgentName(agentName); 117 } 118 121 public String getTransportAgent() 122 { 123 return transport.getTransportAgentName(); 124 } 125 126 129 public void setFromName(String fromName) 130 { 131 this.fromName = fromName; 132 } 133 136 public String getFromName() 137 { 138 return this.fromName; 139 } 140 141 144 public void setFromAddress(String fromAddress) 145 { 146 this.fromAddress = fromAddress; 147 } 148 151 public String getFromAddress() 152 { 153 return this.fromAddress; 154 } 155 156 159 public void setMailServer(String mailServer) 160 { 161 this.smtpServer = mailServer; 162 } 163 166 public String getMailServer() 167 { 168 return this.smtpServer; 169 } 170 171 174 public void setMailPort(int port) 175 { 176 this.port = port; 177 } 178 181 public int getMailPort() 182 { 183 return this.port; 184 } 185 186 189 public void setHTML(boolean html) 190 { 191 this.html = html; 192 } 193 196 public boolean getHTML() 197 { 198 return this.html; 199 } 200 201 204 public void setTo(Vector list) 205 { 206 this.to = list; 207 } 208 211 public Vector getTo() 212 { 213 return this.to; 214 } 215 216 219 public void setCC(Vector list) 220 { 221 this.cc = list; 222 } 223 226 public Vector getCC() 227 { 228 return this.cc; 229 } 230 231 234 public void setBCC(Vector list) 235 { 236 this.bcc = list; 237 } 238 241 public Vector getBCC() 242 { 243 return this.bcc; 244 } 245 246 251 public void setWorkQueue(String workQueue) 252 { 253 this.workQueue = workQueue; 254 } 255 256 261 public String getWorkQueue() 262 { 263 return this.workQueue; 264 } 265 266 269 public void setSubjectFormatter(SyslogMailSubjectFormatter subjectFormat) 270 { 271 this.subjectFormat = subjectFormat; 272 } 273 274 277 public SyslogMailSubjectFormatter getSubjectFormatter() 278 { 279 return this.subjectFormat; 280 } 281 282 285 public final void log(SyslogMessage message) 286 { 287 if (transport == null) 288 return; 289 290 if (workQueue == null) 291 { 292 sendMail(message); 293 } 294 else 295 { 296 Syslog.addWork(workQueue, new MailLogRunnable(this, message)); 298 } 299 } 300 301 private class MailLogRunnable 302 implements Runnable , ObjectPoolObject 303 { 304 private MailLog logger = null; 305 private SyslogMessage message = null; 306 307 public MailLogRunnable(MailLog logger, SyslogMessage message) 308 { 309 this.logger = logger; 310 this.message = message; 311 } 312 313 public void run() 314 { 315 logger.sendMail(message); 316 } 317 318 public void deleteObjectPoolObject() 319 { 320 } 321 322 public boolean isObjectPoolObjectValid() 323 { 324 return true; 325 } 326 327 public void beforeObjectPoolObjectCheckout() 328 { 329 } 330 331 public void afterObjectPoolObjectCheckin() 332 { 333 } 334 } 335 336 void sendMail(SyslogMessage message) 337 { 338 StringBuffer b = new StringBuffer (256); 339 formatLogEntry(b, message); 340 341 try 342 { 343 MailMessage m = new MailMessage(); 344 m.setTo(to); 345 m.setFromAddress(fromAddress); 346 m.setFromName(fromName); 347 m.setCC(cc); 348 m.setBCC(bcc); 349 m.setSubject(subjectFormat.formatMessageSubject(message)); 350 351 if (html) 352 { 353 MIMEMessage container = new MIMEMessage(); 354 MIMEAttachment a 355 = new MIMEAttachment("text/html", "message body", b.toString()); 356 container.addAttachment(a); 357 m.setBody(container); 358 } 359 else 360 { 361 m.setBody(b.toString()); 362 } 363 transport.sendMessage(m); 364 } 365 catch (IOException ix) 366 { 367 System.err.println(MessageFormat.format( 368 Syslog.getResourceString(MessageConstants.MAILLOG_CANNOT_WRITE_SMTP_MESSAGE), 369 new Object [] { ix.toString() })); 370 ix.printStackTrace(); 371 } 372 catch (MailException mx) 373 { 374 System.err.println(MessageFormat.format( 375 Syslog.getResourceString(MessageConstants.MAILLOG_CANNOT_WRITE_SMTP_MESSAGE), 376 new Object [] { mx.toString() })); 377 mx.printStackTrace(); 378 } 379 } 380 381 384 public synchronized void shutdown() 385 { 386 transport = null; 387 } 388 389 public void init() 390 { 391 transport = new SMTPMailTransport(smtpServer, port); 392 } 393 394 public void flush() 395 { 396 } 398 } 399 | Popular Tags |