View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  package org.apache.james.postage.smtpserver;
22  
23  import org.apache.avalon.cornerstone.blocks.sockets.DefaultServerSocketFactory;
24  import org.apache.avalon.cornerstone.blocks.sockets.DefaultSocketFactory;
25  import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory;
26  import org.apache.avalon.cornerstone.services.sockets.SocketFactory;
27  import org.apache.avalon.cornerstone.services.sockets.SocketManager;
28  import org.apache.avalon.cornerstone.services.threads.ThreadManager;
29  import org.apache.avalon.excalibur.thread.impl.DefaultThreadPool;
30  import org.apache.avalon.framework.service.ServiceManager;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.excalibur.thread.ThreadPool;
34  import org.apache.james.postage.SamplingException;
35  import org.apache.james.postage.execution.Sampler;
36  import org.apache.james.postage.result.PostageRunnerResult;
37  import org.apache.james.services.JamesConnectionManager;
38  import org.apache.james.smtpserver.SMTPServer;
39  import org.apache.james.util.connection.SimpleConnectionManager;
40  
41  /***
42   * puts up a gateway SMTP server acting as a mail sink for the external mail sent out by James.<br/>
43   * mails are catched, test mails are identified and tracked
44   */
45  public class SMTPMailSink implements Sampler, SocketManager, ThreadManager {
46  
47      private static Log log = LogFactory.getLog(SMTPMailSink.class);
48  
49      private int m_smtpListenerPort = 2525;
50      private SimpleMailServer m_mailServer = new SimpleMailServer();
51      private SMTPServer m_smtpServer = new SMTPServer();
52  
53      public int getSmtpListenerPort() {
54          return m_smtpListenerPort;
55      }
56  
57      public void setSmtpListenerPort(int smtpListenerPort) {
58          m_smtpListenerPort = smtpListenerPort;
59      }
60  
61      public void setResults(PostageRunnerResult results) {
62          m_mailServer.setResults(results);
63      }
64      public void initialize() throws Exception {
65          m_smtpServer.enableLogging(new AvalonToPostageLogger());
66  
67          m_smtpServer.service(setUpServiceManager());
68  
69          SimpleSMTPServerConfiguration testConfiguration = new SimpleSMTPServerConfiguration(m_smtpListenerPort);
70          testConfiguration.init();
71          m_smtpServer.configure(testConfiguration);
72  
73          m_smtpServer.initialize();
74      }
75  
76      private ServiceManager setUpServiceManager() {
77          SimpleServiceManager serviceManager = new SimpleServiceManager();
78          SimpleConnectionManager connectionManager = new SimpleConnectionManager();
79          connectionManager.enableLogging(new AvalonToPostageLogger());
80          serviceManager.put(JamesConnectionManager.ROLE, connectionManager);
81          serviceManager.put("org.apache.mailet.MailetContext", new TrivialMailContext());
82          serviceManager.put("org.apache.james.services.MailServer", m_mailServer);
83          serviceManager.put("org.apache.james.services.UsersRepository", null); //m_usersRepository);
84          serviceManager.put(SocketManager.ROLE, this);
85          serviceManager.put(ThreadManager.ROLE, this);
86          return serviceManager;
87      }
88  
89      public ServerSocketFactory getServerSocketFactory(String string) throws Exception {
90          return new DefaultServerSocketFactory();
91      }
92  
93      public SocketFactory getSocketFactory(String string) throws Exception {
94          return new DefaultSocketFactory();
95      }
96  
97      public ThreadPool getThreadPool(String string) throws IllegalArgumentException {
98          return getDefaultThreadPool();
99      }
100 
101     public ThreadPool getDefaultThreadPool() {
102         try {
103             DefaultThreadPool defaultThreadPool = new DefaultThreadPool(1);
104             defaultThreadPool.enableLogging(new AvalonToPostageLogger());
105             return defaultThreadPool;
106         } catch (Exception e) {
107             e.printStackTrace();
108             return null;
109         }
110     }
111 
112     public void doSample() throws SamplingException {
113         log.debug("sampling while mails are coming in.");
114     }
115 }