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.nntpserver;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.james.api.user.UsersRepository;
29 import org.apache.james.nntpserver.repository.NNTPRepository;
30 import org.apache.james.services.MailServer;
31 import org.apache.james.socket.AbstractJamesService;
32 import org.apache.james.socket.ProtocolHandler;
33
34 /**
35 * NNTP Server
36 *
37 */
38 public class NNTPServer extends AbstractJamesService implements NNTPServerMBean {
39
40 /**
41 * Whether authentication is required to access this NNTP server
42 */
43 private boolean authRequired = false;
44
45 /**
46 * The repository that stores the news articles for this NNTP server.
47 */
48 private NNTPRepository nntpRepository;
49
50 /**
51 * The repository that stores the local users. Used for authentication.
52 */
53 private UsersRepository userRepository = null;
54
55 private MailServer mailServer;
56
57 /**
58 * Set the UserRepository
59 *
60 * @param userRepository the UserRepository
61 */
62 public void setUserRepository(UsersRepository userRepository) {
63 this.userRepository = userRepository;
64 }
65
66 /**
67 * Set the NNTPRepository
68 *
69 * @param nntpRepository the NNTPRepository
70 */
71 public void setRepository(NNTPRepository nntpRepository) {
72 this.nntpRepository = nntpRepository;
73 }
74
75 public void setMailServer(MailServer mailServer) {
76 this.mailServer = mailServer;
77 }
78
79 /**
80 * The configuration data to be passed to the handler
81 */
82 private NNTPHandlerConfigurationData theConfigData
83 = new NNTPHandlerConfigurationDataImpl();
84
85 /**
86 * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
87 */
88 public void service( final ServiceManager componentManager )
89 throws ServiceException {
90 super.service(componentManager);
91 UsersRepository userRepository = (UsersRepository)componentManager.lookup(UsersRepository.ROLE);
92 setUserRepository(userRepository);
93
94 NNTPRepository repo = (NNTPRepository)componentManager
95 .lookup(NNTPRepository.ROLE);
96 setRepository(repo);
97
98 setMailServer((MailServer) componentManager.lookup(MailServer.ROLE));
99 }
100
101 /**
102 * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
103 */
104 public void configure(final Configuration configuration) throws ConfigurationException {
105 super.configure(configuration);
106 if (isEnabled()) {
107 Configuration handlerConfiguration = configuration.getChild("handler");
108 authRequired =
109 handlerConfiguration.getChild("authRequired").getValueAsBoolean(false);
110 if (getLogger().isDebugEnabled()) {
111 if (authRequired) {
112 getLogger().debug("NNTP Server requires authentication.");
113 } else {
114 getLogger().debug("NNTP Server doesn't require authentication.");
115 }
116 }
117 }
118 }
119
120 /**
121 * @see org.apache.james.socket.AbstractJamesService#getDefaultPort()
122 */
123 protected int getDefaultPort() {
124 return 119;
125 }
126
127 /**
128 * @see org.apache.james.socket.AbstractJamesService#getServiceType()
129 */
130 public String getServiceType() {
131 return "NNTP Service";
132 }
133
134 /**
135 * @see org.apache.james.socket.AbstractJamesService#newProtocolHandlerInstance()
136 */
137 public ProtocolHandler newProtocolHandlerInstance() {
138 return new NNTPHandler();
139 }
140
141 /**
142 * A class to provide NNTP handler configuration to the handlers
143 */
144 private class NNTPHandlerConfigurationDataImpl
145 implements NNTPHandlerConfigurationData {
146
147 /**
148 * @see org.apache.james.nntpserver.NNTPHandlerConfigurationData#getHelloName()
149 */
150 public String getHelloName() {
151 if (NNTPServer.this.helloName == null) {
152 return NNTPServer.this.mailServer.getHelloName();
153 } else {
154 return NNTPServer.this.helloName;
155 }
156 }
157
158 /**
159 * @see org.apache.james.nntpserver.NNTPHandlerConfigurationData#isAuthRequired()
160 */
161 public boolean isAuthRequired() {
162 return NNTPServer.this.authRequired;
163 }
164
165 /**
166 * @see org.apache.james.nntpserver.NNTPHandlerConfigurationData#getUsersRepository()
167 */
168 public UsersRepository getUsersRepository() {
169 return NNTPServer.this.userRepository;
170 }
171
172 /**
173 * @see org.apache.james.nntpserver.NNTPHandlerConfigurationData#getNNTPRepository()
174 */
175 public NNTPRepository getNNTPRepository() {
176 return NNTPServer.this.nntpRepository;
177 }
178
179 }
180
181 /**
182 * @see org.apache.james.socket.AbstractJamesService#getConfigurationData()
183 */
184 protected Object getConfigurationData() {
185 return theConfigData;
186 }
187 }