Package org.apache.james.core

Source Code of org.apache.james.core.AbstractJamesService

/* ====================================================================
* 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.core;

import java.io.*;
import java.net.*;

import org.apache.avalon.framework.logger.*;
import org.apache.avalon.framework.component.*;
import org.apache.avalon.framework.configuration.*;
import org.apache.avalon.framework.activity.*;

import org.apache.avalon.excalibur.thread.ThreadPool;
import org.apache.avalon.cornerstone.services.threads.ThreadManager;

import org.apache.avalon.cornerstone.services.connection.AbstractHandlerFactory;
import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory;
import org.apache.avalon.cornerstone.services.connection.ConnectionManager;
import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory;
import org.apache.avalon.cornerstone.services.sockets.SocketManager;

import org.apache.james.util.connection.SimpleConnectionManager;
import org.apache.james.util.watchdog.ThreadPerWatchdogFactory;
import org.apache.james.util.watchdog.Watchdog;
import org.apache.james.util.watchdog.WatchdogFactory;

/**
* Server which creates connection handlers. All new James service must
* inherit from this abstract implementation.
*
* @author <a href="mailto:myfam@surfeu.fi">Andrei Ivanov</a>
* @author <a href="farsight@alum.mit.edu">Peter M. Goldstein</a>
*/
public abstract class AbstractJamesService extends AbstractHandlerFactory
    implements Component, Composable, Configurable,
               Disposable, Initializable, ConnectionHandlerFactory {

    /**
     * The default value for the connection timeout.
     */
    protected static final int DEFAULT_TIMEOUT = 5* 60 * 1000;

    /**
     * The name of the parameter defining the connection timeout.
     */
    protected static final String TIMEOUT_NAME = "connectiontimeout";

    /**
     * The name of the parameter defining the service hello name.
     */
    public static final String HELLO_NAME = "helloName";

    /**
     * The ConnectionManager that spawns and manages service connections.
     */
    private ConnectionManager connectionManager;

    /**
     * The name of the thread group to be used by this service for
     * generating connections
     */
    protected String threadGroup;

    /**
     * The thread pool used by this service that holds the threads
     * that service the client connections.
     */
    protected ThreadPool threadPool = null;

    /**
     * The server socket type used to generate connections for this server.
     */
    protected String serverSocketType = "plain";

    /**
     * The port on which this service will be made available.
     */
    protected int port = -1;

    /**
     * Network interface to which the service will bind.  If not set,
     * the server binds to all available interfaces.
     */
    protected InetAddress bindTo = null;

    /*
     * The server socket associated with this service
     */
    protected ServerSocket serverSocket;

    /**
     * The name of the connection used by this service.  We need to
     * track this so we can tell the ConnectionManager which service
     * to disconnect upon shutdown.
     */
    protected String connectionName;

    /**
     * The maximum number of connections allowed for this service.
     */
    protected Integer connectionLimit;

    /**
     * The connection idle timeout.  Used primarily to prevent server
     * problems from hanging a connection.
     */
    protected int timeout;

    /**
     * The hello name for the service.
     */
    protected String helloName;

    /**
     * The component manager used by this service.
     */
    private ComponentManager compMgr;

    /**
     * Whether this service is enabled.
     */
    private volatile boolean enabled;

    /**
     * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
     */
    public void compose(ComponentManager comp) throws ComponentException {
        super.compose(comp);
        compMgr = comp;
        connectionManager = (ConnectionManager) compMgr.lookup(ConnectionManager.ROLE);
    }

    /**
     * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
     */
    public void configure(Configuration conf) throws ConfigurationException {
        enabled = conf.getAttributeAsBoolean("enabled", true);
        if (!enabled) {
          getLogger().info(getServiceType() + " disabled by configuration");
          return;
        }

        Configuration handlerConfiguration = conf.getChild("handler");

        // Send the handler subconfiguration to the super class.  This
        // ensures that the handler config is passed to the handlers.
        //
        // TODO: This should be rationalized.  The handler element of the
        //       server configuration doesn't really make a whole lot of
        //       sense.  We should modify the config to get rid of it.
        //       Keeping it for now to maintain backwards compatibility.
        super.configure(handlerConfiguration);

        port = conf.getChild("port").getValueAsInteger(getDefaultPort());

        Configuration serverSocketTypeConf = conf.getChild("serverSocketType", false);
        String confSocketType = null;
        if (serverSocketTypeConf != null ) {
            confSocketType = serverSocketTypeConf.getValue();
        }

        if (confSocketType == null) {
            // Only load the useTLS parameter if a specific socket type has not
            // been specified.  This maintains backwards compatibility while
            // allowing us to have more complex (i.e. multiple SSL configuration)
            // deployments
            final boolean useTLS = conf.getChild("useTLS").getValueAsBoolean(isDefaultTLSEnabled());
            if (useTLS) {
              serverSocketType = "ssl";
            }
        } else {
            serverSocketType = confSocketType;
        }

        StringBuffer infoBuffer;
        threadGroup = conf.getChild("threadGroup").getValue(null);
        if (threadGroup != null) {
            infoBuffer =
                new StringBuffer(64)
                        .append(getServiceType())
                        .append(" uses thread group: ")
                        .append(threadGroup);
            getLogger().info(infoBuffer.toString());
        }
        else {
            getLogger().info(getServiceType() + " uses default thread group.");
        }

        try {
            final String bindAddress = conf.getChild("bind").getValue(null);
            if( null != bindAddress ) {
                bindTo = InetAddress.getByName(bindAddress);
                infoBuffer =
                    new StringBuffer(64)
                            .append(getServiceType())
                            .append(" bound to: ")
                            .append(bindTo);
                getLogger().info(infoBuffer.toString());
            }
        }
        catch( final UnknownHostException unhe ) {
            throw new ConfigurationException( "Malformed bind parameter in configuration of service " + getServiceType(), unhe );
        }

        String hostName = null;
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ue) {
            hostName = "localhost";
        }

        infoBuffer =
            new StringBuffer(64)
                    .append(getServiceType())
                    .append(" is running on: ")
                    .append(hostName);
        getLogger().info(infoBuffer.toString());

        Configuration helloConf = handlerConfiguration.getChild(HELLO_NAME);
        boolean autodetect = helloConf.getAttributeAsBoolean("autodetect", true);
        if (autodetect) {
            helloName = hostName;
        } else {
            helloName = helloConf.getValue("localhost");
        }
        infoBuffer =
            new StringBuffer(64)
                    .append(getServiceType())
                    .append(" handler hello name is: ")
                    .append(helloName);
        getLogger().info(infoBuffer.toString());

        timeout = handlerConfiguration.getChild(TIMEOUT_NAME).getValueAsInteger(DEFAULT_TIMEOUT);

        infoBuffer =
            new StringBuffer(64)
                    .append(getServiceType())
                    .append(" handler connection timeout is: ")
                    .append(timeout);
        getLogger().info(infoBuffer.toString());

        final String location = "generated:" + getServiceType();

        if (connectionManager instanceof SimpleConnectionManager) {
            String connectionLimitString = conf.getChild("connectionLimit").getValue(null);
            if (connectionLimitString != null) {
                try {
                    connectionLimit = new Integer(connectionLimitString);
                } catch (NumberFormatException nfe) {
                    getLogger().error("Connection limit value is not properly formatted.", nfe);
                }
                if (connectionLimit.intValue() < 0) {
                    getLogger().error("Connection limit value cannot be less than zero.");
                    throw new ConfigurationException("Connection limit value cannot be less than zero.");
                }
            } else {
                connectionLimit = new Integer(((SimpleConnectionManager)connectionManager).getMaximumNumberOfOpenConnections());
            }
            infoBuffer = new StringBuffer(128)
                .append(getServiceType())
                .append(" will allow a maximum of ")
                .append(connectionLimit.intValue())
                .append(" connections.");
            getLogger().info(infoBuffer.toString());
        }
    }

    /**
     * @see org.apache.avalon.framework.activity.Initializable#initialize()
     */
    public void initialize() throws Exception {
        if (!isEnabled()) {
            getLogger().info(getServiceType() + " Disabled");
            System.out.println(getServiceType() + " Disabled");
            return;
        }
        getLogger().debug(getServiceType() + " init...");

        SocketManager socketManager = (SocketManager) compMgr.lookup(SocketManager.ROLE);

        ThreadManager threadManager = (ThreadManager) compMgr.lookup(ThreadManager.ROLE);

        if (threadGroup != null) {
            threadPool = threadManager.getThreadPool(threadGroup);
        } else {
            threadPool = threadManager.getDefaultThreadPool();
        }

        ServerSocketFactory factory = socketManager.getServerSocketFactory(serverSocketType);
        ServerSocket serverSocket = factory.createServerSocket(port, 5, bindTo);
   
        if (null == connectionName) {
            final StringBuffer sb = new StringBuffer();
            sb.append(serverSocketType);
            sb.append(':');
            sb.append(port);
   
            if (null != bindTo) {
                sb.append('/');
                sb.append(bindTo);
            }
            connectionName = sb.toString();
        }

        if ((connectionLimit != null) &&
            (connectionManager instanceof SimpleConnectionManager)) {
            if (null != threadPool) {
                ((SimpleConnectionManager)connectionManager).connect(connectionName, serverSocket, this, threadPool, connectionLimit.intValue());
            }
            else {
                ((SimpleConnectionManager)connectionManager).connect(connectionName, serverSocket, this, connectionLimit.intValue()); // default pool
            }
        } else {
            if (null != threadPool) {
                connectionManager.connect(connectionName, serverSocket, this, threadPool);
            }
            else {
                connectionManager.connect(connectionName, serverSocket, this); // default pool
            }
        }

        getLogger().debug(getServiceType() + " ...init end");

        StringBuffer logBuffer =
            new StringBuffer(64)
                .append(getServiceType())
                .append(" started ")
                .append(connectionName);
        String logString = logBuffer.toString();
        System.out.println(logString);
        getLogger().info(logString);
    }

    /**
     * @see org.apache.avalon.framework.activity.Disposable#dispose()
     */
    public void dispose() {

        if (!isEnabled()) {
            return;
        }
        StringBuffer infoBuffer =
            new StringBuffer(64)
                    .append(getServiceType())
                    .append(" dispose... ")
                    .append(connectionName);
        getLogger().debug(infoBuffer.toString());

        try {
            connectionManager.disconnect(connectionName, true);
        } catch (final Exception e) {
            StringBuffer warnBuffer =
                new StringBuffer(64)
                        .append("Error disconnecting ")
                        .append(getServiceType())
                        .append(": ");
            getLogger().warn(warnBuffer.toString(), e);
        }

        compMgr = null;

        connectionManager = null;
        threadPool = null;

        // This is needed to make sure sockets are promptly closed on Windows 2000
        // TODO: Check this - shouldn't need to explicitly gc to force socket closure
        System.gc();
   
        getLogger().debug(getServiceType() + " ...dispose end");
    }

    /**
     * This constructs the WatchdogFactory that will be used to guard
     * against runaway or stuck behavior.  Should only be called once
     * by a subclass in its initialize() method.
     *
     * @return the WatchdogFactory to be employed by subclasses.
     */
    protected WatchdogFactory getWatchdogFactory() {
        WatchdogFactory theWatchdogFactory = null;
        theWatchdogFactory = new ThreadPerWatchdogFactory(threadPool, timeout);
        if (theWatchdogFactory instanceof LogEnabled) {
            ((LogEnabled)theWatchdogFactory).enableLogging(getLogger());
        }
        return theWatchdogFactory;
     }


    /**
     * Describes whether this service is enabled by configuration.
     *
     * @return is the service enabled.
     */
    protected final boolean isEnabled() {
        return enabled;
    }
    /**
     * Overide this method to create actual instance of connection handler.
     *
     * @return the new ConnectionHandler
     * @exception Exception if an error occurs
     */
    protected abstract ConnectionHandler newHandler()
        throws Exception;

    /**
     * Get the default port for this server type.
     *
     * It is strongly recommended that subclasses of this class
     * override this method to specify the default port for their
     * specific server type.
     *
     * @return the default port
     */
     protected int getDefaultPort() {
        return 0;
     }

    /**
     * Get whether TLS is enabled for this server's socket by default.
     *
     * @return the default port
     */
     protected boolean isDefaultTLSEnabled() {
        return false;
     }

    /**
     * This method returns the type of service provided by this server.
     * This should be invariant over the life of the class.
     *
     * Subclasses may override this implementation.  This implementation
     * parses the complete class name and returns the undecorated class
     * name.
     *
     * @return description of this server
     */
    public String getServiceType() {
        String name = getClass().getName();
        int p = name.lastIndexOf(".");
        if (p > 0 && p < name.length() - 2) {
            name = name.substring(p + 1);
        }
        return name;
    }
}
TOP

Related Classes of org.apache.james.core.AbstractJamesService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.