james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-james/src/org/apache/james/smtpserver SMTPHandler.java SMTPServer.java SizeLimitedSMTPHandler.java
Date Mon, 05 Mar 2001 16:18:11 GMT
donaldp     01/03/05 08:18:11

  Modified:    src/org/apache/james/smtpserver SMTPHandler.java
                        SMTPServer.java SizeLimitedSMTPHandler.java
  Log:
  Updated to latest configuration and also latest scheduler/connection/sockets2
  
  Revision  Changes    Path
  1.29      +80 -70    jakarta-james/src/org/apache/james/smtpserver/SMTPHandler.java
  
  Index: SMTPHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/org/apache/james/smtpserver/SMTPHandler.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- SMTPHandler.java	2001/02/03 18:21:29	1.28
  +++ SMTPHandler.java	2001/03/05 16:18:10	1.29
  @@ -1,33 +1,37 @@
  -/*****************************************************************************
  - * Copyright (C) The Apache Software Foundation. All rights reserved.        *
  - * ------------------------------------------------------------------------- *
  - * This software is published under the terms of the Apache Software License *
  - * version 1.1, a copy of which has been included  with this distribution in *
  - * the LICENSE file.                                                         *
  - *****************************************************************************/
  +/*
  + * Copyright (C) The Apache Software Foundation. All rights reserved.
  + *
  + * This software is published under the terms of the Apache Software License
  + * version 1.1, a copy of which has been included with this distribution in
  + * the LICENSE file.
  + */
   package org.apache.james.smtpserver;
   
   import java.io.*;
   import java.net.*;
   import java.util.*;
  -
   import javax.mail.*;
   import javax.mail.internet.*;
  -
  -import org.apache.avalon.*;
  -import org.apache.cornerstone.services.Scheduler;
  -
  +import org.apache.avalon.AbstractLoggable;
  +import org.apache.avalon.ComponentManager;
  +import org.apache.avalon.Composer;
  +import org.apache.avalon.Context;
  +import org.apache.avalon.Contextualizable;
  +import org.apache.avalon.Stoppable;
  +import org.apache.avalon.Initializable;
  +import org.apache.avalon.configuration.Configurable;
  +import org.apache.avalon.configuration.Configuration;
  +import org.apache.avalon.configuration.ConfigurationException;
  +import org.apache.cornerstone.services.connection.ConnectionHandler;
  +import org.apache.cornerstone.services.scheduler.PeriodicTimeTrigger;
  +import org.apache.cornerstone.services.scheduler.Target;
  +import org.apache.cornerstone.services.scheduler.TimeScheduler;
   import org.apache.james.*;
   import org.apache.james.core.*;
   import org.apache.james.services.MailServer;
   import org.apache.james.util.*;
  -
  -import org.apache.log.LogKit;
  -import org.apache.log.Logger;
  -
   import org.apache.mailet.*;
   
  -
   /**
    * This handles an individual incoming message.  It handles regular SMTP
    * commands, and when it receives a message, adds it to the spool.
  @@ -35,7 +39,10 @@
    * @author Federico Barbieri <scoobie@systemy.it>
    * @version 0.9
    */
  -public class SMTPHandler implements Composer, Configurable, Runnable, Stoppable, Scheduler.Target,
Contextualizable {
  +public class SMTPHandler 
  +    extends AbstractLoggable
  +    implements ConnectionHandler, Contextualizable, Composer, Configurable, 
  +    Initializable, Target  {
   
       public final static String SERVER_NAME = "SERVER_NAME";
       public final static String SERVER_TYPE = "SERVER_TYPE";
  @@ -62,8 +69,7 @@
       private ComponentManager compMgr;
       private Configuration conf;
       private Context context;
  -    private Logger logger =  LogKit.getLoggerFor("james.SMTPServer");
  -    private Scheduler scheduler;
  +    private TimeScheduler scheduler;
       private MailServer mailServer;
   
       private String servername;
  @@ -71,11 +77,11 @@
       private static long count;
       private Hashtable state;
       private Random random;
  -    private long timeout;
  +    private int timeout;
   
       public void configure(Configuration conf) throws ConfigurationException {
           this.conf = conf;
  -	timeout = conf.getChild("connectiontimeout").getValueAsLong(120000);
  +        timeout = conf.getChild("connectiontimeout").getValueAsInt(120000);
       }
   
       public void  contextualize(Context context) {
  @@ -88,16 +94,25 @@
   
       public void init() throws Exception {
           mailServer = (MailServer) compMgr.lookup("org.apache.james.services.MailServer");
  -        scheduler = (Scheduler) compMgr.lookup("org.apache.cornerstone.services.Scheduler");
  +        scheduler = (TimeScheduler) compMgr.
  +            lookup("org.apache.cornerstone.services.scheduler.TimeScheduler");
           servername = (String) context.get(Constants.HELO_NAME);
           state = new Hashtable();
           random = new Random();
       }
  -
  -    public void parseRequest(Socket socket) {
   
  +    /**
  +     * Handle a connection.
  +     * This handler is responsible for processing connections as they occur.
  +     *
  +     * @param connection the connection
  +     * @exception IOException if an error reading from socket occurs
  +     * @exception ProtocolException if an error handling connection occurs
  +     */
  +    public void handleConnection( Socket connection ) 
  +        throws IOException {
           try {
  -            this.socket = socket;
  +            this.socket = connection;
               socketIn = new BufferedInputStream(socket.getInputStream(), 1024);
               in = new DataInputStream(socketIn);
               out = new InternetPrintWriter(socket.getOutputStream(), true);
  @@ -112,47 +127,45 @@
               state.put(REMOTE_IP, remoteIP);
               state.put(SMTP_ID, smtpID);
           } catch (Exception e) {
  -            logger.error("Cannot open connection from " + remoteHost + " (" + remoteIP
+ "): " + e.getMessage());
  +            getLogger().error("Cannot open connection from " + remoteHost + " (" + remoteIP
+ "): " + e.getMessage(), e );
               throw new RuntimeException("Cannot open connection from " + remoteHost + "
(" + remoteIP + "): " + e.getMessage());
           }
  -
  -        logger.info("Connection from " + remoteHost + " (" + remoteIP + ")");
  -    }
   
  -    public void run() {
  +        getLogger().info("Connection from " + remoteHost + " (" + remoteIP + ")");
   
           try {
               // Initially greet the connector
               // Format is:  Sat,  24 Jan 1998 13:16:09 -0500
   
  -            scheduler.setAlarm(this.toString(), new Scheduler.Alarm(timeout), this);
  +            final PeriodicTimeTrigger trigger = new PeriodicTimeTrigger( timeout, -1 );
  +            scheduler.addTrigger( this.toString(), trigger, this );
               out.println("220 " + this.servername + " SMTP Server (" + softwaretype + ")
ready " + RFC822DateFormat.toString(new Date()));
   
               while  (parseCommand(in.readLine())) {
  -                scheduler.resetAlarm(this.toString());
  +                scheduler.resetTrigger(this.toString());
               }
               socket.close();
  -            scheduler.removeAlarm(this.toString());
  +            scheduler.removeTrigger(this.toString());
           } catch (SocketException e) {
  -            logger.debug("Socket to " + remoteHost + " closed remotely.");
  +            getLogger().debug("Socket to " + remoteHost + " closed remotely.");
           } catch (InterruptedIOException e) {
  -            logger.debug("Socket to " + remoteHost + " timeout.");
  +            getLogger().debug("Socket to " + remoteHost + " timeout.");
           } catch (IOException e) {
  -            logger.debug("Exception handling socket to " + remoteHost + ":"
  -			 + e.getMessage());
  +            getLogger().debug("Exception handling socket to " + remoteHost + ":"
  +                              + e.getMessage());
           } catch (Exception e) {
  -            logger.debug("Exception opening socket: " + e.getMessage());
  +            getLogger().debug("Exception opening socket: " + e.getMessage());
           } finally {
               try {
  -            socket.close();
  +                socket.close();
               } catch (IOException e) {
  -                logger.error("Exception closing socket: " + e.getMessage());
  +                getLogger().error("Exception closing socket: " + e.getMessage());
               }
           }
       }
   
  -    public void wake(String name, Scheduler.Event event) {
  -        logger.error("Connection timeout on socket");
  +    public void targetTriggered( final String triggerName ) {
  +        getLogger().error("Connection timeout on socket");
           try {
               out.println("Connection timeout. Closing connection");
               socket.close();
  @@ -161,10 +174,10 @@
       }
   
       private boolean parseCommand(String command)
  -	throws Exception {
  +        throws Exception {
   
           if (command == null) return false;
  -        logger.info("Command received: " + command);
  +        getLogger().info("Command received: " + command);
           StringTokenizer commandLine = new StringTokenizer(command.trim(), " :");
           int arguments = commandLine.countTokens();
           if (arguments == 0) {
  @@ -180,7 +193,7 @@
           if(arguments > 2) {
               argument1 = commandLine.nextToken();
           }
  -            // HELO Command
  +        // HELO Command
           if (command.equalsIgnoreCase("HELO")) {
               if (state.containsKey(CURRENT_HELO_MODE)) {
                   out.println("250 " + state.get(SERVER_NAME) + " Duplicate HELO/EHLO");
  @@ -220,8 +233,8 @@
                   String sender = argument1.trim();
                   if (!sender.startsWith("<") || !sender.endsWith(">")) {
                       out.println("501 Syntax error in parameters or arguments");
  -                    logger.error("Error parsing sender address: " + sender
  -				 + ": did not start and end with < >");
  +                    getLogger().error("Error parsing sender address: " + sender
  +                                      + ": did not start and end with < >");
                       return true;
                   }
                   MailAddress senderAddress = null;
  @@ -231,8 +244,8 @@
                       senderAddress = new MailAddress(sender);
                   } catch (Exception pe) {
                       out.println("501 Syntax error in parameters or arguments");
  -                    logger.error("Error parsing sender address: " + sender
  -				 + ": " + pe.getMessage());
  +                    getLogger().error("Error parsing sender address: " + sender
  +                                      + ": " + pe.getMessage());
                       return true;
                   }
                   state.put(SENDER, senderAddress);
  @@ -255,9 +268,9 @@
                   String recipient = argument1.trim();
                   if (!recipient.startsWith("<") || !recipient.endsWith(">")) {
                       out.println("Syntax error in parameters or arguments");
  -                    logger.error("Error parsing recipient address: "
  -				 + recipient
  -				 + ": did not start and end with < >");
  +                    getLogger().error("Error parsing recipient address: "
  +                                      + recipient
  +                                      + ": did not start and end with < >");
                       return true;
                   }
                   MailAddress recipientAddress = null;
  @@ -267,8 +280,8 @@
                       recipientAddress = new MailAddress(recipient);
                   } catch (Exception pe) {
                       out.println("501 Syntax error in parameters or arguments");
  -                    logger.error("Error parsing recipient address: "
  -				 + recipient + ": " + pe.getMessage());
  +                    getLogger().error("Error parsing recipient address: "
  +                                      + recipient + ": " + pe.getMessage());
                       return true;
                   }
                   rcptColl.add(recipientAddress);
  @@ -278,13 +291,13 @@
               }
               // NOOP Command
           } else if (command.equalsIgnoreCase("NOOP")) {
  -                out.println("250 OK");
  -                return true;
  +            out.println("250 OK");
  +            return true;
               // DATA Command
           } else if (command.equalsIgnoreCase("RSET")) {
  -                resetState();
  -                out.println("250 OK");
  -                return true;
  +            resetState();
  +            out.println("250 OK");
  +            return true;
               // DATA Command
           } else if (command.equalsIgnoreCase("DATA")) {
               if (!state.containsKey(SENDER)) {
  @@ -304,10 +317,10 @@
                           headers.setHeader("Date", RFC822DateFormat.toString (new Date ()));
                       }
                       /*
  -                    We no longer add To as this in practice is not set (from what I've
seen)
  -                    if (!headers.isSet("To")) {
  -                        headers.setHeader("To", );
  -                    }
  +                      We no longer add To as this in practice is not set (from what I've
seen)
  +                      if (!headers.isSet("To")) {
  +                      headers.setHeader("To", );
  +                      }
                       */
                       if (!headers.isSet("From")) {
                           headers.setHeader("From", state.get(SENDER).toString());
  @@ -333,11 +346,11 @@
                       mailServer.sendMail(mail);
                   } catch (MessagingException me) {
                       out.println("451 Error processing message: " + me.getMessage());
  -                    logger.error("Error processing message: "
  -				 + me.getMessage());
  +                    getLogger().error("Error processing message: "
  +                                      + me.getMessage());
                       return true;
                   }
  -                logger.info("Mail sent to Mail Server");
  +                getLogger().info("Mail sent to Mail Server");
                   resetState();
                   out.println("250 Message received");
                   return true;
  @@ -358,8 +371,5 @@
           state.put(REMOTE_NAME, remoteHost);
           state.put(REMOTE_IP, remoteIP);
           state.put(SMTP_ID, smtpID);
  -    }
  -
  -    public void stop() {
       }
   }
  
  
  
  1.19      +41 -67    jakarta-james/src/org/apache/james/smtpserver/SMTPServer.java
  
  Index: SMTPServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/org/apache/james/smtpserver/SMTPServer.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- SMTPServer.java	2001/02/18 05:41:56	1.18
  +++ SMTPServer.java	2001/03/05 16:18:10	1.19
  @@ -1,88 +1,62 @@
  -/*****************************************************************************
  - * Copyright (C) The Apache Software Foundation. All rights reserved.        *
  - * ------------------------------------------------------------------------- *
  - * This software is published under the terms of the Apache Software License *
  - * version 1.1, a copy of which has been included  with this distribution in *
  - * the LICENSE file.                                                         *
  - *****************************************************************************/
  -
  +/*
  + * Copyright (C) The Apache Software Foundation. All rights reserved.
  + *
  + * This software is published under the terms of the Apache Software License
  + * version 1.1, a copy of which has been included with this distribution in
  + * the LICENSE file.
  + */
   package org.apache.james.smtpserver;
   
  -import java.net.*;
  -import org.apache.avalon.*;
  -import org.apache.avalon.util.lang.ThreadManager;
  -import org.apache.avalon.util.thread.ThreadPool;
  -import org.apache.cornerstone.services.SocketServer;
  -import org.apache.james.*;
  +import java.net.InetAddress;
  +import java.net.UnknownHostException;
  +import org.apache.avalon.configuration.Configuration;
  +import org.apache.avalon.configuration.ConfigurationException;
  +import org.apache.cornerstone.services.connection.AbstractService;
  +import org.apache.cornerstone.services.connection.ConnectionHandlerFactory;
  +import org.apache.cornerstone.services.connection.DefaultHandlerFactory;
  +import org.apache.phoenix.Block;
   
   /**
  + *
    * @version 1.1.0, 06/02/2001
    * @author  Federico Barbieri <scoobie@pop.systemy.it>
    * @author  Matthew Pangaro <mattp@lokitech.com>
  + * @author  <a href="mailto:donaldp@apache.org">Peter Donald</a>
    */
   public class SMTPServer 
  -    extends AbstractLoggable
  -    implements SocketServer.SocketHandler, Configurable, Composer, Contextualizable {
  +    extends AbstractService {
   
  -    private Context context;
  -    private Configuration conf;
  -    private ComponentManager compMgr;
  -    private ThreadPool threadPool;
  -    private String handlerStartMesg = "Executing handler";
  -    
  -    public void configure(Configuration conf) throws ConfigurationException{
  -        this.conf = conf;
  -    }
  -    
  -    public void compose(ComponentManager comp) {
  -        compMgr = comp;
  -    }
  -    
  -    public void contextualize(Context context) {
  -        this.context = context;
  +    protected ConnectionHandlerFactory createFactory()
  +    {
  +        return new DefaultHandlerFactory( SMTPHandler.class );
       }
   
  -    public void init() throws Exception {
  +    public void configure( final Configuration configuration )
  +        throws ConfigurationException {
   
  -        getLogger().info("SMTPServer init...");
  -        threadPool = ThreadManager.getWorkerPool("whateverNameYouFancy");
  -        SocketServer socketServer = (SocketServer) compMgr.lookup("org.apache.cornerstone.services.SocketServer");
  -        int port = conf.getChild("port").getValueAsInt(25);
  -        InetAddress bind = null;
  -        try {
  -            String bindTo = conf.getChild("bind").getValue();
  -            if (bindTo.length() > 0) {
  -                bind = InetAddress.getByName(bindTo);
  -            }
  -            //get the configured max message size so we can log it
  -            long limit =
  -                conf.getChild("smtphandler").getChild("maxmessagesize").getValueAsLong(0);
  -            if (limit > 0) {
  -                handlerStartMesg += " with message size limit : " + limit 
  -                    + " KBytes";
  +        m_port = configuration.getChild( "port" ).getValueAsInt( 25 );
  +
  +        try 
  +        { 
  +            final String bindAddress = configuration.getChild( "bind" ).getValue( null
);
  +            if( null != bindAddress )
  +            {
  +                m_bindTo = InetAddress.getByName( bindAddress ); 
               }
  -        } catch (ConfigurationException e) {
           }
  -        socketServer.openListener("SMTPListener", SocketServer.DEFAULT, port, bind, this);
  -        getLogger().info("SMTPServer ...init end");
  +        catch( final UnknownHostException unhe ) 
  +        {
  +            throw new ConfigurationException( "Malformed bind parameter", unhe );
  +        }
  +
  +        super.configure( configuration.getChild( "smtphandler" ) );
       }
   
  -    public void parseRequest(Socket s) {
  +    public void init() throws Exception {
   
  -        try {
  -            SMTPHandler smtpHandler = new SMTPHandler();
  -            smtpHandler.configure(conf.getChild("smtphandler"));
  -            smtpHandler.contextualize(context);
  -            smtpHandler.compose(compMgr);
  -            smtpHandler.init();
  -            smtpHandler.parseRequest(s);
  -            threadPool.execute((Runnable)smtpHandler);
  -            getLogger().debug(handlerStartMesg);
  -        } catch (Exception e) {
  -            getLogger().error("Cannot parse request on socket " + s + " : "
  -                              + e.getMessage());
  -            e.printStackTrace();
  -        }
  +        getLogger().info("SMTPServer init...");
  +        super.init();
  +        getLogger().info("SMTPServer ...init end");
       }
   }
       
  
  
  
  1.2       +8 -1      jakarta-james/src/org/apache/james/smtpserver/SizeLimitedSMTPHandler.java
  
  Index: SizeLimitedSMTPHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/org/apache/james/smtpserver/SizeLimitedSMTPHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SizeLimitedSMTPHandler.java	2001/02/09 14:59:51	1.1
  +++ SizeLimitedSMTPHandler.java	2001/03/05 16:18:10	1.2
  @@ -14,7 +14,14 @@
   import javax.mail.*;
   import javax.mail.internet.*;
   
  -import org.apache.avalon.*;
  +import org.apache.avalon.Contextualizable;
  +import org.apache.avalon.Context;
  +import org.apache.avalon.Composer;
  +import org.apache.avalon.ComponentManager;
  +import org.apache.avalon.configuration.Configurable;
  +import org.apache.avalon.configuration.Configuration;
  +import org.apache.avalon.configuration.ConfigurationException;
  +import org.apache.avalon.Stoppable;
   import org.apache.cornerstone.services.Scheduler;
   
   import org.apache.james.*;
  
  
  

Mime
View raw message