📄 abstractjamesservice.java
字号:
/*********************************************************************** * Copyright (c) 2000-2004 The Apache Software Foundation. * * All rights reserved. * * ------------------------------------------------------------------- * * Licensed under the Apache License, Version 2.0 (the "License"); you * * may not use this file except in compliance with the License. You * * may obtain a copy of the License at: * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * * implied. See the License for the specific language governing * * permissions and limitations under the License. * ***********************************************************************/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.sockets.ServerSocketFactory;import org.apache.avalon.cornerstone.services.sockets.SocketManager;import org.apache.james.services.JamesConnectionManager;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. * */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 default value for the connection backlog. */ protected static final int DEFAULT_BACKLOG = 5; /** * The name of the parameter defining the connection backlog. */ protected static final String BACKLOG_NAME = "connectionBacklog"; /** * 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 JamesConnectionManager 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 connection backlog. */ protected int backlog; /** * 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 = (JamesConnectionManager) compMgr.lookup(JamesConnectionManager.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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -