001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.transport.mail.server;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.context.ConfigurationContext;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import java.io.IOException;
028: import java.net.ServerSocket;
029: import java.net.Socket;
030:
031: public class SMTPServer extends Thread {
032: private static final Log log = LogFactory.getLog(SMTPServer.class);
033: private boolean actAsMailet = false;
034: private boolean running = false;
035: private ConfigurationContext configurationContext;
036: private int port;
037: private ServerSocket ss;
038: private Storage st;
039:
040: public SMTPServer(Storage st, int port) {
041: this .st = st;
042: this .port = port;
043: actAsMailet = false;
044: }
045:
046: public SMTPServer(Storage st,
047: ConfigurationContext configurationContext, int port) {
048: this .st = st;
049: this .configurationContext = configurationContext;
050: this .port = port;
051: actAsMailet = true;
052: }
053:
054: public void run() {
055: runServer();
056: }
057:
058: public void runServer() {
059: try {
060: synchronized (this ) {
061: running = true;
062: ss = new ServerSocket(port);
063: log.info("SMTP Server started on port " + port);
064: }
065: } catch (IOException ex) {
066: log.info(ex.getMessage());
067: }
068:
069: while (running) {
070: try {
071:
072: // wait for a client
073: Socket socket = ss.accept();
074: SMTPWorker thread = null;
075:
076: if (actAsMailet) {
077: thread = new SMTPWorker(socket, st,
078: configurationContext);
079: } else {
080: thread = new SMTPWorker(socket, st);
081: }
082:
083: thread.start();
084: } catch (IOException ex) {
085: if (running) {
086: log.info(ex.getMessage());
087: }
088: }
089: }
090: }
091:
092: public void stopServer() throws AxisFault {
093: try {
094: synchronized (this ) {
095: running = false;
096: ss.close();
097: ss = null;
098: }
099: } catch (IOException e) {
100: throw new AxisFault(e.getMessage(), e);
101: }
102: }
103: }
|