Author: hammant Date: Sun Aug 22 16:27:32 2004 New Revision: 36708 Added: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/AbstractConnectionManager.java avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/AvalonLoggerConnectionMonitor.java avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/CDIConnectionManager.java avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/CommonsLoggingConnectionMonitor.java avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/ConnectionMonitor.java avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/NullConnectionMonitor.java Modified: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/Connection.java avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/DefaultConnectionManager.java avalon/trunk/planet/cornerstone/index.xml Log: ConnectionManager can run ouside of A-F now
Added: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/AbstractConnectionManager.java ============================================================================== --- (empty file) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/AbstractConnectionManager.java Sun Aug 22 16:27:32 2004 @@ -0,0 +1,151 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation + * 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.avalon.cornerstone.blocks.connection; + +import java.net.ServerSocket; +import java.util.HashMap; +import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory; +import org.apache.avalon.cornerstone.services.connection.ConnectionManager; +import org.apache.avalon.cornerstone.services.threads.ThreadManager; +import org.apache.excalibur.thread.ThreadPool; + +/** + * This is the service through which ConnectionManagement occurs. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a> + */ +public class AbstractConnectionManager implements ConnectionManager +{ + private final HashMap m_connections = new HashMap(); + protected ThreadManager m_threadManager; + protected ConnectionMonitor monitor; + + + /** + * Start managing a connection. + * Management involves accepting connections and farming them out to threads + * from pool to be handled. + * + * @param name the name of connection + * @param socket the ServerSocket from which to + * @param handlerFactory the factory from which to aquire handlers + * @param threadPool the thread pool to use + * @exception Exception if an error occurs + */ + public synchronized void connect( String name, + ServerSocket socket, + ConnectionHandlerFactory handlerFactory, + ThreadPool threadPool ) + throws Exception + { + if( null != m_connections.get( name ) ) + { + final String message = "Connection already exists with name " + name; + throw new IllegalArgumentException( message ); + } + + //Make sure timeout is specified for socket. + if( 0 == socket.getSoTimeout() ) + { + socket.setSoTimeout( 500 ); + } + + final Connection runner = + new Connection( socket, handlerFactory, threadPool, monitor ); + m_connections.put( name, runner ); + threadPool.execute( runner ); + } + + /** + * Start managing a connection. + * This is similar to other connect method except that it uses default thread pool. + * + * @param name the name of connection + * @param socket the ServerSocket from which to + * @param handlerFactory the factory from which to aquire handlers + * @exception Exception if an error occurs + */ + public void connect( String name, + ServerSocket socket, + ConnectionHandlerFactory handlerFactory ) + throws Exception + { + connect( name, socket, handlerFactory, m_threadManager.getDefaultThreadPool() ); + } + + /** + * This shuts down all handlers and socket, waiting for each to gracefully shutdown. + * + * @param name the name of connection + * @exception Exception if an error occurs + */ + public void disconnect( final String name ) + throws Exception + { + disconnect( name, false ); + } + + /** + * This shuts down all handlers and socket. + * If tearDown is true then it will forcefully shutdown all connections and try + * to return as soon as possible. Otherwise it will behave the same as + * void disconnect( String name ); + * + * @param name the name of connection + * @param tearDown if true will forcefully tear down all handlers + * @exception Exception if an error occurs + */ + public synchronized void disconnect( final String name, final boolean tearDown ) + throws Exception + { + final Connection connection = (Connection)m_connections.remove( name ); + + if( connection != null ) + { + //TODO: Stop ignoring tearDown + connection.dispose(); + } + else + { + final String error = + "Invalid request for the disconnection of an unrecognized connection name: " + + name; + throw new IllegalArgumentException( error ); + } + } + public void dispose() + { + if( monitor.isDebugEnabled(this.getClass()) ) + { + monitor.debugMessage(this.getClass(), "disposal" ); + } + final String[] names = (String[])m_connections.keySet().toArray( new String[ 0 ] ); + for( int i = 0; i < names.length; i++ ) + { + try + { + disconnect( names[ i ] ); + } + catch( final Exception e ) + { + final String message = "Error disconnecting " + names[ i ]; + monitor.unexpectedException(this.getClass(), message, e ); + } + } + } +} Added: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/AvalonLoggerConnectionMonitor.java ============================================================================== --- (empty file) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/AvalonLoggerConnectionMonitor.java Sun Aug 22 16:27:32 2004 @@ -0,0 +1,33 @@ +package org.apache.avalon.cornerstone.blocks.connection; + +import org.apache.avalon.framework.logger.AbstractLogEnabled; + +import java.io.IOException; + +/** + * @author Paul Hammant + * @version $Revision: 1.8 $ + */ +public class AvalonLoggerConnectionMonitor extends AbstractLogEnabled implements ConnectionMonitor { + + public void acceptingConnectionException(Class clazz, String message, IOException ioe) { + getLogger().error(message, ioe); + } + + public void unexpectedException(Class clazz, String message, Exception e) { + getLogger().error(message, e); + + } + + public void shutdownSocketWarning(Class clazz, String message, IOException ioe) { + getLogger().warn(message, ioe); + } + + public void debugMessage(Class clazz, String message) { + getLogger().debug(message); + } + + public boolean isDebugEnabled(Class clazz) { + return getLogger().isDebugEnabled(); + } +} Added: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/CDIConnectionManager.java ============================================================================== --- (empty file) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/CDIConnectionManager.java Sun Aug 22 16:27:32 2004 @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation + * 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.avalon.cornerstone.blocks.connection; + +import org.apache.avalon.cornerstone.services.connection.ConnectionManager; +import org.apache.avalon.cornerstone.services.threads.ThreadManager; + +/** + * This is the service through which ConnectionManagement occurs. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a> + */ +public class CDIConnectionManager extends AbstractConnectionManager implements ConnectionManager +{ + + public CDIConnectionManager(ThreadManager threadManager, ConnectionMonitor monitor) + { + super.monitor = monitor; + super.m_threadManager = threadManager; + } + +} Added: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/CommonsLoggingConnectionMonitor.java ============================================================================== --- (empty file) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/CommonsLoggingConnectionMonitor.java Sun Aug 22 16:27:32 2004 @@ -0,0 +1,57 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation + * 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.avalon.cornerstone.blocks.connection; + +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; + +/** + * A Commons Logging implementation of the ConnectionMonitor. Not very IoC, but then, nor is CommonsLogging. + * + * @author Paul Hammant + * @version $Revision: 1.8 $ + */ +public class CommonsLoggingConnectionMonitor implements ConnectionMonitor + { + + public void acceptingConnectionException(Class clazz, String message, IOException ioe) + { + LogFactory.getLog(clazz).error(message, ioe); + } + + public void unexpectedException(Class clazz, String message, Exception e) + { + LogFactory.getLog(clazz).error(message, e); + } + + public void shutdownSocketWarning(Class clazz, String message, IOException ioe) + { + LogFactory.getLog(clazz).error(message, ioe); + } + + public void debugMessage(Class clazz, String message) + { + LogFactory.getLog(clazz).error(message); + } + + public boolean isDebugEnabled(Class clazz) + { + return LogFactory.getLog(clazz).isDebugEnabled(); + } +} Modified: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/Connection.java ============================================================================== --- avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/Connection.java (original) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/Connection.java Sun Aug 22 16:27:32 2004 @@ -29,7 +29,6 @@ import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory; import org.apache.excalibur.thread.ThreadPool; -import org.apache.avalon.framework.logger.AbstractLogEnabled; /** * Support class for the DefaultConnectionManager. @@ -38,7 +37,6 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a> */ class Connection - extends AbstractLogEnabled implements Runnable { private final ServerSocket m_serverSocket; @@ -48,16 +46,20 @@ //Need to synchronize access to thread object private Thread m_thread; + protected ConnectionMonitor monitor; public Connection( final ServerSocket serverSocket, final ConnectionHandlerFactory handlerFactory, - final ThreadPool threadPool ) + final ThreadPool threadPool, + final ConnectionMonitor monitor) { m_serverSocket = serverSocket; m_handlerFactory = handlerFactory; m_threadPool = threadPool; + this.monitor = monitor; } + public void dispose() throws Exception { @@ -102,8 +104,7 @@ { final Socket socket = m_serverSocket.accept(); final ConnectionRunner runner = - new ConnectionRunner( socket, m_runners, m_handlerFactory ); - setupLogger( runner ); + new ConnectionRunner( socket, m_runners, m_handlerFactory, monitor ); m_threadPool.execute( runner ); } catch( final InterruptedIOException iioe ) @@ -113,12 +114,12 @@ catch( final IOException ioe ) { final String message = "Exception accepting connection"; - getLogger().error( message, ioe ); + monitor.acceptingConnectionException(this.getClass(), message, ioe ); } catch( final Exception e ) { final String message = "Exception executing runner"; - getLogger().error( message, e ); + monitor.unexpectedException(this.getClass(), message, e ); } } @@ -131,22 +132,24 @@ } class ConnectionRunner - extends AbstractLogEnabled implements Runnable { private Socket m_socket; private Thread m_thread; private List m_runners; private ConnectionHandlerFactory m_handlerFactory; + private ConnectionMonitor monitor; private boolean m_finished; ConnectionRunner( final Socket socket, final List runners, - final ConnectionHandlerFactory handlerFactory ) + final ConnectionHandlerFactory handlerFactory, + final ConnectionMonitor monitor) { m_socket = socket; m_runners = runners; m_handlerFactory = handlerFactory; + this.monitor = monitor; } public void dispose() @@ -197,7 +200,7 @@ catch( final Exception e ) { final String message = "Error handling connection"; - getLogger().warn( message, e ); + monitor.unexpectedException(this.getClass(), message, e ); } if( null != handler ) @@ -227,13 +230,13 @@ */ private void debugBanner( final boolean starting ) { - if( getLogger().isDebugEnabled() ) + if( monitor.isDebugEnabled(this.getClass()) ) { final String prefix = ( starting ) ? "Starting" : "Ending"; final String message = prefix + " connection on " + m_socket.getInetAddress().getHostAddress(); - getLogger().debug( message ); + monitor.debugMessage(this.getClass(), message ); } } @@ -249,7 +252,7 @@ catch( final IOException ioe ) { final String message = "Error shutting down connection"; - getLogger().warn( message, ioe ); + monitor.shutdownSocketWarning(this.getClass(), message, ioe ); } } } Added: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/ConnectionMonitor.java ============================================================================== --- (empty file) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/ConnectionMonitor.java Sun Aug 22 16:27:32 2004 @@ -0,0 +1,38 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation + * 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.avalon.cornerstone.blocks.connection; + +import java.io.IOException; + +/** + * @author Paul Hammant + * @version $Revision: 1.8 $ + */ +public interface ConnectionMonitor { + + void acceptingConnectionException(Class clazz, String message, IOException ioe); + + void unexpectedException(Class clazz, String message, Exception e); + + void shutdownSocketWarning(Class clazz, String message, IOException ioe); + + void debugMessage(Class clazz, String message); + + boolean isDebugEnabled(Class clazz); + +} Modified: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/DefaultConnectionManager.java ============================================================================== --- avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/DefaultConnectionManager.java (original) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/DefaultConnectionManager.java Sun Aug 22 16:27:32 2004 @@ -1,4 +1,4 @@ -/* +/* * Copyright 1999-2004 The Apache Software Foundation * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,14 +17,11 @@ package org.apache.avalon.cornerstone.blocks.connection; -import java.net.ServerSocket; -import java.util.HashMap; -import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory; import org.apache.avalon.cornerstone.services.connection.ConnectionManager; import org.apache.avalon.cornerstone.services.threads.ThreadManager; -import org.apache.excalibur.thread.ThreadPool; import org.apache.avalon.framework.activity.Disposable; -import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.avalon.framework.logger.LogEnabled; +import org.apache.avalon.framework.logger.Logger; import org.apache.avalon.framework.service.ServiceException; import org.apache.avalon.framework.service.ServiceManager; import org.apache.avalon.framework.service.Serviceable; @@ -37,11 +34,15 @@ * @avalon.service type="org.apache.avalon.cornerstone.services.connection.ConnectionManager" */ public class DefaultConnectionManager - extends AbstractLogEnabled - implements ConnectionManager, Serviceable, Disposable + extends AbstractConnectionManager + implements ConnectionManager, Serviceable, Disposable, LogEnabled { - private final HashMap m_connections = new HashMap(); - private ThreadManager m_threadManager; + + public void enableLogging(Logger logger) { + AvalonLoggerConnectionMonitor avalonLoggerConnectionMonitor = new AvalonLoggerConnectionMonitor(); + avalonLoggerConnectionMonitor.enableLogging(logger); + monitor = avalonLoggerConnectionMonitor; + } /** * @avalon.dependency type="org.apache.avalon.cornerstone.services.threads.ThreadManager" @@ -52,118 +53,5 @@ m_threadManager = (ThreadManager)serviceManager.lookup( ThreadManager.ROLE ); } - public void dispose() - { - if( getLogger().isDebugEnabled() ) - { - getLogger().debug( "disposal" ); - } - final String[] names = (String[])m_connections.keySet().toArray( new String[ 0 ] ); - for( int i = 0; i < names.length; i++ ) - { - try - { - disconnect( names[ i ] ); - } - catch( final Exception e ) - { - final String message = "Error disconnecting " + names[ i ]; - getLogger().warn( message, e ); - } - } - } - - /** - * Start managing a connection. - * Management involves accepting connections and farming them out to threads - * from pool to be handled. - * - * @param name the name of connection - * @param socket the ServerSocket from which to - * @param handlerFactory the factory from which to aquire handlers - * @param threadPool the thread pool to use - * @exception Exception if an error occurs - */ - public synchronized void connect( String name, - ServerSocket socket, - ConnectionHandlerFactory handlerFactory, - ThreadPool threadPool ) - throws Exception - { - if( null != m_connections.get( name ) ) - { - final String message = "Connection already exists with name " + name; - throw new IllegalArgumentException( message ); - } - - //Make sure timeout is specified for socket. - if( 0 == socket.getSoTimeout() ) - { - socket.setSoTimeout( 500 ); - } - - final Connection runner = - new Connection( socket, handlerFactory, threadPool ); - setupLogger( runner ); - m_connections.put( name, runner ); - threadPool.execute( runner ); - } - - /** - * Start managing a connection. - * This is similar to other connect method except that it uses default thread pool. - * - * @param name the name of connection - * @param socket the ServerSocket from which to - * @param handlerFactory the factory from which to aquire handlers - * @exception Exception if an error occurs - */ - public void connect( String name, - ServerSocket socket, - ConnectionHandlerFactory handlerFactory ) - throws Exception - { - connect( name, socket, handlerFactory, m_threadManager.getDefaultThreadPool() ); - } - - /** - * This shuts down all handlers and socket, waiting for each to gracefully shutdown. - * - * @param name the name of connection - * @exception Exception if an error occurs - */ - public void disconnect( final String name ) - throws Exception - { - disconnect( name, false ); - } - - /** - * This shuts down all handlers and socket. - * If tearDown is true then it will forcefully shutdown all connections and try - * to return as soon as possible. Otherwise it will behave the same as - * void disconnect( String name ); - * - * @param name the name of connection - * @param tearDown if true will forcefully tear down all handlers - * @exception Exception if an error occurs - */ - public synchronized void disconnect( final String name, final boolean tearDown ) - throws Exception - { - final Connection connection = (Connection)m_connections.remove( name ); - if( connection != null ) - { - //TODO: Stop ignoring tearDown - connection.dispose(); - } - else - { - final String error = - "Invalid request for the disconnection of an unrecognized connection name: " - + name; - throw new IllegalArgumentException( error ); - } - } } Added: avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/NullConnectionMonitor.java ============================================================================== --- (empty file) +++ avalon/trunk/planet/cornerstone/connection/impl/src/main/org/apache/avalon/cornerstone/blocks/connection/NullConnectionMonitor.java Sun Aug 22 16:27:32 2004 @@ -0,0 +1,50 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation + * 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.avalon.cornerstone.blocks.connection; + +import java.io.IOException; + +/** + * A ConnectionMonitor made with accordance to the NullObjectPattern. Use with caution. + * @author Paul Hammant + * @version $Revision: 1.8 $ + */ +public class NullConnectionMonitor implements ConnectionMonitor +{ + + public void acceptingConnectionException(Class clazz, String message, IOException ioe) + { + } + + public void unexpectedException(Class clazz, String message, Exception e) + { + } + + public void shutdownSocketWarning(Class clazz, String message, IOException ioe) + { + } + + public void debugMessage(Class clazz, String message) + { + } + + public boolean isDebugEnabled(Class clazz) + { + return false; + } +} Modified: avalon/trunk/planet/cornerstone/index.xml ============================================================================== --- avalon/trunk/planet/cornerstone/index.xml (original) +++ avalon/trunk/planet/cornerstone/index.xml Sun Aug 22 16:27:32 2004 @@ -15,6 +15,15 @@ <resource> <info> + <group>commons-logging</group> + <name>commons-logging</name> + <version>1.0.4</version> + </info> + </resource> + + + <resource> + <info> <group>concurrent</group> <name>concurrent</name> <version>1.3.1</version> @@ -174,6 +183,7 @@ <include key="cornerstone-connection-api"/> <include key="cornerstone-threads-impl"/> <include key="avalon-framework-impl"/> + <include key="commons-logging"/> </dependencies> <plugins> <include key="avalon-meta-tools"/> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]