FileDocCategorySizeDatePackage
MailServer.javaAPI DocApache Axis 1.47731Sat Apr 22 18:57:26 BST 2006org.apache.axis.transport.mail

MailServer

public class MailServer extends Object implements Runnable
This is a simple implementation of an SMTP/POP3 server for processing SOAP requests via Apache's xml-axis. This is not intended for production use. Its intended uses are for demos, debugging, and performance profiling.
author
Davanum Srinivas
author
Rob Jellinghaus (robj@unrealities.com)

Fields Summary
protected static Log
log
private String
host
private int
port
private String
userid
private String
password
private static boolean
doThreads
private static org.apache.axis.server.AxisServer
myAxisServer
private boolean
stopped
private org.apache.commons.net.pop3.POP3Client
pop3
POP3 connection
Constructors Summary
public MailServer(String host, int port, String userid, String password)


             
        this.host = host;
        this.port = port;
        this.userid = userid;
        this.password = password;
    
Methods Summary
protected static synchronized org.apache.axis.server.AxisServergetAxisServer()


         
        if (myAxisServer == null) {
            myAxisServer = new AxisServer();
        }
        return myAxisServer;
    
public booleangetDoThreads()

        return doThreads;
    
public java.lang.StringgetHost()

        return host;
    
public org.apache.commons.net.pop3.POP3ClientgetPOP3()
Obtain the serverSocket that that MailServer is listening on.

        return pop3;
    
public static voidmain(java.lang.String[] args)
Server process.

        Options opts = null;
        try {
            opts = new Options(args);
        } catch (MalformedURLException e) {
            log.error(Messages.getMessage("malformedURLException00"), e);
            return;
        }

        try {
            doThreads = (opts.isFlagSet('t") > 0);
            String host = opts.getHost();
            int port = ((opts.isFlagSet('p") > 0) ? opts.getPort() : 110);
            POP3Client pop3 = new POP3Client();
            MailServer sas = new MailServer(host, port, opts.getUser(), opts.getPassword());

            sas.setPOP3(pop3);
            sas.start();
        } catch (Exception e) {
            log.error(Messages.getMessage("exception00"), e);
            return;
        }

    
public voidrun()
Accept requests from a given TCP port and send them through the Axis engine for processing.


                         
       
        log.info(Messages.getMessage("start00", "MailServer", host + ":" + port));

        // Accept and process requests from the socket
        while (!stopped) {
            try {
                pop3.connect(host, port);
                pop3.login(userid, password);

                POP3MessageInfo[] messages = pop3.listMessages();
                if (messages != null && messages.length > 0) {
                    for (int i = 0; i < messages.length; i++) {
                        Reader reader = pop3.retrieveMessage(messages[i].number);
                        if (reader == null) {
                            continue;
                        }

                        StringBuffer buffer = new StringBuffer();
                        BufferedReader bufferedReader =
                                new BufferedReader(reader);
                        int ch;
                        while ((ch = bufferedReader.read()) != -1) {
                            buffer.append((char) ch);
                        }
                        bufferedReader.close();
                        ByteArrayInputStream bais = new ByteArrayInputStream(buffer.toString().getBytes());
                        Properties prop = new Properties();
                        Session session = Session.getDefaultInstance(prop, null);

                        MimeMessage mimeMsg = new MimeMessage(session, bais);
                        pop3.deleteMessage(messages[i].number);
                        if (mimeMsg != null) {
                            MailWorker worker = new MailWorker(this, mimeMsg);
                            if (doThreads) {
                                Thread thread = new Thread(worker);
                                thread.setDaemon(true);
                                thread.start();
                            } else {
                                worker.run();
                            }
                        }
                    }
                }
            } catch (java.io.InterruptedIOException iie) {
            } catch (Exception e) {
                log.debug(Messages.getMessage("exception00"), e);
                break;
            } finally {
                try {
                    pop3.logout();
                    pop3.disconnect();
                    Thread.sleep(3000);
                } catch (Exception e) {
                    log.error(Messages.getMessage("exception00"), e);
                }
            }
        }
        log.info(Messages.getMessage("quit00", "MailServer"));
    
public voidsetDoThreads(boolean value)


        
        doThreads = value;
    
public voidsetPOP3(org.apache.commons.net.pop3.POP3Client pop3)
Set the serverSocket this server should listen on. (note : changing this will not affect a running server, but if you stop() and then start() the server, the new socket will be used).

        this.pop3 = pop3;
    
public voidstart()
Start this server as a NON-daemon.

        start(false);
    
public voidstart(boolean daemon)
Start this server. Spawns a worker thread to listen for HTTP requests.

param
daemon a boolean indicating if the thread should be a daemon.

        if (doThreads) {
            Thread thread = new Thread(this);
            thread.setDaemon(daemon);
            thread.start();
        } else {
            run();
        }
    
public voidstop()
Stop this server. This will interrupt any pending accept().

        /* 
         * Close the server socket cleanly, but avoid fresh accepts while
         * the socket is closing.
         */
        stopped = true;
        log.info(Messages.getMessage("quit00", "MailServer"));

        // Kill the JVM, which will interrupt pending accepts even on linux.
        System.exit(0);