KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jacorb.orb.giop;
21
22 import org.apache.avalon.framework.logger.Logger;
23 import org.apache.avalon.framework.configuration.*;
24
25 import java.util.*;
26
27 import org.jacorb.util.ObjectUtil;
28
29 /**
30  * @author Nicolas Noffke
31  * @version $Id: GIOPConnectionManager.java,v 1.10 2004/05/06 12:40:00 nicolas Exp $
32  */

33
34 public class GIOPConnectionManager
35     implements Configurable
36 {
37     /** the configuration object */
38     private org.jacorb.config.Configuration configuration = null;
39
40     /** configuration properties */
41     private Logger logger = null;
42
43     //private List client_giop_connections = null;
44
private List server_giop_connections = null;
45     private int max_server_giop_connections = 0;
46     private Class JavaDoc statistics_provider_class = null;
47     private SelectionStrategy selection_strategy = null;
48     private int wait_for_idle_interval = 0;
49
50
51
52     public GIOPConnectionManager()
53     {
54         server_giop_connections = new LinkedList();
55     }
56
57     /**
58      * configures the GIOPConnectionManager
59      */

60
61     public void configure(Configuration myConfiguration)
62         throws ConfigurationException
63     {
64         this.configuration = (org.jacorb.config.Configuration)myConfiguration;
65         logger =
66             configuration.getNamedLogger("jacorb.orb.giop.conn");
67         
68         max_server_giop_connections =
69             configuration.getAttributeAsInteger("jacorb.connection.max_server_connections",
70                                                 Integer.MAX_VALUE );
71         
72         selection_strategy = (SelectionStrategy)
73             configuration.getAttributeAsObject(
74                 "jacorb.connection.selection_strategy_class" );
75         
76         wait_for_idle_interval =
77             configuration.getAttributeAsInteger(
78                 "jacorb.connection.wait_for_idle_interval", 500 );
79
80         String JavaDoc s =
81             configuration.getAttribute( "jacorb.connection.statistics_provider_class","" );
82
83         if( s.length() > 0 )
84         {
85             try
86             {
87                 statistics_provider_class = ObjectUtil.classForName( s );
88             }
89             catch( Exception JavaDoc e )
90             {
91                 if (logger.isErrorEnabled())
92                 {
93                     logger.error( "Unable to create class from property >jacorb.connection.statistics_provider_class<: " + e.getMessage() );
94                 }
95             }
96         }
97
98     }
99     
100
101     public ServerGIOPConnection createServerGIOPConnection(org.omg.ETF.Profile profile,
102                                                            org.omg.ETF.Connection transport,
103                                                            RequestListener request_listener,
104                                                            ReplyListener reply_listener )
105     {
106         //if too many open connections, shut one down
107
if( server_giop_connections.size() >= max_server_giop_connections )
108         {
109             if( selection_strategy != null )
110             {
111                 while( server_giop_connections.size() >= max_server_giop_connections )
112                 {
113                     ServerGIOPConnection to_close = null;
114
115                     synchronized( server_giop_connections )
116                     {
117                         to_close =
118                             selection_strategy.selectForClose( server_giop_connections );
119                     }
120                     
121                     if( to_close != null &&
122                         to_close.tryClose() )
123                     {
124                         break;
125                     }
126                     else
127                     {
128                         try
129                         {
130                             Thread.sleep( wait_for_idle_interval );
131                         }
132                         catch( Exception JavaDoc e )
133                         {
134                             if (logger.isWarnEnabled())
135                             {
136                                 logger.warn("During thread.sleep: " + e.getMessage());
137                             }
138                         }
139                     }
140                 }
141             }
142             else
143             {
144                 if (logger.isErrorEnabled())
145                 {
146                     logger.error( "No. of max server giop connections set, but no SelectionStrategy present" );
147                 }
148             }
149         }
150
151         ServerGIOPConnection connection =
152             new ServerGIOPConnection( profile,
153                                       transport,
154                                       request_listener,
155                                       reply_listener,
156                                       getStatisticsProvider(),
157                                       this);
158
159         try
160         {
161             connection.configure( configuration );
162         }
163         catch( ConfigurationException ce )
164         {
165             if (logger.isWarnEnabled())
166                 logger.warn("ConfigurationException", ce);
167         }
168
169         synchronized( server_giop_connections )
170         {
171             server_giop_connections.add( connection );
172         }
173         
174         return connection;
175     }
176
177     public void unregisterServerGIOPConnection(
178         ServerGIOPConnection connection )
179     {
180         synchronized( server_giop_connections )
181         {
182             server_giop_connections.remove( connection );
183         }
184     }
185
186     public GIOPConnection createClientGIOPConnection(
187         org.omg.ETF.Profile profile,
188         org.omg.ETF.Connection transport,
189         RequestListener request_listener,
190         ReplyListener reply_listener )
191     {
192         ClientGIOPConnection connection =
193             new ClientGIOPConnection( profile,
194                                       transport,
195                                       request_listener,
196                                       reply_listener,
197                                       null );
198         
199         try
200         {
201             connection.configure( configuration );
202         }
203         catch( ConfigurationException ce )
204         {
205             if (logger.isWarnEnabled())
206                 logger.warn("ConfigurationException", ce);
207         }
208         return connection;
209     }
210
211     private StatisticsProvider getStatisticsProvider()
212     {
213         StatisticsProvider result = null;
214         if( statistics_provider_class != null )
215         {
216             try
217             {
218                 result = (StatisticsProvider)
219                     statistics_provider_class.newInstance();
220             }
221             catch( Exception JavaDoc e )
222             {
223                 if (logger.isErrorEnabled())
224                     logger.error( "Unable to create instance from Class >" +
225                                   statistics_provider_class + '<');
226             }
227         }
228         return result;
229     }
230
231 }// GIOPConnectionManager
232

233
234
235
Popular Tags