KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > connection > DefaultConnectionManager


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

17
18 package org.apache.avalon.cornerstone.blocks.connection;
19
20 import java.net.ServerSocket JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory;
23 import org.apache.avalon.cornerstone.services.connection.ConnectionManager;
24 import org.apache.avalon.cornerstone.services.threads.ThreadManager;
25 import org.apache.excalibur.thread.ThreadPool;
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.Serviceable;
31
32 /**
33  * This is the service through which ConnectionManagement occurs.
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @avalon.component name="connection-manager" lifestyle="singleton"
37  * @avalon.service type="org.apache.avalon.cornerstone.services.connection.ConnectionManager"
38  */

39 public class DefaultConnectionManager
40     extends AbstractLogEnabled
41     implements ConnectionManager, Serviceable, Disposable
42 {
43     private final HashMap JavaDoc m_connections = new HashMap JavaDoc();
44     private ThreadManager m_threadManager;
45
46    /**
47     * @avalon.dependency type="org.apache.avalon.cornerstone.services.threads.ThreadManager"
48     */

49     public void service( final ServiceManager serviceManager )
50         throws ServiceException
51     {
52         m_threadManager = (ThreadManager)serviceManager.lookup( ThreadManager.ROLE );
53     }
54
55     public void dispose()
56     {
57         if( getLogger().isDebugEnabled() )
58         {
59             getLogger().debug( "disposal" );
60         }
61         final String JavaDoc[] names = (String JavaDoc[])m_connections.keySet().toArray( new String JavaDoc[ 0 ] );
62         for( int i = 0; i < names.length; i++ )
63         {
64             try
65             {
66                 disconnect( names[ i ] );
67             }
68             catch( final Exception JavaDoc e )
69             {
70                 final String JavaDoc message = "Error disconnecting " + names[ i ];
71                 getLogger().warn( message, e );
72             }
73         }
74     }
75
76     /**
77      * Start managing a connection.
78      * Management involves accepting connections and farming them out to threads
79      * from pool to be handled.
80      *
81      * @param name the name of connection
82      * @param socket the ServerSocket from which to
83      * @param handlerFactory the factory from which to aquire handlers
84      * @param threadPool the thread pool to use
85      * @exception Exception if an error occurs
86      */

87     public synchronized void connect( String JavaDoc name,
88                                       ServerSocket JavaDoc socket,
89                                       ConnectionHandlerFactory handlerFactory,
90                                       ThreadPool threadPool )
91         throws Exception JavaDoc
92     {
93         if( null != m_connections.get( name ) )
94         {
95             final String JavaDoc message = "Connection already exists with name " + name;
96             throw new IllegalArgumentException JavaDoc( message );
97         }
98
99         //Make sure timeout is specified for socket.
100
if( 0 == socket.getSoTimeout() )
101         {
102             socket.setSoTimeout( 500 );
103         }
104
105         final Connection runner =
106             new Connection( socket, handlerFactory, threadPool );
107         setupLogger( runner );
108         m_connections.put( name, runner );
109         threadPool.execute( runner );
110     }
111
112     /**
113      * Start managing a connection.
114      * This is similar to other connect method except that it uses default thread pool.
115      *
116      * @param name the name of connection
117      * @param socket the ServerSocket from which to
118      * @param handlerFactory the factory from which to aquire handlers
119      * @exception Exception if an error occurs
120      */

121     public void connect( String JavaDoc name,
122                          ServerSocket JavaDoc socket,
123                          ConnectionHandlerFactory handlerFactory )
124         throws Exception JavaDoc
125     {
126         connect( name, socket, handlerFactory, m_threadManager.getDefaultThreadPool() );
127     }
128
129     /**
130      * This shuts down all handlers and socket, waiting for each to gracefully shutdown.
131      *
132      * @param name the name of connection
133      * @exception Exception if an error occurs
134      */

135     public void disconnect( final String JavaDoc name )
136         throws Exception JavaDoc
137     {
138         disconnect( name, false );
139     }
140
141     /**
142      * This shuts down all handlers and socket.
143      * If tearDown is true then it will forcefully shutdown all connections and try
144      * to return as soon as possible. Otherwise it will behave the same as
145      * void disconnect( String name );
146      *
147      * @param name the name of connection
148      * @param tearDown if true will forcefully tear down all handlers
149      * @exception Exception if an error occurs
150      */

151     public synchronized void disconnect( final String JavaDoc name, final boolean tearDown )
152         throws Exception JavaDoc
153     {
154         final Connection connection = (Connection)m_connections.remove( name );
155
156         if( connection != null )
157         {
158             //TODO: Stop ignoring tearDown
159
connection.dispose();
160         }
161         else
162         {
163             final String JavaDoc error =
164                 "Invalid request for the disconnection of an unrecognized connection name: "
165                 + name;
166             throw new IllegalArgumentException JavaDoc( error );
167         }
168     }
169 }
170
Popular Tags