1 26 27 28 package org.objectweb.mobilitools.util.corba; 29 30 31 import org.omg.CORBA.*; 32 import org.omg.CosNaming.*; 33 import org.omg.CosNaming.NamingContextPackage.*; 34 import java.util.*; 35 36 37 42 public class NameService 43 { 44 NamingContext nameServiceRef; 46 static final int LIST_MAX_ONCE = 100; 47 48 49 55 public NameService(ORB orb) throws NameServiceException 56 { 57 org.omg.CORBA.Object objRef = null; 58 59 try 60 { 61 objRef = orb.resolve_initial_references("NameService"); 62 } 63 catch (org.omg.CORBA.ORBPackage.InvalidName e) 64 { 65 throw new NameServiceException(e); 66 } 67 if (objRef != null) 68 { 69 try 70 { 71 nameServiceRef = NamingContextHelper.narrow(objRef); 72 } 73 catch (org.omg.CORBA.BAD_PARAM e) 74 { 75 throw new NameServiceException(e); 76 } 77 } 78 else 79 { 80 throw new NameServiceException(); 81 } 82 } 83 84 85 91 public NameService(org.omg.CORBA.Object obj) throws NameServiceException 92 { 93 if (obj != null) 94 { 95 try 96 { 97 nameServiceRef = NamingContextHelper.narrow(obj); 98 } 99 catch (org.omg.CORBA.BAD_PARAM e) 100 { 101 throw new NameServiceException(e); 102 } 103 } 104 else 105 { 106 throw new NameServiceException(); 107 } 108 } 109 110 111 119 public org.omg.CORBA.Object resolve(String name) throws NameServiceException 120 { 121 return resolve(stringToName(name)); 122 } 123 124 125 139 public org.omg.CORBA.Object resolve(NameComponent[] name) throws NameServiceException 140 { 141 org.omg.CORBA.Object obj = null; 142 143 if (name.length == 0) 144 { 145 return nameServiceRef; 146 } 147 148 try 149 { 150 obj = nameServiceRef.resolve(name); 151 } 152 catch (SystemException e) 153 { 154 throw new NameServiceException(e); 155 } 156 catch (InvalidName e) 157 { 158 throw new NameServiceException(e); 159 } 160 catch (NotFound e) 161 { 162 throw new NameServiceException(e); 163 } 164 catch (CannotProceed e) 165 { 166 throw new NameServiceException(e); 167 } 168 return obj; 169 } 170 171 172 179 public NSbinding[] list(String contextName) throws NameServiceException 180 { 181 return list(stringToName(contextName)); 182 } 183 184 185 196 public NSbinding[] list(NameComponent[] contextName) throws NameServiceException 197 { 198 NamingContext context = null; 199 BindingListHolder listHolder = new BindingListHolder(); 200 BindingIteratorHolder iteratorHolder = new BindingIteratorHolder(); 201 NSbinding element; 202 int size; 203 204 try 205 { 206 context = NamingContextHelper.narrow(resolve(contextName)); 207 } 208 catch (org.omg.CORBA.BAD_PARAM e) 209 { 210 throw new NameServiceException(e); 211 } 212 try 213 { 214 context.list(LIST_MAX_ONCE, listHolder, iteratorHolder); 215 } 216 catch (SystemException e) 217 { 218 throw new NameServiceException(e); 219 } 220 221 Vector vect = new Vector(LIST_MAX_ONCE, LIST_MAX_ONCE); 222 boolean again = true; 223 while (again) 224 { 225 for (int i=0 ; i<listHolder.value.length ; ++i) 226 { 227 try 228 { 229 element = new NSbinding( 230 listHolder.value[i].binding_name[listHolder.value[i].binding_name.length-1].id, 231 listHolder.value[i].binding_name[listHolder.value[i].binding_name.length-1].kind, 232 (listHolder.value[i].binding_type.value() == BindingType._nobject 233 ? NSbinding.OBJECT : NSbinding.CONTEXT), 234 context.resolve(listHolder.value[i].binding_name)); 235 vect.addElement(element); 236 } 237 catch (SystemException e) 238 { 239 throw new NameServiceException(e); 240 } 241 catch (InvalidName e) 242 { 243 } 244 catch (NotFound e) 245 { 246 } 247 catch (CannotProceed e) 248 { 249 throw new NameServiceException(e); 250 } 251 } 252 if (iteratorHolder.value != null) 253 { 254 try 255 { 256 again = iteratorHolder.value.next_n(LIST_MAX_ONCE, listHolder); 257 } 258 catch (SystemException e) 259 { 260 throw new NameServiceException(e); 261 } 262 } 263 else 264 { 265 again = false; 266 } 267 } 268 NSbinding[] result = new NSbinding[vect.size()]; 269 Enumeration lister = vect.elements(); 270 for (int i=0 ; lister.hasMoreElements() ; ++i) 271 { 272 result[i] = (NSbinding)lister.nextElement(); 273 } 274 return result; 275 } 276 277 278 public void makePath(String name) throws NameServiceException 279 { 280 makePath(stringToName(name)); 281 } 282 283 284 public void makePath(NameComponent[] name) throws NameServiceException 285 { 286 NamingContext nc = nameServiceRef; 287 NameComponent[] step = new NameComponent[1]; 288 289 for (int i=0 ; i<name.length ; ++i) 290 { 291 step[0] = name[i]; 292 try 293 { 294 nc = nc.bind_new_context(step); 295 } 296 catch (AlreadyBound e) 297 { 298 try 299 { 300 nc = NamingContextHelper.narrow(nc.resolve(step)); 301 } 302 catch (SystemException e2) 303 { 304 throw new NameServiceException(e); 305 } 306 catch (InvalidName e2) 307 { 308 throw new NameServiceException(e); 309 } 310 catch (NotFound e2) 311 { 312 throw new NameServiceException(e); 313 } 314 catch (CannotProceed e2) 315 { 316 throw new NameServiceException(e); 317 } 318 } 319 catch (SystemException e) 320 { 321 throw new NameServiceException(e); 322 } 323 catch (InvalidName e) 324 { 325 throw new NameServiceException(e); 326 } 327 catch (NotFound e) 328 { 329 throw new NameServiceException(e); 330 } 331 catch (CannotProceed e) 332 { 333 throw new NameServiceException(e); 334 } 335 } 336 } 337 338 339 public void bind(String name, org.omg.CORBA.Object obj) throws NameServiceException 340 { 341 bind(stringToName(name), obj, false); 342 } 343 344 345 public void bind(NameComponent[] name, org.omg.CORBA.Object obj) throws NameServiceException 346 { 347 bind(name, obj, false); 348 } 349 350 351 public void rebind(String name, org.omg.CORBA.Object obj) throws NameServiceException 352 { 353 bind(stringToName(name), obj, true); 354 } 355 356 357 public void rebind(NameComponent[] name, org.omg.CORBA.Object obj) throws NameServiceException 358 { 359 bind(name, obj, true); 360 } 361 362 363 public void unbind(String name) throws NameServiceException 364 { 365 unbind(stringToName(name)); 366 } 367 368 369 public void unbind(NameComponent[] name) throws NameServiceException 370 { 371 try 372 { 373 nameServiceRef.unbind(name); 374 } 375 catch (SystemException e) 376 { 377 throw new NameServiceException(e); 378 } 379 catch (InvalidName e) 380 { 381 throw new NameServiceException(e); 382 } 383 catch (NotFound e) 384 { 385 throw new NameServiceException(e); 386 } 387 catch (CannotProceed e) 388 { 389 throw new NameServiceException(e); 390 } 391 } 392 393 394 public void destroy(String name) throws NameServiceException 395 { 396 destroy(stringToName(name)); 397 } 398 399 400 public void destroy(NameComponent[] name) throws NameServiceException 401 { 402 try 403 { 404 NamingContext nc = NamingContextHelper.narrow(nameServiceRef.resolve(name)); 405 unbind(name); 406 nc.destroy(); 407 } 408 catch (SystemException e) 409 { 410 throw new NameServiceException(e); 411 } 412 catch (InvalidName e) 413 { 414 throw new NameServiceException(e); 415 } 416 catch (NotFound e) 417 { 418 throw new NameServiceException(e); 419 } 420 catch (CannotProceed e) 421 { 422 throw new NameServiceException(e); 423 } 424 catch (NotEmpty e) 425 { 426 throw new NameServiceException(e); 427 } 428 catch (NullPointerException e) 429 { 430 throw new NameServiceException(e); 431 } 432 } 433 434 435 public void deepUnbind(String name) throws NameServiceException 436 { 437 deepUnbind(stringToName(name)); 438 } 439 440 441 public void deepUnbind(NameComponent[] name) throws NameServiceException 442 { 443 if (isContext(name)) 444 { 445 NSbinding[] bindings = list(name); 446 NameComponent[] dependency = new NameComponent[1 + name.length]; 447 int i; 448 449 for (i=0 ; i<name.length ; ++i) 450 { 451 dependency[i] = name[i]; 452 } 453 for (i=0 ; i<bindings.length ; ++i) 454 { 455 dependency[name.length] = new NameComponent(bindings[i].name, bindings[i].kind); 456 deepUnbind(dependency); 457 } 458 destroy(name); 459 } 460 else 461 { 462 unbind(name); 463 } 464 } 465 466 467 public void bind(NameComponent[] name, org.omg.CORBA.Object obj, boolean force) 468 throws NameServiceException 469 { 470 if (name.length > 0) 471 { 472 NameComponent[] path = new NameComponent[name.length - 1]; 473 for (int i=0 ; i<path.length ; ++i) 474 { 475 path[i] = name[i]; 476 } 477 makePath(path); 478 NameComponent[] singleName = new NameComponent[1]; 479 singleName[0] = name[name.length-1]; 480 NamingContext context = NamingContextHelper.narrow(resolve(path)); 481 try 482 { 483 if (force) 484 { 485 if (isContext(obj)) 486 { 487 context.rebind_context(singleName, NamingContextHelper.narrow(obj)); 488 } 489 else 490 { 491 context.rebind(singleName, obj); 492 } 493 } 494 else 495 { 496 if (isContext(obj)) 497 { 498 context.bind_context(singleName, NamingContextHelper.narrow(obj)); 499 } 500 else 501 { 502 context.bind(singleName, obj); 503 } 504 } 505 } 506 catch (SystemException e) 507 { 508 throw new NameServiceException(e); 509 } 510 catch (InvalidName e) 511 { 512 throw new NameServiceException(e); 513 } 514 catch (NotFound e) 515 { 516 throw new NameServiceException(e); 517 } 518 catch (CannotProceed e) 519 { 520 throw new NameServiceException(e); 521 } 522 catch (AlreadyBound e) 523 { 524 throw new NameServiceException(e); 525 } 526 } 527 else 528 { 529 throw new NameServiceException("The provided name is empty."); 530 } 531 } 532 533 534 static public boolean isContext(org.omg.CORBA.Object obj) 535 { 536 if (obj instanceof NamingContext) 537 { 538 return true; 539 } 540 try 541 { 542 NamingContextHelper.narrow(obj); 543 } 544 catch (org.omg.CORBA.BAD_PARAM e) 545 { 546 return false; 547 } 548 return true; 549 } 550 551 552 public boolean isContext(String name) throws NameServiceException 553 { 554 return NameService.isContext(resolve(name)); 555 } 556 557 558 public boolean isContext(NameComponent[] name) throws NameServiceException 559 { 560 return NameService.isContext(resolve(name)); 561 } 562 563 564 static public String nameToString(NameComponent[] name) 565 { 566 String strName = ""; 567 568 for (int i=0 ; i<name.length ; ++i) 569 { 570 strName = strName + name[i].id; 571 if (name[i].kind.length() != 0) 572 { 573 strName = strName + "!" + name[i].kind; 574 } 575 strName = strName + "/"; 576 } 577 return strName; 578 } 579 580 581 static public NameComponent[] stringToName(String strName) 582 { 583 StringTokenizer parser = new StringTokenizer(strName, "/", false); 584 NameComponent[] name = new NameComponent[parser.countTokens()]; 585 StringTokenizer subParser; 586 String token; 587 588 for (int i=0 ; i<name.length ; ++i) 589 { 590 subParser = new StringTokenizer(parser.nextToken(), "!", true); 591 name[i] = new NameComponent(); 592 if (subParser.hasMoreTokens()) 593 { 594 token = subParser.nextToken(); 595 if (token.equals("!")) 596 { 597 name[i].id = ""; 598 } 599 else 600 { 601 name[i].id = token; 602 } 603 if (subParser.hasMoreTokens()) 604 { 605 token = subParser.nextToken(); 606 if (token.equals("!")) 607 { 608 if (subParser.hasMoreTokens()) 609 { 610 name[i].kind = subParser.nextToken(); 611 } 612 else 613 { 614 name[i].kind = ""; 615 } 616 } 617 else 618 { 619 name[i].kind = token; 620 } 621 } 622 else 623 { 624 name[i].kind = ""; 625 } 626 } 627 else 628 { 629 name[i].id = ""; 630 name[i].kind = ""; 631 } 632 } 633 return name; 634 } 635 } 636 | Popular Tags |