1 20 package org.jacorb.orb.iiop; 21 22 import java.util.*; 23 24 import org.apache.avalon.framework.configuration.*; 25 import org.apache.avalon.framework.logger.Logger; 26 27 import org.jacorb.orb.CDRInputStream; 28 import org.jacorb.orb.CDROutputStream; 29 import org.jacorb.orb.IIOPAddress; 30 import org.jacorb.orb.TaggedComponentList; 31 32 import org.omg.ETF.*; 33 import org.omg.IOP.*; 34 import org.omg.SSLIOP.*; 35 import org.omg.CSIIOP.*; 36 37 41 public class IIOPProfile 42 extends org.jacorb.orb.etf.ProfileBase 43 { 44 private IIOPAddress primaryAddress = null; 45 46 private boolean dnsEnabled = false; 47 private Logger logger; 48 49 public IIOPProfile() 50 { 51 super(); 52 } 53 54 public IIOPProfile(byte[] data) 55 { 56 initFromProfileData(data); 57 } 58 59 public IIOPProfile(IIOPAddress address, byte[] objectKey) 60 { 61 this.version = new org.omg.GIOP.Version((byte)1,(byte)2); 62 this.primaryAddress = address; 63 this.objectKey = objectKey; 64 this.components = new TaggedComponentList(); 65 } 66 67 public IIOPProfile(IIOPAddress address, byte[] objectKey, int minor) 68 { 69 this.version = new org.omg.GIOP.Version((byte)1,(byte)minor); 70 this.primaryAddress = address; 71 this.objectKey = objectKey; 72 this.components = new TaggedComponentList(); 73 } 74 75 79 80 public IIOPProfile(String corbaloc) 81 { 82 this.version = null; 83 this.primaryAddress = null; 84 this.objectKey = null; 85 this.components = null; 86 this.corbalocStr = corbaloc; 87 } 88 89 public void configure(Configuration configuration) 90 throws ConfigurationException 91 { 92 this.configuration = (org.jacorb.config.Configuration)configuration; 93 logger = this.configuration.getNamedLogger("jacorb.iiop.profile"); 94 dnsEnabled = configuration.getAttribute("jacorb.dns.enable","off").equals("on"); 95 if (this.primaryAddress != null) 96 this.primaryAddress.configure(configuration); 97 98 if (this.corbalocStr != null) 99 { 100 try 101 { 102 this.decode_corbaloc(this.corbalocStr); 103 } 104 catch(Exception e) 105 { 106 e.printStackTrace(); } 108 } 109 } 110 111 112 113 private void decode_corbaloc(String addr) 114 { 115 String host = "127.0.0.1"; short port = 2809; 118 int major = 1; 119 int minor = 2; 121 String errorstr = 122 "Illegal IIOP protocol format in object address format: " + addr; 123 int sep = addr.indexOf(':'); 124 String protocol_identifier = ""; 125 if( sep != 0) 126 protocol_identifier = addr.substring( 0,sep); 127 if( sep + 1 == addr.length()) 128 throw new IllegalArgumentException (errorstr); 129 addr = addr.substring(sep + 1); 130 131 sep = addr.indexOf( '@' ); 133 if( sep > -1) 134 { 135 String ver_str = addr.substring(0,sep); 136 addr = addr.substring(sep+1); 137 sep = ver_str.indexOf('.'); 138 if( sep != -1 ) 139 { 140 try 141 { 142 major = Integer.parseInt(ver_str.substring(0,sep)); 143 minor = Integer.parseInt(ver_str.substring(sep+1)); 144 } 145 catch( NumberFormatException nfe ) 146 { 147 throw new IllegalArgumentException (errorstr); 148 } 149 } 150 } 151 version = new org.omg.GIOP.Version((byte)major,(byte)minor); 152 153 sep = addr.indexOf(':'); 154 if( sep != -1 ) 155 { 156 try 157 { 158 port =(short)Integer.parseInt(addr.substring(sep+1)); 159 host = addr.substring(0, sep); 160 } 161 catch( NumberFormatException ill ) 162 { 163 throw new IllegalArgumentException (errorstr); 164 } 165 } 166 primaryAddress = new IIOPAddress(host,port); 167 try 168 { 169 primaryAddress.configure(configuration); 170 } 171 catch( ConfigurationException ce) 172 { 173 if (logger.isWarnEnabled()) 174 logger.warn("ConfigurationException", ce ); 175 } 176 decode_extensions(protocol_identifier.toLowerCase()); 177 } 178 179 private void decode_extensions(String ident) 180 { 181 this.components = new TaggedComponentList(); 182 if (ident.equals("ssliop")) 183 { 184 SSL ssl = new SSL(); 185 ssl.port = (short)primaryAddress.getPort(); 186 String propname = 187 "jacorb.security.ssl.corbaloc_ssliop.supported_options"; 188 ssl.target_supports = get_ssl_options(propname); 189 propname = 190 "jacorb.security.ssl.corbaloc_ssliop.required_options"; 191 ssl.target_requires = get_ssl_options(propname); 192 193 CDROutputStream out = new CDROutputStream(); 195 out.beginEncapsulatedArray(); 196 SSLHelper.write( out, ssl ); 197 198 components.addComponent 199 (new TaggedComponent( TAG_SSL_SEC_TRANS.value, 200 out.getBufferCopy() ) 201 ); 202 } 203 } 204 205 private short get_ssl_options(String propname) 206 { 207 211 short value = 212 (short)configuration.getAttributeAsInteger(propname,EstablishTrustInTarget.value); 213 return value; 214 } 215 216 221 296 297 303 public void writeAddressProfile(CDROutputStream addressProfileStream) 304 { 305 org.omg.GIOP.VersionHelper.write( addressProfileStream, version); 306 addressProfileStream.write_string(dnsEnabled 307 ? primaryAddress.getHostname() 308 : primaryAddress.getIP()); 309 addressProfileStream.write_ushort( (short) primaryAddress.getPort()); 310 } 311 312 318 public void readAddressProfile(CDRInputStream addressProfileStream) 319 { 320 this.version = org.omg.GIOP.VersionHelper.read(addressProfileStream); 321 this.primaryAddress = IIOPAddress.read(addressProfileStream); 322 if (configuration != null) 323 { 324 try 325 { 326 primaryAddress.configure(configuration); 327 } 328 catch( ConfigurationException ce) 329 { 330 if (logger.isWarnEnabled()) 331 logger.warn("ConfigurationException", ce ); 332 } 333 } 334 } 335 336 342 public int hash() 343 { 344 return hashCode(); 345 } 346 347 public Object clone() throws CloneNotSupportedException 348 { 349 IIOPProfile result = (IIOPProfile)super.clone(); 351 result.version = new org.omg.GIOP.Version(this.version.major, 352 this.version.minor); 353 354 359 if (this.objectKey != null) 360 { 361 result.objectKey = new byte [this.objectKey.length]; 362 System.arraycopy(this.objectKey, 0, result.objectKey, 0, 363 this.objectKey.length); 364 } 365 366 if (this.components != null) 367 { 368 result.components = (TaggedComponentList)this.components.clone(); 369 } 370 371 return result; 372 } 373 374 384 public boolean is_match(Profile prof) 385 { 386 if (prof instanceof IIOPProfile) 387 { 388 IIOPProfile other = (IIOPProfile)prof; 389 return this.primaryAddress.equals(other.primaryAddress) 390 && this.getAlternateAddresses().equals(other.getAlternateAddresses()) 391 && this.getSSLPort() == other.getSSLPort(); 392 } 393 else 394 return false; 395 } 396 397 public int tag() 398 { 399 return TAG_INTERNET_IOP.value; 400 } 401 402 public IIOPAddress getAddress() 403 { 404 return primaryAddress; 405 } 406 407 411 public void patchPrimaryAddress(String newHost, int newPort) 412 { 413 if (newHost != null) 414 { 415 primaryAddress = new IIOPAddress 416 ( 417 newHost, 418 (newPort != -1) ? newPort 419 : primaryAddress.getPort() 420 ); 421 422 } 423 else if(newPort != -1) 424 { 425 primaryAddress = new IIOPAddress(primaryAddress.getIP(), 426 newPort); 427 } 428 try 429 { 430 primaryAddress.configure(configuration); 431 } 432 catch( ConfigurationException ce) 433 { 434 if (logger.isWarnEnabled()) 435 logger.warn("ConfigurationException", ce ); 436 } 437 } 438 439 public List getAlternateAddresses() 440 { 441 return components.getComponents(TAG_ALTERNATE_IIOP_ADDRESS.value, 442 IIOPAddress.class); 443 } 444 445 public SSL getSSL() 446 { 447 return (SSL)components.getComponent( TAG_SSL_SEC_TRANS.value, 448 SSLHelper.class ); 449 } 450 451 459 public int getTLSPortFromCSIComponent() 460 { 461 CompoundSecMechList csmList = 462 (CompoundSecMechList)components.getComponent( 463 TAG_CSI_SEC_MECH_LIST.value, 464 CompoundSecMechListHelper.class); 465 if (csmList != null && csmList.mechanism_list.length > 0) 466 { 467 byte[] tlsSecTransData = 468 csmList.mechanism_list[0].transport_mech.component_data; 469 CDRInputStream in = 470 new CDRInputStream((org.omg.CORBA.ORB )null, tlsSecTransData); 471 try 472 { 473 in.openEncapsulatedArray(); 474 TLS_SEC_TRANS tls = TLS_SEC_TRANSHelper.read(in); 475 if (tls.addresses.length > 0) 476 { 477 int ssl_port = tls.addresses[0].port; 478 if (ssl_port != 0) 479 { 480 if (ssl_port < 0) 481 ssl_port += 65536; 482 return ssl_port; 483 } 484 } 485 } 486 catch ( Exception ex ) 487 { 488 } 489 } 490 return -1; 491 492 } 493 494 498 public int getSSLPort() 499 { 500 SSL ssl = getSSL(); 501 if (ssl == null) 502 return getTLSPortFromCSIComponent(); 503 else 504 { 505 int port = ssl.port; 506 if (port < 0) port += 65536; 507 return port; 508 } 509 } 510 511 514 public IIOPProfile to_GIOP_1_0() 515 { 516 IIOPProfile result = new IIOPProfile(this.primaryAddress, 517 this.objectKey); 518 result.version.minor = 0; 519 return result; 520 } 521 522 public boolean equals(Object other) 523 { 524 if (other instanceof org.omg.ETF.Profile) 525 return this.is_match((org.omg.ETF.Profile)other); 526 else 527 return false; 528 } 529 530 public int hashCode() 531 { 532 return primaryAddress.hashCode(); 533 } 534 535 public String toString() 536 { 537 return primaryAddress.toString(); 538 } 539 540 } 541 | Popular Tags |