View Javadoc

1   /*
2    * Wallace IMAP Server
3    * Copyright (C) 2004  Robert Newson
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package net.sf.wallace.mina;
20  
21  import java.io.IOException;
22  import java.net.InetSocketAddress;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.mina.io.filter.IoThreadPoolFilter;
27  import org.apache.mina.io.socket.SocketAcceptor;
28  import org.apache.mina.protocol.ProtocolProvider;
29  import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter;
30  import org.apache.mina.protocol.io.IoProtocolAcceptor;
31  
32  /***
33   * Run Wallace run.
34   * 
35   * @author rnewson
36   */
37  public final class WallaceServer {
38  
39      private final Log log = LogFactory.getLog(getClass());
40  
41      private int port;
42  
43      private IoThreadPoolFilter ioThreadPoolFilter;
44  
45      private ProtocolThreadPoolFilter protocolThreadPoolFilter;
46  
47      private ProtocolProvider protocolProvider;
48  
49      private IoProtocolAcceptor acceptor;
50  
51      public WallaceServer() {
52          // Empty.
53      }
54  
55      public void setProtocolProvider(final ProtocolProvider newProtocolProvider) {
56          protocolProvider = newProtocolProvider;
57      }
58  
59      public void setPort(final int newPort) {
60          port = newPort;
61      }
62  
63      public void start() throws IOException {
64          log.info("Wallace Server starting.");
65  
66          ioThreadPoolFilter = new IoThreadPoolFilter();
67          protocolThreadPoolFilter = new ProtocolThreadPoolFilter();
68  
69          ioThreadPoolFilter.start();
70          protocolThreadPoolFilter.start();
71  
72          acceptor = new IoProtocolAcceptor(new SocketAcceptor());
73  
74          acceptor.getIoAcceptor().addFilter(Integer.MAX_VALUE, ioThreadPoolFilter);
75          acceptor.addFilter(Integer.MAX_VALUE, protocolThreadPoolFilter);
76          acceptor.bind(new InetSocketAddress(port), protocolProvider);
77  
78          log.info("Wallace Server started.");
79      }
80  
81      public void stop() {
82          log.info("Wallace Server stopping.");
83  
84          ioThreadPoolFilter.stop();
85          protocolThreadPoolFilter.stop();
86          acceptor.unbind(new InetSocketAddress(port));
87  
88          log.info("Wallace Server stopped.");
89      }
90  
91  }