1 20 21 package org.jacorb.orb.giop; 22 23 import java.util.*; 24 25 import org.apache.avalon.framework.logger.Logger; 26 27 import org.jacorb.orb.CDROutputStream; 28 import org.jacorb.orb.ParsedIOR; 29 30 import org.omg.IOP.*; 31 import org.omg.CONV_FRAME.*; 32 33 42 43 public class ClientConnection 44 implements ReplyListener, ConnectionListener 45 { 46 private GIOPConnection connection = null; 47 private org.omg.CORBA.ORB orb = null; 48 49 private HashMap replies; 50 51 private HashMap sasContexts; 53 private static long last_client_context_id = 0; 54 55 56 private int client_count = 0; 57 58 private int id_count = 0; 60 61 private ClientConnectionManager conn_mg = null; 62 63 private boolean client_initiated = true; 64 65 private String info = null; 66 67 private boolean gracefulStreamClose = false; 71 72 private org.omg.ETF.Profile registeredProfile = null; 76 77 private Logger logger = null; 78 79 public ClientConnection( GIOPConnection connection, 80 org.omg.CORBA.ORB orb, 81 ClientConnectionManager conn_mg, 82 org.omg.ETF.Profile registeredProfile, 83 boolean client_initiated ) 84 { 85 this.connection = connection; 86 this.orb = orb; 87 this.conn_mg = conn_mg; 88 this.registeredProfile = registeredProfile; 89 this.info = registeredProfile.toString(); 90 this.client_initiated = client_initiated; 91 92 logger = 93 ((org.jacorb.orb.ORB)orb).getConfiguration().getNamedLogger("jacorb.giop.conn"); 94 95 if( ! client_initiated ) 101 { 102 id_count = 1; 103 } 104 105 connection.setReplyListener( this ); 106 connection.setConnectionListener( this ); 107 108 replies = new HashMap(); 109 sasContexts = new HashMap(); 110 } 111 112 public final GIOPConnection getGIOPConnection() 113 { 114 return connection; 115 } 116 117 122 public org.omg.ETF.Profile getRegisteredProfile() 123 { 124 return registeredProfile; 125 } 126 127 public void setCodeSet( ParsedIOR pior ) 128 { 129 if( isTCSNegotiated() ) 130 { 131 return; 133 } 134 135 if( pior.getEffectiveProfile().version().minor == 0 ) 138 { 139 connection.markTCSNegotiated(); 140 return; 141 } 142 143 int tcs = -1; 144 int tcsw = -1; 145 146 CodeSetComponentInfo info = pior.getCodeSetComponentInfo(); 147 if( info != null ) 148 { 149 tcs = CodeSet.selectTCS( info ); 150 tcsw = CodeSet.selectTCSW( info ); 151 } 152 else 153 { 154 if (logger.isDebugEnabled()) 155 logger.debug("No CodeSetComponentInfo in IOR. Will use default CodeSets" ); 156 157 161 167 168 171 tcs = CodeSet.getTCSDefault(); 172 tcsw = CodeSet.getTCSWDefault(); 173 } 174 175 if( tcs == -1 || tcsw == -1 ) 176 { 177 throw new org.omg.CORBA.CODESET_INCOMPATIBLE ( 180 "WARNING: CodeSet negotiation failed! No matching " + 181 (( tcs == -1 )? "normal" : "wide") + 182 " CodeSet found"); 183 } 184 185 connection.setCodeSets( tcs, tcsw ); 186 187 if (logger.isDebugEnabled()) 188 { 189 logger.debug( "Successfully negotiated Codesets. Using " + 190 CodeSet.csName( tcs ) + " as TCS and " + 191 CodeSet.csName( tcsw ) + " as TCSW" ); 192 } 193 194 } 195 196 public boolean isTCSNegotiated() 197 { 198 return connection.isTCSNegotiated(); 199 } 200 201 public int getTCS() 202 { 203 return connection.getTCS(); 204 } 205 206 public int getTCSW() 207 { 208 return connection.getTCSW(); 209 } 210 211 public String getInfo() 212 { 213 return info; 214 } 215 216 public synchronized int getId() 217 { 218 int id = id_count; 219 220 id_count += 2; 223 224 return id; 225 } 226 227 public synchronized void incClients() 228 { 229 client_count++; 230 } 231 232 238 public synchronized boolean decClients() 239 { 240 boolean result = false; 241 242 client_count--; 243 244 if (client_count == 0 ) 245 { 246 result = true; 247 } 248 return result; 249 } 250 251 252 public boolean isClientInitiated() 253 { 254 return client_initiated; 255 } 256 257 260 public void sendRequest( MessageOutputStream os, 261 ReplyPlaceholder placeholder, 262 int request_id, 263 boolean response_expected ) 264 { 265 Integer key = new Integer ( request_id ); 266 267 synchronized( replies ) 268 { 269 replies.put( key, placeholder ); 270 } 271 272 try 273 { 274 sendRequest( os, response_expected ); 275 } 276 catch( org.omg.CORBA.SystemException e ) 277 { 278 synchronized( replies ) 281 { 282 replies.remove( key ); 283 } 284 throw e; 285 } 286 287 } 288 289 public void sendRequest( MessageOutputStream os, 290 boolean response_expected ) 291 { 292 try 293 { 294 connection.sendRequest( os, response_expected ); 295 } 296 catch (java.io.IOException e) 297 { 298 if (logger.isDebugEnabled()) 299 logger.debug("IOException", e); 300 301 throw new org.omg.CORBA.COMM_FAILURE 302 (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE); 303 } 304 } 305 306 309 310 public void close() 311 { 312 connection.close(); 313 } 314 315 318 319 public void replyReceived( byte[] reply, 320 GIOPConnection connection ) 321 { 322 connection.decPendingMessages(); 323 324 Integer key = new Integer ( Messages.getRequestId( reply )); 325 326 ReplyPlaceholder placeholder = null; 327 328 synchronized( replies ) 329 { 330 placeholder = 331 (ReplyPlaceholder) replies.remove( key ); 332 } 333 334 if( placeholder != null ) 335 { 336 ReplyInputStream ris = new ReplyInputStream (orb, reply); 337 ris.setCodeSet (this.getTCS(),this.getTCSW()); 338 placeholder.replyReceived(ris); 340 } 341 else 342 { 343 if (logger.isWarnEnabled()) 344 { 345 logger.warn("Received reply for unknown request id: " + 346 key); 347 } 348 } 349 } 350 351 352 public void locateReplyReceived( byte[] reply, 353 GIOPConnection connection ) 354 { 355 connection.decPendingMessages(); 356 357 Integer key = new Integer ( Messages.getRequestId( reply )); 358 359 ReplyPlaceholder placeholder = null; 360 361 synchronized( replies ) 362 { 363 placeholder = 364 (ReplyPlaceholder) replies.remove( key ); 365 } 366 367 if( placeholder != null ) 368 { 369 placeholder.replyReceived( new LocateReplyInputStream( orb, 371 reply )); 372 } 373 else 374 { 375 if (logger.isWarnEnabled()) 376 { 377 logger.warn("Received reply for unknown request id: " + 378 key); 379 } 380 } 381 } 382 383 389 public void closeConnectionReceived( byte[] close_conn, 390 GIOPConnection connection ) 391 { 392 if (logger.isInfoEnabled()) 393 { 394 logger.info("Received CloseConnection message"); 395 } 396 397 if( client_initiated ) 398 { 399 gracefulStreamClose = true; 400 ((ClientGIOPConnection) connection).closeAllowReopen(); 401 402 streamClosed(); 408 } 409 } 410 411 412 416 417 public void connectionClosed() 418 { 419 if( ! client_initiated ) 420 { 421 426 conn_mg.removeConnection( this ); 427 } 428 429 streamClosed(); 430 } 431 432 436 437 public void streamClosed() 438 { 439 synchronized( replies ) 440 { 441 if( replies.size() > 0 ) 442 { 443 if( gracefulStreamClose ) 444 { 445 if (logger.isDebugEnabled()) 446 { 447 logger.debug("Stream closed. Will remarshal " + 448 replies.size() + " messages" ); 449 } 450 } 451 else 452 { 453 if (logger.isWarnEnabled()) 454 { 455 logger.warn("Abnormal connection termination. Lost " + 456 replies.size() + " outstanding replie(s)!"); 457 } 458 } 459 460 Iterator entries = replies.values().iterator(); 461 ReplyPlaceholder placeholder; 462 463 while( entries.hasNext() ) 464 { 465 placeholder = (ReplyPlaceholder)entries.next(); 466 467 if( gracefulStreamClose ) 468 { 469 placeholder.retry(); 470 } 471 else 472 { 473 placeholder.cancel(); 474 } 475 entries.remove(); 476 } 477 } 478 } 479 480 gracefulStreamClose = false; 481 } 482 483 public org.omg.ETF.Profile get_server_profile() 484 { 485 return connection.getTransport().get_server_profile(); 486 } 487 488 public long cacheSASContext(byte[] client_authentication_token) 489 { 490 long client_context_id = 0; 491 String key = new String (client_authentication_token); 492 synchronized ( sasContexts ) 493 { 494 if (!sasContexts.containsKey(key)) 495 { 496 client_context_id = ++last_client_context_id; 498 sasContexts.put(key, new Long (client_context_id)); 499 client_context_id = -client_context_id; 500 } 501 else 502 { 503 client_context_id = ((Long )sasContexts.get(key)).longValue(); 505 } 506 } 507 return client_context_id; 508 } 509 510 public long purgeSASContext(long client_context_id) 511 { 512 synchronized ( sasContexts ) 513 { 514 Iterator entries = sasContexts.keySet().iterator(); 515 while( entries.hasNext() ) 516 { 517 Object key = entries.next(); 518 if (((Long )sasContexts.get(key)).longValue() != client_context_id) 519 { 520 continue; 521 } 522 entries.remove(); 523 break; 524 } 525 } 526 return client_context_id; 527 } 528 } | Popular Tags |