KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > nntpserver > NNTPServer


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.nntpserver;
19
20 import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
21 import org.apache.avalon.excalibur.pool.DefaultPool;
22 import org.apache.avalon.excalibur.pool.HardResourceLimitingPool;
23 import org.apache.avalon.excalibur.pool.ObjectFactory;
24 import org.apache.avalon.excalibur.pool.Pool;
25 import org.apache.avalon.excalibur.pool.Poolable;
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.activity.Initializable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.component.Component;
31 import org.apache.avalon.framework.component.ComponentException;
32 import org.apache.avalon.framework.component.ComponentManager;
33 import org.apache.avalon.framework.component.Composable;
34 import org.apache.avalon.framework.logger.LogEnabled;
35
36 import org.apache.james.core.AbstractJamesService;
37 import org.apache.james.nntpserver.repository.NNTPRepository;
38 import org.apache.james.services.UsersRepository;
39 import org.apache.james.services.UsersStore;
40 import org.apache.james.util.watchdog.Watchdog;
41 import org.apache.james.util.watchdog.WatchdogFactory;
42 import org.apache.james.util.watchdog.WatchdogTarget;
43
44 import java.net.InetAddress JavaDoc;
45 import java.net.UnknownHostException JavaDoc;
46
47 /**
48  * NNTP Server
49  *
50  */

