/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache", "Jakarta", "JAMES" and "Apache Software Foundation"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
package org.apache.james.remotemanager;
import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
import org.apache.avalon.excalibur.pool.DefaultPool;
import org.apache.avalon.excalibur.pool.HardResourceLimitingPool;
import org.apache.avalon.excalibur.pool.ObjectFactory;
import org.apache.avalon.excalibur.pool.Pool;
import org.apache.avalon.excalibur.pool.Poolable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.james.core.AbstractJamesService;
import org.apache.james.services.*;
import org.apache.james.util.watchdog.Watchdog;
import org.apache.james.util.watchdog.WatchdogFactory;
import org.apache.james.util.watchdog.WatchdogTarget;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
/**
* Provides a really rude network interface to administer James.
* Allow to add accounts.
* TODO: -improve protocol
* -add remove user
* -much more...
* @version 1.0.0, 24/04/1999
* @author Federico Barbieri <scoobie@pop.systemy.it>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
* @author Peter M. Goldstein <farsight@alum.mit.edu>
*/
public class RemoteManager
extends AbstractJamesService implements Component {
/**
* A HashMap of (user id, passwords) for James administrators
*/
private HashMap adminAccounts = new HashMap();
/**
* The UsersStore that contains all UsersRepositories managed by this RemoteManager
*/
private UsersStore usersStore;
/**
* The current UsersRepository being managed/viewed/modified
*/
private UsersRepository users;
/**
* The reference to the internal MailServer service
*/
private MailServer mailServer;
/**
* The pool used to provide RemoteManager Handler objects
*/
private Pool theHandlerPool = null;
/**
* The pool used to provide RemoteManager Handler objects
*/
private ObjectFactory theHandlerFactory = new RemoteManagerHandlerFactory();
/**
* The factory used to generate Watchdog objects
*/
private WatchdogFactory theWatchdogFactory;
/**
* The configuration data to be passed to the handler
*/
private RemoteManagerHandlerConfigurationData theConfigData
= new RemoteManagerHandlerConfigurationDataImpl();
/**
* @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
*/
public void compose( final ComponentManager componentManager )
throws ComponentException {
super.compose(componentManager);
mailServer = (MailServer)componentManager.
lookup( "org.apache.james.services.MailServer" );
usersStore = (UsersStore)componentManager.
lookup( "org.apache.james.services.UsersStore" );
users = usersStore.getRepository("LocalUsers");
if (users == null) {
throw new ComponentException("The user repository could not be found.");
}
}
/**
* @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
*/
public void configure( final Configuration configuration )
throws ConfigurationException {
super.configure(configuration);
if (isEnabled()) {
Configuration handlerConfiguration = configuration.getChild("handler");
Configuration admin = handlerConfiguration.getChild( "administrator_accounts" );
Configuration[] accounts = admin.getChildren( "account" );
for ( int i = 0; i < accounts.length; i++ ) {
adminAccounts.put( accounts[ i ].getAttribute( "login" ),
accounts[ i ].getAttribute( "password" ) );
}
}
}
/**
* @see org.apache.avalon.framework.activity.Initializable#initialize()
*/
public void initialize() throws Exception {
super.initialize();
if (!isEnabled()) {
return;
}
if (connectionLimit != null) {
theHandlerPool = new HardResourceLimitingPool(theHandlerFactory, 5, connectionLimit.intValue());
getLogger().debug("Using a bounded pool for RemoteManager handlers with upper limit " + connectionLimit.intValue());
} else {
// NOTE: The maximum here is not a real maximum. The handler pool will continue to
// provide handlers beyond this value.
theHandlerPool = new DefaultPool(theHandlerFactory, null, 5, 30);
getLogger().debug("Using an unbounded pool for RemoteManager handlers.");
}
if (theHandlerPool instanceof LogEnabled) {
((LogEnabled)theHandlerPool).enableLogging(getLogger());
}
if (theHandlerPool instanceof Initializable) {
((Initializable)theHandlerPool).initialize();
}
theWatchdogFactory = getWatchdogFactory();
}
/**
* @see org.apache.james.core.AbstractJamesService#getDefaultPort()
*/
protected int getDefaultPort() {
return 4555;
}
/**
* @see org.apache.james.core.AbstractJamesService#getServiceType()
*/
public String getServiceType() {
return "Remote Manager Service";
}
/**
* @see org.apache.avalon.cornerstone.services.connection.AbstractHandlerFactory#newHandler()
*/
protected ConnectionHandler newHandler()
throws Exception {
RemoteManagerHandler theHandler = (RemoteManagerHandler)theHandlerPool.get();
theHandler.enableLogging(getLogger());
Watchdog theWatchdog = theWatchdogFactory.getWatchdog(theHandler.getWatchdogTarget());
theHandler.setConfigurationData(theConfigData);
theHandler.setWatchdog(theWatchdog);
return theHandler;
}
/**
* @see org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory#releaseConnectionHandler(ConnectionHandler)
*/
public void releaseConnectionHandler( ConnectionHandler connectionHandler ) {
if (!(connectionHandler instanceof RemoteManagerHandler)) {
throw new IllegalArgumentException("Attempted to return non-RemoteManagerHandler to pool.");
}
theHandlerPool.put((Poolable)connectionHandler);
}
/**
* The factory for producing handlers.
*/
private static class RemoteManagerHandlerFactory
implements ObjectFactory {
/**
* @see org.apache.avalon.excalibur.pool.ObjectFactory#newInstance()
*/
public Object newInstance() throws Exception {
return new RemoteManagerHandler();
}
/**
* @see org.apache.avalon.excalibur.pool.ObjectFactory#getCreatedClass()
*/
public Class getCreatedClass() {
return RemoteManagerHandler.class;
}
/**
* @see org.apache.avalon.excalibur.pool.ObjectFactory#decommision(Object)
*/
public void decommission( Object object ) throws Exception {
return;
}
}
/**
* A class to provide RemoteManager handler configuration to the handlers
*/
private class RemoteManagerHandlerConfigurationDataImpl
implements RemoteManagerHandlerConfigurationData {
/**
* @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getHelloName()
*/
public String getHelloName() {
return RemoteManager.this.helloName;
}
/**
* @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getMailServer()
*/
public MailServer getMailServer() {
return RemoteManager.this.mailServer;
}
/**
* @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersRepository()
*/
public UsersRepository getUsersRepository() {
return RemoteManager.this.users;
}
/**
* @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getUsersStore()
*/
public UsersStore getUserStore() {
return RemoteManager.this.usersStore;
}
/**
* @see org.apache.james.remotemanager.RemoteManagerHandlerConfigurationData#getAdministrativeAccountData()
*/
public HashMap getAdministrativeAccountData() {
return RemoteManager.this.adminAccounts;
}
}
}