KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package org.apache.avalon.cornerstone.blocks.connection;
21
22 import java.net.ServerSocket JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory;
25 import org.apache.avalon.cornerstone.services.connection.ConnectionManager;
26 import org.apache.avalon.cornerstone.services.threads.ThreadManager;
27 import org.apache.excalibur.thread.ThreadPool;
28
29 /**
30  * This is the service through which ConnectionManagement occurs.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  */

34 public class AbstractConnectionManager implements ConnectionManager
35 {
36     private final HashMap JavaDoc m_connections = new HashMap JavaDoc();
37     protected ThreadManager m_threadManager;
38     protected ConnectionMonitor monitor;
39
40
41     /**
42      * Start managing a connection.
43      * Management involves accepting connections and farming them out to threads
44      * from pool to be handled.
45      *
46      * @param name the name of connection
47      * @param socket the ServerSocket from which to
48      * @param handlerFactory the factory from which to aquire handlers
49      * @param threadPool the thread pool to use
50      * @exception Exception if an error occurs
51      */

52     public synchronized void connect( String JavaDoc name,
53                                       ServerSocket JavaDoc socket,
54                                       ConnectionHandlerFactory handlerFactory,
55                                       ThreadPool threadPool )
56         throws Exception JavaDoc
57     {
58         if( null != m_connections.get( name ) )
59         {
60             final String JavaDoc message = "Connection already exists with name " + name;
61             throw new IllegalArgumentException JavaDoc( message );
62         }
63
64         //Make sure timeout is specified for socket.
65
if( 0 == socket.getSoTimeout() )
66         {
67             socket.setSoTimeout( 500 );
68         }
69
70         final Connection runner =
71             new Connection( socket, handlerFactory, threadPool, monitor );
72         m_connections.put( name, runner );
73         threadPool.execute( runner );
74     }
75
76     /**
77      * Start managing a connection.
78      * This is similar to other connect method except that it uses default thread pool.
79      *
80      * @param name the name of connection
81      * @param socket the ServerSocket from which to
82      * @param handlerFactory the factory from which to aquire handlers
83      * @exception Exception if an error occurs
84      */

85     public void connect( String JavaDoc name,
86                          ServerSocket JavaDoc socket,
87                          ConnectionHandlerFactory handlerFactory )
88         throws Exception JavaDoc
89     {
90         connect( name, socket, handlerFactory, m_threadManager.getDefaultThreadPool() );
91     }
92
93     /**
94      * This shuts down all handlers and socket, waiting for each to gracefully shutdown.
95      *
96      * @param name the name of connection
97      * @exception Exception if an error occurs
98      */

99     public void disconnect( final String JavaDoc name )
100         throws Exception JavaDoc
101     {
102         disconnect( name, false );
103     }
104
105     /**
106      * This shuts down all handlers and socket.
107      * If tearDown is true then it will forcefully shutdown all connections and try
108      * to return as soon as possible. Otherwise it will behave the same as
109      * void disconnect( String name );
110      *
111      * @param name the name of connection
112      * @param tearDown if true will forcefully tear down all handlers
113      * @exception Exception if an error occurs
114      */

115     public synchronized void disconnect( final String JavaDoc name, final boolean tearDown )
116         throws Exception JavaDoc
117     {
118         final Connection connection = (Connection)m_connections.remove( name );
119
120         if( connection != null )
121         {
122             //TODO: Stop ignoring tearDown
123
connection.dispose();
124         }
125         else
126         {
127             final String JavaDoc error =
128                 "Invalid request for the disconnection of an unrecognized connection name: "
129                 + name;
130             throw new IllegalArgumentException JavaDoc( error );
131         }
132     }
133     public void dispose()
134     {
135         if( monitor.isDebugEnabled(this.getClass()) )
136         {
137             monitor.debugMessage(this.getClass(), "disposal" );
138         }
139         final String JavaDoc[] names = (String JavaDoc[])m_connections.keySet().toArray( new String JavaDoc[ 0 ] );
140         for( int i = 0; i < names.length; i++ )
141         {
142             try
143             {
144                 disconnect( names[ i ] );
145             }
146             catch( final Exception JavaDoc e )
147             {
148                 final String JavaDoc message = "Error disconnecting " + names[ i ];
149                 monitor.unexpectedException(this.getClass(), message, e );
150             }
151         }
152     }
153 }
154
Popular Tags