51 public class NNTPServer extends AbstractJamesService implements Component, NNTPServerMBean {
52
53     /**
54      * Whether authentication is required to access this NNTP server
55      */

56     private boolean authRequired = false;
57
58     /**
59      * The repository that stores the news articles for this NNTP server.
60      */

61     private NNTPRepository repo;
62
63     /**
64      * The repository that stores the local users. Used for authentication.
65      */

66     private UsersRepository userRepository = null;
67
68     /**
69      * The pool used to provide NNTP Handler objects
70      */

71     private Pool theHandlerPool = null;
72
73     /**
74      * The pool used to provide NNTP Handler objects
75      */

76     private ObjectFactory theHandlerFactory = new NNTPHandlerFactory();
77
78     /**
79      * The factory used to generate Watchdog objects
80      */

81     private WatchdogFactory theWatchdogFactory;
82
83     /**
84      * The configuration data to be passed to the handler
85      */

86     private NNTPHandlerConfigurationData theConfigData
87         = new NNTPHandlerConfigurationDataImpl();
88
89     /**
90      * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
91      */

92     public void compose( final ComponentManager componentManager )
93         throws ComponentException {
94         super.compose(componentManager);
95         UsersStore usersStore = (UsersStore)componentManager.lookup(UsersStore.ROLE);
96         userRepository = usersStore.getRepository("LocalUsers");
97         if (userRepository == null) {
98             throw new ComponentException("The user repository could not be found.");
99         }
100
101         repo = (NNTPRepository)componentManager
102             .lookup("org.apache.james.nntpserver.repository.NNTPRepository");
103
104     }
105
106     /**
107      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
108      */

109     public void configure(final Configuration configuration) throws ConfigurationException {
110         super.configure(configuration);
111         if (isEnabled()) {
112             Configuration handlerConfiguration = configuration.getChild("handler");
113             authRequired =
114                 handlerConfiguration.getChild("authRequired").getValueAsBoolean(false);
115             if (getLogger().isDebugEnabled()) {
116                 if (authRequired) {
117                     getLogger().debug("NNTP Server requires authentication.");
118                 } else {
119                     getLogger().debug("NNTP Server doesn't require authentication.");
120                 }
121             }
122         }
123     }
124
125     /**
126      * @see org.apache.avalon.framework.activity.Initializable#initialize()
127      */

128     public void initialize() throws Exception JavaDoc {
129         super.initialize();
130         if (!isEnabled()) {
131             return;
132         }
133
134         if (connectionLimit != null) {
135             theHandlerPool = new HardResourceLimitingPool(theHandlerFactory, 5, connectionLimit.intValue());
136             getLogger().debug("Using a bounded pool for NNTP handlers with upper limit " + connectionLimit.intValue());
137         } else {
138             // NOTE: The maximum here is not a real maximum. The handler pool will continue to
139
// provide handlers beyond this value.
140
theHandlerPool = new DefaultPool(theHandlerFactory, null, 5, 30);
141             getLogger().debug("Using an unbounded pool for NNTP handlers.");
142         }
143         if (theHandlerPool instanceof LogEnabled) {
144             ((LogEnabled)theHandlerPool).enableLogging(getLogger());
145         }
146         if (theHandlerPool instanceof Initializable) {
147             ((Initializable)theHandlerPool).initialize();
148         }
149
150         theWatchdogFactory = getWatchdogFactory();
151     }
152
153     /**
154      * @see org.apache.james.core.AbstractJamesService#getDefaultPort()
155      */

156      protected int getDefaultPort() {
157         return 119;
158      }
159
160     /**
161      * @see org.apache.james.core.AbstractJamesService#getServiceType()
162      */

163     public String JavaDoc getServiceType() {
164         return "NNTP Service";
165     }
166
167     /**
168      * @see org.apache.avalon.cornerstone.services.connection.AbstractHandlerFactory#newHandler()
169      */

170     protected ConnectionHandler newHandler()
171             throws Exception JavaDoc {
172         NNTPHandler theHandler = (NNTPHandler)theHandlerPool.get();
173
174         Watchdog theWatchdog = theWatchdogFactory.getWatchdog(theHandler.getWatchdogTarget());
175
176         theHandler.setConfigurationData(theConfigData);
177         theHandler.setWatchdog(theWatchdog);
178         return theHandler;
179     }
180
181     /**
182      * @see org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory#releaseConnectionHandler(ConnectionHandler)
183      */

184     public void releaseConnectionHandler( ConnectionHandler connectionHandler ) {
185         if (!(connectionHandler instanceof NNTPHandler)) {
186             throw new IllegalArgumentException JavaDoc("Attempted to return non-NNTPHandler to pool.");
187         }
188         theHandlerPool.put((Poolable)connectionHandler);
189     }
190
191     /**
192      * The factory for producing handlers.
193      */

194     private static class NNTPHandlerFactory
195         implements ObjectFactory {
196
197         /**
198          * @see org.apache.avalon.excalibur.pool.ObjectFactory#newInstance()
199          */

200         public Object JavaDoc newInstance() throws Exception JavaDoc {
201             return new NNTPHandler();
202         }
203
204         /**
205          * @see org.apache.avalon.excalibur.pool.ObjectFactory#getCreatedClass()
206          */

207         public Class JavaDoc getCreatedClass() {
208             return NNTPHandler.class;
209         }
210
211         /**
212          * @see org.apache.avalon.excalibur.pool.ObjectFactory#decommision(Object)
213          */

214         public void decommission( Object JavaDoc object ) throws Exception JavaDoc {
215             return;
216         }
217     }
218
219     /**
220      * A class to provide NNTP handler configuration to the handlers
221      */

222     private class NNTPHandlerConfigurationDataImpl
223         implements NNTPHandlerConfigurationData {
224
225         /**
226          * @see org.apache.james.smtpserver.NNTPHandlerConfigurationData#getHelloName()
227          */

228         public String JavaDoc getHelloName() {
229             return NNTPServer.this.helloName;
230         }
231
232         /**
233          * @see org.apache.james.smtpserver.NNTPHandlerConfigurationData#isAuthRequired()
234          */

235         public boolean isAuthRequired() {
236             return NNTPServer.this.authRequired;
237         }
238
239         /**
240          * @see org.apache.james.smtpserver.NNTPHandlerConfigurationData#getUsersRepository()
241          */

242         public UsersRepository getUsersRepository() {
243             return NNTPServer.this.userRepository;
244         }
245
246         /**
247          * @see org.apache.james.smtpserver.NNTPHandlerConfigurationData#getNNTPRepository()
248          */

249         public NNTPRepository getNNTPRepository() {
250             return NNTPServer.this.repo;
251         }
252
253     }
254 }
255
Popular Tags