KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > giop > ServerGIOPConnection


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.orb.giop;
22
23 import org.apache.avalon.framework.configuration.*;
24 import org.apache.avalon.framework.logger.Logger;
25
26 import java.io.*;
27
28 import org.jacorb.orb.iiop.*;
29
30 /**
31  * @author Nicolas Noffke
32  * @version $Id: ServerGIOPConnection.java,v 1.15 2004/05/06 12:40:00 nicolas Exp $
33  */

34
35 public class ServerGIOPConnection
36     extends GIOPConnection
37 {
38     private static final byte[] CLOSE_CONNECTION_MESSAGE =
39     new byte[] {
40         // Byte casts are for JDK1.2 compatibility.
41
(byte )'G', (byte )'I', (byte )'O', (byte )'P', //magic start
42
1, //GIOP major
43
0, //GIOP minor
44
0, //endianess, big-endian
45
5, //message type, CloseConnection message
46
0, 0, 0, 0 // message size, 0 because CloseConnection has no body
47
};
48
49     private GIOPConnectionManager manager = null;
50     private Logger logger = null;
51     private boolean closeOnReadTimeout = false;
52     private boolean delayClose = false;
53
54     public ServerGIOPConnection( org.omg.ETF.Profile profile,
55                                  org.omg.ETF.Connection transport,
56                                  RequestListener request_listener,
57                                  ReplyListener reply_listener,
58                                  StatisticsProvider statistics_provider,
59                                  GIOPConnectionManager manager )
60     {
61         super( profile, transport, request_listener, reply_listener, statistics_provider );
62         this.manager = manager;
63     }
64
65
66
67     public void configure(Configuration configuration)
68         throws ConfigurationException
69     {
70         super.configure(configuration);
71         logger = ((org.jacorb.config.Configuration)configuration).getNamedLogger("jacorb.giop.conn");
72         delayClose =
73             configuration.getAttribute("jacorb.connection.delay_close","off").equals("on");
74     }
75
76
77     /*
78      * <code>tryClose</close> called by GIOPConnectionManager::createServerGIOPConnection
79      * if there are too many GIOP connections or if we have timed out with nothing pending.
80      */

81     boolean tryClose()
82     {
83         if( tryDiscard() )
84         {
85             sendCloseConnection();
86
87             closeOnReadTimeout = true;
88
89             if( connection_listener != null )
90             {
91                 connection_listener.connectionClosed();
92             }
93
94             return true;
95         }
96         else
97         {
98             return false;
99         }
100     }
101
102
103     /**
104      * Atomically try to set this connection into discarding mode, if
105      * it doesn't have any pending messages.
106      *
107      * @return true, if the connection has been idle and discarding
108      * has been set
109      */

110     private boolean tryDiscard()
111     {
112         if( ! hasPendingMessages() )
113         {
114             synchronized( pendingUndecidedSync )
115             {
116                 discard_messages = true;
117             }
118
119             return true;
120         }
121         else
122         {
123             return false;
124         }
125     }
126
127
128     /**
129      * <code>sendCloseConnection</code> sends a close connection message
130      * flushing the transport.
131      */

132     private void sendCloseConnection()
133     {
134         try
135         {
136             getWriteLock();
137
138             write( CLOSE_CONNECTION_MESSAGE,
139                    0,
140                    CLOSE_CONNECTION_MESSAGE.length );
141
142             transport.flush();
143
144             if (statistics_provider != null)
145             {
146                 statistics_provider.flushed();
147             }
148
149             if( delayClose && transport instanceof IIOPConnection )
150             {
151                 ((IIOPConnection)transport).turnOnFinalTimeout();
152             }
153             else
154             {
155                 // Set do_close to true so anything waiting in waitUntilConnection
156
// doesn't think there is a possibility of connecting. I think.
157
do_close = true;
158
159                 transport.close();
160             }
161         }
162         catch( org.omg.CORBA.COMM_FAILURE JavaDoc e )
163         {
164             if (logger.isErrorEnabled())
165                 logger.error("COMM_FAILURE" , e );
166         }
167         finally
168         {
169             releaseWriteLock();
170         }
171
172         if( manager != null )
173         {
174             manager.unregisterServerGIOPConnection( this );
175         }
176     }
177
178
179     protected void readTimedOut()
180     {
181         if( closeOnReadTimeout )
182         {
183             close();
184         }
185         else
186         {
187             /*
188              * If we don't have any more pending messages, we'll send a
189              * GIOP CloseConnection message (done by tryClose() ).
190              */

191             tryClose();
192         }
193     }
194
195     /**
196      * We're server side and can't reopen, therefore close completely
197      * if stream closed.
198      */

199     protected void streamClosed()
200     {
201         close();
202     }
203     
204     /**
205      * @overrides close in GIOPConnection
206      */

207     public void close()
208     {
209         super.close();
210         if( manager != null )
211         {
212             manager.unregisterServerGIOPConnection( this );
213         }
214     }
215
216 }// ServerGIOPConnection
217
Popular Tags