1 18 package org.apache.geronimo.interop.rmi.iiop; 19 20 import java.util.Hashtable ; 21 22 import org.apache.geronimo.interop.rmi.iiop.client.ClientNamingContext; 23 import org.apache.geronimo.interop.rmi.iiop.client.Connection; 24 import org.apache.geronimo.interop.security.SimpleSubject; 25 import org.apache.geronimo.interop.util.Base16Binary; 26 import org.apache.geronimo.interop.util.FutureObject; 27 import org.apache.geronimo.interop.util.StringUtil; 28 import org.apache.geronimo.interop.util.ThreadContext; 29 import org.apache.geronimo.interop.util.UTF8; 30 import org.apache.geronimo.interop.util.UnsignedShort; 31 import org.omg.IIOP.ProfileBody_1_1; 32 import org.omg.IIOP.ProfileBody_1_1Helper; 33 import org.omg.IIOP.Version; 34 import org.omg.IOP.IOR ; 35 import org.omg.IOP.TAG_INTERNET_IOP ; 36 import org.omg.IOP.TaggedComponent ; 37 import org.omg.IOP.TaggedProfile ; 38 39 public class ObjectRef extends CorbaObject 40 { 41 42 public static ObjectRef _getInstance() 43 { 44 return new ObjectRef(); 45 } 46 47 51 public static final int IIOP_VERSION_1_0 = 0; 52 public static final int IIOP_VERSION_1_1 = 1; 53 public static final int IIOP_VERSION_1_2 = 2; 54 55 59 private static FutureObject _defaultNamingContext = new FutureObject() 60 { 61 public Object evaluate() 62 { 63 Hashtable env = new Hashtable (); 64 return ClientNamingContext.getInstance(env); 65 } 66 }; 67 68 private static Version VERSION_1_1 = new Version((byte)1, (byte)1); 69 70 private static Version VERSION_1_2 = new Version((byte)1, (byte)2); 71 72 private static TaggedComponent [] NO_PROFILE_COMPONENTS = {}; 73 74 private static TaggedProfile AUTOMATIC_FAILOVER_PROFILE; 75 76 77 private int _iiopVersion = IIOP_VERSION_1_2; 78 79 private IOR _ior; 80 81 private ClientNamingContext _namingContext; 82 83 private ObjectRef _forwardingAddress; 84 85 private String _repositoryID; 87 private int _protocol = Protocol.IIOP; 88 89 private String _endpoint; 90 91 private String _host; 92 93 private int _port = -1; 94 95 public byte[] _objectKey; 96 97 public byte[] _objectState; 98 99 public long _objectVersion; 100 101 103 public ObjectRef() 104 { 105 } 106 107 public Connection $connect() 108 { 109 if(_forwardingAddress != null) 110 { 111 return _forwardingAddress.$connect(); 112 } 113 try 114 { 115 Connection conn = $getNamingContext().getConnectionPool().get(_protocol, $getEndpoint(), this); 116 conn.beforeInvoke(); 117 return conn; 118 } 119 catch (RuntimeException ex) 120 { 121 throw ex; 122 } 123 } 124 125 public ObjectRef $getForwardingAddress() 126 { 127 return _forwardingAddress; 128 } 129 130 public void $setForwardingAddress(ObjectRef ref) 131 { 132 _forwardingAddress = ref; 133 } 134 135 public int $getIiopVersion() 136 { 137 return _iiopVersion; 138 } 139 140 public void $setIiopVersion(int version) 141 { 142 _iiopVersion = version; 143 } 144 145 public String $getID() 146 { 147 if (_repositoryID == null) 148 { 149 return ""; 150 } 151 else 152 { 153 return _repositoryID; 154 } 155 } 156 157 public void $setID(String id) 158 { 159 _repositoryID = id; 160 } 161 162 public IOR $getIOR() 163 { 164 if (_ior == null) 165 { 166 ProfileBody_1_1 profileBody = new ProfileBody_1_1(); 167 profileBody.iiop_version = _iiopVersion == IIOP_VERSION_1_1 168 ? VERSION_1_1 : VERSION_1_2; 169 if (_host == null || _host.length() == 0) 170 { 171 profileBody.host = ThreadContext.getDefaultRmiHost(); 172 } 173 else 174 { 175 profileBody.host = _host; 176 } 177 if (_port == -1) 178 { 179 profileBody.port = (short)ThreadContext.getDefaultRmiPort(); 180 } 181 else 182 { 183 profileBody.port = (short)_port; 184 } 185 profileBody.object_key = _objectKey; 186 profileBody.components = NO_PROFILE_COMPONENTS; 188 189 TaggedProfile profile = new TaggedProfile (); 190 profile.tag = TAG_INTERNET_IOP.value; 191 CdrOutputStream output = CdrOutputStream.getInstanceForEncapsulation(); 192 ProfileBody_1_1Helper.write(output, profileBody); 193 profile.profile_data = output.getEncapsulation(); 194 195 IOR ior = new IOR (); 196 ior.type_id = $getID(); 197 ior.profiles = new TaggedProfile [] { profile }; 198 return ior; 199 } 200 return _ior; 201 } 202 203 public void $setIOR(IOR ior) 204 { 205 _ior = ior; 206 _endpoint = null; 207 _objectKey = null; 208 $getObjectKey(); } 210 211 public ClientNamingContext $getNamingContext() 212 { 213 if (_namingContext == null) 214 { 215 _namingContext = (ClientNamingContext)_defaultNamingContext.getValue(); 216 } 217 return _namingContext; 218 } 219 220 public void $setNamingContext(ClientNamingContext namingContext) 221 { 222 _namingContext = namingContext; 223 } 224 225 public int $getProtocol() 226 { 227 if (_objectKey == null) 228 { 229 $getObjectKey(); } 231 return _protocol; 232 } 233 234 public void $setProtocol(int protocol) 235 { 236 _protocol = protocol; 237 _ior = null; 238 } 239 240 public String $getHost() 241 { 242 if (_objectKey == null) 243 { 244 $getObjectKey(); } 246 return _host; 247 } 248 249 public void $setHost(String host) 250 { 251 _host = host; 252 _endpoint = null; 253 _ior = null; 254 } 255 256 public int $getPort() 257 { 258 if (_objectKey == null) 259 { 260 $getObjectKey(); } 262 return _port; 263 } 264 265 public void $setPort(int port) 266 { 267 _port = port; 268 _endpoint = null; 269 _ior = null; 270 } 271 272 public String $getEndpoint() 273 { 274 if (_endpoint == null) 275 { 276 _endpoint = _host + ":" + _port; 277 } 278 return _endpoint; 279 } 280 281 public byte[] $getObjectKey() 282 { 283 if (_objectKey == null) 284 { 285 if (_ior == null) 286 { 287 throw new IllegalStateException ("$getObjectKey: _ior == null && _objectKey = null"); 288 } 289 int n = _ior.profiles.length; 290 for (int i = 0; i < n; i++) 291 { 292 TaggedProfile profile = _ior.profiles[i]; 293 if (profile.tag == TAG_INTERNET_IOP.value) 294 { 295 ProfileBody_1_1 profileBody; 296 CdrInputStream input = CdrInputStream.getInstanceForEncapsulation(); 297 input.setEncapsulation(profile.profile_data); 298 profileBody = ProfileBody_1_1Helper.read(input); 299 input.recycle(); 300 301 _protocol = Protocol.IIOP; _iiopVersion = profileBody.iiop_version.minor; 303 _host = profileBody.host; 304 _port = UnsignedShort.intValue(profileBody.port); 305 _objectKey = profileBody.object_key; 306 } 307 } 308 } 309 return _objectKey; 310 } 311 312 public String $getObjectKeyString() 313 { 314 return UTF8.toString($getObjectKey()); 315 } 316 317 public String $getObjectName() 318 { 319 byte[] objectKey = $getObjectKey(); 320 int keyLength = objectKey.length; 321 322 for (int colonPos = 0; colonPos < keyLength; colonPos++) 323 { 324 if (objectKey[colonPos] == ':') 325 { 326 return UTF8.toString(objectKey, 0, colonPos); 327 } 328 } 329 return UTF8.toString(objectKey); 330 } 331 332 public void $setObjectKey(byte[] objectKey) 333 { 334 _objectKey = objectKey; 335 _ior = null; 336 } 337 338 public void $setObjectKey(String objectKey) 339 { 340 $setObjectKey(UTF8.fromString(objectKey)); 341 } 342 343 public void $setObjectKey(String prefix, byte[] suffixBytes) 344 { 345 byte[] prefixBytes = UTF8.fromString(prefix); 346 int p = prefixBytes.length; 347 int s = suffixBytes.length; 348 byte[] objectKey = new byte[p + 1 + s]; 349 System.arraycopy(prefixBytes, 0, objectKey, 0, p); 350 objectKey[p] = (byte)':'; 351 System.arraycopy(suffixBytes, 0, objectKey, p + 1, s); 352 $setObjectKey(objectKey); 353 } 354 355 public void $setObjectKey(Class compClass) 356 { 357 SimpleSubject subject = SimpleSubject.getCurrent(); 358 if (subject != null 359 && (subject.getFlags() & SimpleSubject.FLAG_SESSION_MANAGER) != 0) 360 { 361 ObjectKey objectKey = new ObjectKey(); 363 objectKey.component = compClass.getName(); 364 objectKey.username = subject.getUsername(); 365 objectKey.password = subject.getPassword(); 366 byte[] key = objectKey.encode(); 367 key[0] = 'I'; 368 _iiopVersion = IIOP_VERSION_1_1; 369 $setObjectKey(key); 370 } 371 else 372 { 373 $setObjectKey(compClass.getName()); 375 } 376 } 377 378 public Object $getRequestKey() 379 { 380 return null; 381 } 382 383 public String $getIORString() 384 { 385 IOR ior = $getIOR(); 386 CdrOutputStream output = CdrOutputStream.getInstanceForEncapsulation(); 387 output.setGiopVersion(GiopVersion.VERSION_1_0); 388 output.write_Object(this); 389 byte[] bytes = output.getEncapsulation(); 390 String hex = Base16Binary.toString(bytes); 391 String iorString = "IOR:" + hex; 392 return iorString; 393 } 394 395 public void $setIORString(String iorString) 396 { 397 $setIOR($getObjectFromIOR(iorString).$getIOR()); 398 } 399 400 public static ObjectRef $getObjectFromIOR(String iorString) 401 { 402 String hex = StringUtil.removePrefix(iorString, "IOR:"); 403 byte[] bytes = Base16Binary.fromString(hex); 404 CdrInputStream input = CdrInputStream.getInstanceForEncapsulation(); 405 input.setGiopVersion(GiopVersion.VERSION_1_0); 406 input.setEncapsulation(bytes); 407 ObjectRef object = (ObjectRef)input.read_Object(); 408 return object; 409 } 410 411 public String toString() 412 { 413 return getClass().getName() + ":protocol=" + Protocol.getName($getProtocol()) 414 + ":host=" + $getHost() + ":port=" + $getPort() 415 + ":key=" + Base16Binary.toString($getObjectKey()); 416 } 417 } 418 | Popular Tags |