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
22 package org.apache.james.pop3server;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.container.ContainerUtil;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.james.api.user.UsersRepository;
30 import org.apache.james.services.MailServer;
31 import org.apache.james.socket.AbstractJamesService;
32 import org.apache.james.socket.ProtocolHandler;
33
34 /**
35 * <p>Accepts POP3 connections on a server socket and dispatches them to POP3Handlers.</p>
36 *
37 * <p>Also responsible for loading and parsing POP3 specific configuration.</p>
38 *
39 * @version 1.0.0, 24/04/1999
40 */
41 public class POP3Server extends AbstractJamesService implements POP3ServerMBean {
42
43 /**
44 * The handler chain - POP3handlers can lookup handlerchain to obtain
45 * Command handlers , Message handlers and connection handlers
46 */
47 POP3HandlerChain handlerChain = new POP3HandlerChain();
48
49 /**
50 * The internal mail server service
51 */
52 private MailServer mailServer;
53
54 /**
55 * The user repository for this server - used to authenticate users.
56 */
57 private UsersRepository users;
58
59 /**
60 * The number of bytes to read before resetting
61 * the connection timeout timer. Defaults to
62 * 20 KB.
63 */
64 private int lengthReset = 20 * 1024;
65
66 /**
67 * The configuration data to be passed to the handler
68 */
69 private POP3HandlerConfigurationData theConfigData
70 = new POP3HandlerConfigurationDataImpl();
71
72 private ServiceManager serviceManager;
73
74 public void setMailServer(MailServer mailServer) {
75 this.mailServer = mailServer;
76 }
77
78 public void setUsers(UsersRepository users) {
79 this.users = users;
80 }
81
82 /**
83 * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
84 */
85 public void service( final ServiceManager componentManager )
86 throws ServiceException {
87 super.service(componentManager);
88 serviceManager = componentManager;
89 MailServer mailServer = (MailServer)componentManager.lookup( MailServer.ROLE );
90 setMailServer(mailServer);
91 UsersRepository users = (UsersRepository)componentManager.lookup( UsersRepository.ROLE );
92 setUsers(users);
93 }
94
95 /**
96 * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
97 */
98 public void configure(final Configuration configuration) throws ConfigurationException {
99 super.configure(configuration);
100 if (isEnabled()) {
101 Configuration handlerConfiguration = configuration.getChild("handler");
102 lengthReset = handlerConfiguration.getChild("lengthReset").getValueAsInteger(lengthReset);
103 if (getLogger().isInfoEnabled()) {
104 getLogger().info("The idle timeout will be reset every " + lengthReset + " bytes.");
105 }
106 //set the logger
107 ContainerUtil.enableLogging(handlerChain,getLogger());
108
109 try {
110 ContainerUtil.service(handlerChain,serviceManager);
111 } catch (ServiceException e) {
112 if (getLogger().isErrorEnabled()) {
113 getLogger().error("Failed to service handlerChain",e);
114 }
115 throw new ConfigurationException("Failed to service handlerChain");
116 }
117
118 //read from the XML configuration and create and configure each of the handlers
119 ContainerUtil.configure(handlerChain,handlerConfiguration.getChild("handlerchain"));
120
121 }
122 }
123
124 /**
125 * @see org.apache.james.socket.AbstractJamesService#getDefaultPort()
126 */
127 protected int getDefaultPort() {
128 return 110;
129 }
130
131 /**
132 * @see org.apache.james.socket.AbstractJamesService#getServiceType()
133 */
134 public String getServiceType() {
135 return "POP3 Service";
136 }
137
138
139 /**
140 * @see org.apache.james.socket.AbstractJamesService#newProtocolHandlerInstance()
141 */
142 public ProtocolHandler newProtocolHandlerInstance() {
143 POP3Handler protocolHandler = new POP3Handler();
144 //pass the handler chain to every POP3handler
145 protocolHandler.setHandlerChain(handlerChain);
146 return protocolHandler;
147 }
148
149 /**
150 * A class to provide POP3 handler configuration to the handlers
151 */
152 private class POP3HandlerConfigurationDataImpl
153 implements POP3HandlerConfigurationData {
154
155 /**
156 * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getHelloName()
157 */
158 public String getHelloName() {
159 if (POP3Server.this.helloName == null) {
160 return POP3Server.this.mailServer.getHelloName();
161 } else {
162 return POP3Server.this.helloName;
163 }
164 }
165
166 /**
167 * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getResetLength()
168 */
169 public int getResetLength() {
170 return POP3Server.this.lengthReset;
171 }
172
173 /**
174 * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getMailServer()
175 */
176 public MailServer getMailServer() {
177 return POP3Server.this.mailServer;
178 }
179
180 /**
181 * @see org.apache.james.pop3server.POP3HandlerConfigurationData#getUsersRepository()
182 */
183 public UsersRepository getUsersRepository() {
184 return POP3Server.this.users;
185 }
186 }
187
188 /**
189 * @see org.apache.james.socket.AbstractJamesService#getConfigurationData()
190 */
191 protected Object getConfigurationData() {
192 return theConfigData;
193 }
194 }