KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
21 import java.io.InterruptedIOException JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.avalon.cornerstone.services.connection.ConnectionHandler;
29 import org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory;
30
31 import org.apache.excalibur.thread.ThreadPool;
32 import org.apache.avalon.framework.logger.AbstractLogEnabled;
33
34 /**
35  * Support class for the DefaultConnectionManager.
36  * This manages an individual ServerSocket.
37  *
38  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
39  */

40 class Connection
41     extends AbstractLogEnabled
42     implements Runnable JavaDoc
43 {
44     private final ServerSocket JavaDoc m_serverSocket;
45     private final ConnectionHandlerFactory m_handlerFactory;
46     private final ThreadPool m_threadPool;
47     private final Vector JavaDoc m_runners = new Vector JavaDoc();
48
49     //Need to synchronize access to thread object
50
private Thread JavaDoc m_thread;
51
52     public Connection( final ServerSocket JavaDoc serverSocket,
53                        final ConnectionHandlerFactory handlerFactory,
54                        final ThreadPool threadPool )
55     {
56         m_serverSocket = serverSocket;
57         m_handlerFactory = handlerFactory;
58         m_threadPool = threadPool;
59     }
60
61     public void dispose()
62         throws Exception JavaDoc
63     {
64         synchronized( this )
65         {
66             if( null != m_thread )
67             {
68                 final Thread JavaDoc thread = m_thread;
69                 m_thread = null;
70                 thread.interrupt();
71
72                 //Can not join as threads are part of pool
73
//and will never finish
74
//m_thread.join();
75

76                 wait( /*1000*/ );
77             }
78         }
79
80         final Iterator JavaDoc runners = m_runners.iterator();
81         while( runners.hasNext() )
82         {
83             final ConnectionRunner runner = (ConnectionRunner)runners.next();
84             runner.dispose();
85         }
86
87         m_runners.clear();
88     }
89
90     public void run()
91     {
92         m_thread = Thread.currentThread();
93
94         while( null != m_thread && !Thread.interrupted() )
95         {
96             //synchronized( this )
97
//{
98
//if( null == m_thread ) break;
99
//}
100

101             try
102             {
103                 final Socket JavaDoc socket = m_serverSocket.accept();
104                 final ConnectionRunner runner =
105                     new ConnectionRunner( socket, m_runners, m_handlerFactory );
106                 setupLogger( runner );
107                 m_threadPool.execute( runner );
108             }
109             catch( final InterruptedIOException JavaDoc iioe )
110             {
111                 //Consume exception
112
}
113             catch( final IOException JavaDoc ioe )
114             {
115                 final String JavaDoc message = "Exception accepting connection";
116                 getLogger().error( message, ioe );
117             }
118             catch( final Exception JavaDoc e )
119             {
120                 final String JavaDoc message = "Exception executing runner";
121                 getLogger().error( message, e );
122             }
123         }
124
125         synchronized( this )
126         {
127             notifyAll();
128             m_thread = null;
129         }
130     }
131 }
132
133 class ConnectionRunner
134     extends AbstractLogEnabled
135     implements Runnable JavaDoc
136 {
137     private Socket JavaDoc m_socket;
138     private Thread JavaDoc m_thread;
139     private List JavaDoc m_runners;
140     private ConnectionHandlerFactory m_handlerFactory;
141     private boolean m_finished;
142
143     ConnectionRunner( final Socket JavaDoc socket,
144                       final List JavaDoc runners,
145                       final ConnectionHandlerFactory handlerFactory )
146     {
147         m_socket = socket;
148         m_runners = runners;
149         m_handlerFactory = handlerFactory;
150     }
151
152     public void dispose()
153         throws Exception JavaDoc
154     {
155         synchronized( this )
156         {
157             m_finished = true;
158             if( null != m_thread )
159             {
160                 m_thread.interrupt();
161                 m_thread = null;
162                 //Can not join as threads are part of pool
163
//and will never finish
164
//m_thread.join();
165

166                 wait( /*1000*/ );
167             }
168         }
169     }
170
171     public void run()
172     {
173         //Synchronized section to guard against
174
//dispose() being called before thread is
175
//run and reaches next section
176
synchronized( this )
177         {
178             if( m_finished )
179             {
180                 shutdownSocket();
181                 return;
182             }
183             m_thread = Thread.currentThread();
184             m_runners.add( this );
185         }
186
187         ConnectionHandler handler = null;
188         try
189         {
190             debugBanner( true );
191
192             handler = m_handlerFactory.createConnectionHandler();
193             handler.handleConnection( m_socket );
194
195             debugBanner( false );
196         }
197         catch( final Exception JavaDoc e )
198         {
199             final String JavaDoc message = "Error handling connection";
200             getLogger().warn( message, e );
201         }
202
203         if( null != handler )
204         {
205             m_handlerFactory.releaseConnectionHandler( handler );
206         }
207
208         shutdownSocket();
209
210         //Synchronized section to make sure that thread
211
//in dispose() will not hang due to race conditions
212
synchronized( this )
213         {
214             m_thread = null;
215             m_runners.remove( this );
216
217             notifyAll();
218         }
219
220     }
221
222     /**
223      * Print out debug banner indicating that handling of a connection
224      * is starting or ending.
225      *
226      * @param starting true if starting, false othrewise
227      */

228     private void debugBanner( final boolean starting )
229     {
230         if( getLogger().isDebugEnabled() )
231         {
232             final String JavaDoc prefix = ( starting ) ? "Starting" : "Ending";
233             final String JavaDoc message =
234                 prefix + " connection on " +
235                 m_socket.getInetAddress().getHostAddress();
236             getLogger().debug( message );
237         }
238     }
239
240     /**
241      * Utility method for shutting down associated socket.
242      */

243     private void shutdownSocket()
244     {
245         try
246         {
247             m_socket.close();
248         }
249         catch( final IOException JavaDoc ioe )
250         {
251             final String JavaDoc message = "Error shutting down connection";
252             getLogger().warn( message, ioe );
253         }
254     }
255 }
256
Popular Tags