| 
001 /*002  * Copyright (C) 2006  exedio GmbH (www.exedio.com)
 003  *
 004  * This library is free software; you can redistribute it and/or
 005  * modify it under the terms of the GNU Lesser General Public
 006  * License as published by the Free Software Foundation; either
 007  * version 2.1 of the License, or (at your option) any later version.
 008  *
 009  * This library is distributed in the hope that it will be useful,
 010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 012  * Lesser General Public License for more details.
 013  *
 014  * You should have received a copy of the GNU Lesser General Public
 015  * License along with this library; if not, write to the Free Software
 016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 017  */
 018 package com.exedio.pop3serv.log4j;
 019
 020 import com.exedio.pop3serv.*;
 021 import java.io.IOException;
 022 import java.io.UnsupportedEncodingException;
 023 import java.util.ArrayList;
 024 import java.util.Date;
 025 import java.util.List;
 026 import org.apache.log4j.AppenderSkeleton;
 027 import org.apache.log4j.spi.LocationInfo;
 028 import org.apache.log4j.spi.LoggingEvent;
 029 import org.apache.log4j.spi.ThrowableInformation;
 030
 031 /**
 032  * This is a log4j {@link org.apache.log4j.Appender Appender} which provides each {@link LoggingEvent} as an email.
 033  * @author ebert
 034  */
 035 public class MailAppender extends AppenderSkeleton
 036 {
 037   private int thePort = 110;
 038   private String theUserName = "username";
 039   private String thePassword = "password";
 040   private LogMailDropManager theManager;
 041   private Server theServer;
 042
 043   public void activateOptions()
 044   {
 045     theManager = new LogMailDropManager(theUserName, thePassword);
 046     theServer = new Server(theManager);
 047     try
 048     {
 049       theServer.start(thePort);
 050     }
 051     catch(IOException e)
 052     {
 053       throw new RuntimeException(e);
 054     }
 055     super.activateOptions();
 056   }
 057
 058   public void close()
 059   {
 060     try
 061     {
 062       theServer.stop();
 063     }
 064     catch(IOException e)
 065     {
 066       throw new RuntimeException(e);
 067     }
 068     theServer = null;
 069     theManager = null;
 070   }
 071
 072   public void setPort(int port)
 073   {
 074     thePort = port;
 075   }
 076
 077   public void setUsername(String userName)
 078   {
 079     theUserName = userName;
 080   }
 081
 082   public void setPassword(String password)
 083   {
 084     thePassword = password;
 085   }
 086
 087   public boolean requiresLayout()
 088   {
 089     return false;
 090   }
 091
 092   protected void append(LoggingEvent loggingEvent)
 093   {
 094     try
 095     {
 096       theManager.append(getName(), loggingEvent);
 097     }
 098     catch(UnsupportedEncodingException e)
 099     {
 100       throw new RuntimeException(e);
 101     }
 102   }
 103
 104
 105
 106
 107 }
 |