1 22 package org.objectweb.petals.kernel.registry; 23 24 import java.util.Hashtable ; 25 import java.util.Map ; 26 import java.util.TreeMap ; 27 import java.util.Map.Entry; 28 29 import javax.naming.Binding ; 30 import javax.naming.InvalidNameException ; 31 import javax.naming.LinkRef ; 32 import javax.naming.NameAlreadyBoundException ; 33 import javax.naming.NameClassPair ; 34 import javax.naming.NameNotFoundException ; 35 import javax.naming.NamingException ; 36 import javax.naming.NotContextException ; 37 38 import org.objectweb.petals.kernel.registry.jndi.JNDIConnection; 39 import org.objectweb.petals.kernel.registry.jndi.NamingContextImpl; 40 import org.objectweb.petals.util.LoggingUtil; 41 import org.objectweb.petals.util.SystemUtil; 42 43 48 public class RegistryUtil { 49 50 53 protected LoggingUtil log; 54 55 58 protected boolean superDebug; 59 60 63 protected RegistryServer server; 64 65 public RegistryUtil(RegistryServer server) { 66 super(); 67 this.server = server; 68 } 69 70 82 public void bind(Object contextName, Object key, Object value) 83 throws NamingException { 84 if (superDebug) { 85 log.start(); 86 } 87 if (!(key instanceof String )) { 88 throw new InvalidNameException ("Key must be non null and a String"); 89 } 90 if (!(contextName instanceof String )) { 91 throw new InvalidNameException ( 92 "Context name must be non null and a String"); 93 } 94 if (((String ) key).length() == 0) { 95 throw new InvalidNameException ("Key must not be an empty String"); 96 } 97 String path = "" 98 + contextName + key; 99 String fullContextName = path.substring(0, path.lastIndexOf("/") + 1); 100 if (path.lastIndexOf("/") == 0) { 101 fullContextName = (String ) contextName; 102 } 103 String lastKey = path.substring(path.lastIndexOf("/") + 1); 104 if (server.getData().containsKey(fullContextName)) { 105 Map <String , Object > subData = server.getData().get(fullContextName); 106 if (subData.containsKey(lastKey)) { 107 if (superDebug) { 108 log.debug(lastKey 109 + " is already bound in context " + fullContextName); 110 } 111 throw new NameAlreadyBoundException (lastKey 112 + " is already bound in context " + fullContextName); 113 } else { 114 subData.put(lastKey, value); 115 } 116 } else { 117 throw new NotContextException ("Context " 118 + fullContextName + " does not exists"); 119 } 120 if (superDebug) { 121 log.end(); 122 } 123 } 124 125 137 public String createSubcontext(Object contextName, Object key) 138 throws NamingException { 139 if (superDebug) { 140 log.start(); 141 } 142 if (!(key instanceof String )) { 143 throw new InvalidNameException ("Key must be non null and a String"); 144 } 145 if (!(contextName instanceof String )) { 146 throw new InvalidNameException ( 147 "Context name must be non null and a String"); 148 } 149 if (((String ) key).length() == 0) { 150 throw new InvalidNameException ("Key must not be an empty String"); 151 } 152 String path = "" 153 + contextName + key; 154 String fullContextName = path.substring(0, path.lastIndexOf("/") + 1); 155 if (path.lastIndexOf("/") == 0) { 156 fullContextName = (String ) contextName; 157 } 158 String lastKey = path.substring(path.lastIndexOf("/") + 1); 159 if (!server.getData().containsKey(fullContextName)) { 160 throw new NotContextException ("Context " 161 + fullContextName + " does not exists"); 162 } else { 163 Map <String , Object > newMap = new TreeMap <String , Object >(); 164 server.getData().put(path 165 + "/", newMap); 166 bind(fullContextName, lastKey, newMap); 167 } 168 if (superDebug) { 169 log.end(); 170 } 171 return "" 172 + contextName + key + "/"; 173 } 174 175 187 public void destroySubcontext(Object contextName, Object key) 188 throws NamingException { 189 if (superDebug) { 190 log.start(); 191 } 192 if (!(key instanceof String )) { 193 throw new InvalidNameException ("Key must be non null and a String"); 194 } 195 if (!(contextName instanceof String )) { 196 throw new InvalidNameException ( 197 "Context name must be non null and a String"); 198 } 199 if (((String ) key).length() == 0) { 200 throw new InvalidNameException ("Key must not be an empty String"); 201 } 202 String path = "" 203 + contextName + key; 204 String fullContextName = path.substring(0, path.lastIndexOf("/") + 1); 205 if (path.lastIndexOf("/") == 0) { 206 fullContextName = (String ) contextName; 207 } 208 String lastKey = path.substring(path.lastIndexOf("/") + 1); 209 if (!server.getData().containsKey(fullContextName)) { 210 throw new NotContextException ("Context " 211 + fullContextName + " does not exists"); 212 } else { 213 unbind(fullContextName, lastKey); 214 server.getData().remove(path 215 + "/"); 216 for (String contextKey : server.getData().keySet()) { 217 if (contextKey.startsWith(path 218 + "/")) { 219 server.getData().remove(contextKey); 220 } 221 } 222 } 223 if (superDebug) { 224 log.end(); 225 } 226 } 227 228 238 public NameClassPair [] list(Object contextName, Object contextToList) 239 throws NamingException { 240 if (superDebug) { 241 log.start(); 242 } 243 if (!(contextToList instanceof String )) { 244 throw new InvalidNameException ( 245 "The name of the context to list must be non null and a String"); 246 } 247 if (!(contextName instanceof String )) { 248 throw new InvalidNameException ( 249 "Context name must be non null and a String"); 250 } 251 if (((String ) contextToList).length() == 0) { 252 throw new InvalidNameException ("You should retrieve this context " 253 + contextName + " by another way"); 254 } 255 NameClassPair [] names = null; 256 String path = "" 257 + contextName + contextToList + "/"; 258 if ("/".equals(contextToList)) { 259 path = (String ) contextToList; 260 } 261 if (!server.getData().containsKey(path)) { 262 throw new NotContextException ("Context " 263 + path + " does not exists"); 264 } else { 265 Map <String , Object > mapToList = server.getData().get(path); 266 names = new NameClassPair [mapToList.keySet().size()]; 267 int i = 0; 268 for (Entry<String , ?> entry : mapToList.entrySet()) { 269 if (entry.getValue() != null) { 270 String className = entry.getValue().getClass().getName(); 271 if (server.getData().containsKey(path 272 + entry.getKey() + "/")) { 273 className = NamingContextImpl.class.getName(); 274 } 275 names[i] = new NameClassPair (entry.getKey(), className); 276 } else { 277 names[i] = new NameClassPair (entry.getKey(), null); 278 } 279 i++; 280 } 281 } 282 if (superDebug) { 283 log.end(); 284 } 285 return names; 286 } 287 288 298 public Binding [] listBindings(Object contextName, Object contextToList) 299 throws NamingException { 300 if (superDebug) { 301 log.start(); 302 } 303 if (!(contextToList instanceof String )) { 304 throw new InvalidNameException ( 305 "The name of the context to list must be non null and a String"); 306 } 307 if (!(contextName instanceof String )) { 308 throw new InvalidNameException ( 309 "Context name must be non null and a String"); 310 } 311 if (((String ) contextToList).length() == 0) { 312 throw new InvalidNameException ("You should retrieve this context " 313 + contextName + " by another way"); 314 } 315 Binding [] bindings = null; 316 Map <String , ?> mapToList = null; 317 String path = "" 318 + contextName + contextToList + "/"; 319 if ("/".equals(contextToList)) { 320 path = (String ) contextToList; 321 } 322 if (!server.getData().containsKey(path)) { 323 throw new NotContextException ("Context " 324 + path + " does not exists"); 325 } else { 326 mapToList = server.getData().get(path); 327 bindings = new Binding [mapToList.keySet().size()]; 328 int i = 0; 329 for (Entry<String , ?> entry : mapToList.entrySet()) { 330 if (entry.getValue() != null) { 331 String className = entry.getValue().getClass().getName(); 332 Object value = entry.getValue(); 333 if (server.getData().containsKey(path 334 + entry.getKey() + "/")) { 335 className = NamingContextImpl.class.getName(); 336 value = new NamingContextImpl( 337 new Hashtable <String , Object >(), 338 new JNDIConnection(Integer.parseInt(SystemUtil 339 .getJndiPort()), SystemUtil.getHost()), path 340 + entry.getKey() + "/"); 341 } 342 bindings[i] = new Binding (entry.getKey(), className, value); 343 } else { 344 bindings[i] = new Binding (entry.getKey(), null, null); 345 } 346 i++; 347 } 348 } 349 if (superDebug) { 350 log.end(); 351 } 352 return bindings; 353 } 354 355 365 public Object lookup(Object contextName, Object key) throws NamingException { 366 if (superDebug) { 367 log.start(); 368 } 369 if (!(key instanceof String )) { 370 throw new InvalidNameException ("Key must be non null and a String"); 371 } 372 if (!(contextName instanceof String )) { 373 throw new InvalidNameException ( 374 "Context name must be non null and a String"); 375 } 376 if (((String ) key).length() == 0) { 377 throw new InvalidNameException ("You should retrieve this context " 378 + contextName + " by another way"); 379 } 380 Object out = null; 381 String path = "" 382 + contextName + key; 383 if (((String ) key).startsWith("/")) { 384 path = (String ) key; 386 } else if (((String ) key).startsWith(".")) { 387 path = "" 389 + contextName + ((String ) key).substring(1); 390 } 391 String fullContextName = path.substring(0, path.lastIndexOf("/") + 1); 392 if (path.lastIndexOf("/") == 0) { 393 fullContextName = (String ) contextName; 394 } 395 String lastKey = path.substring(path.lastIndexOf("/") + 1); 396 if (!server.getData().containsKey(fullContextName)) { 397 throw new NotContextException ("Context " 398 + fullContextName + " does not exists"); 399 } else { 400 if (server.getData().get(fullContextName).containsKey(lastKey)) { 401 if (server.getData().containsKey(fullContextName 402 + lastKey + "/")) { 403 out = new NamingContextImpl( 404 new Hashtable <String , Object >(), new JNDIConnection( 405 Integer.parseInt(SystemUtil.getJndiPort()), 406 SystemUtil.getHost()), "" 407 + contextName + key + "/"); 408 } else { 409 out = server.getData().get(fullContextName).get(lastKey); 410 } 411 } else { 412 throw new NameNotFoundException (lastKey 413 + " is not bound in the context " + fullContextName); 414 } 415 } 416 if (superDebug) { 417 log.end(); 418 } 419 return out; 420 } 421 422 432 public Object lookupLink(Object contextName, Object key) 433 throws NamingException { 434 if (superDebug) { 435 log.start(); 436 } 437 if (!(key instanceof String )) { 438 throw new InvalidNameException ("Key must be non null and a String"); 439 } 440 if (!(contextName instanceof String )) { 441 throw new InvalidNameException ( 442 "Context name must be non null and a String"); 443 } 444 if (((String ) key).length() == 0) { 445 throw new InvalidNameException ("You should retrieve this context " 446 + contextName + " by another way"); 447 } 448 Object out = null; 449 boolean test = true; 450 while (test) { 451 out = lookup(contextName, key); 452 if (out instanceof LinkRef ) { 453 LinkRef linkRef = (LinkRef ) out; 454 contextName = "/"; 455 key = linkRef.getLinkName().substring(1); 456 } else { 457 test = false; 458 } 459 } 460 if (superDebug) { 461 log.end(); 462 } 463 return out; 464 } 465 466 480 public void rebind(Object contextName, Object key, Object value) 481 throws NamingException { 482 if (superDebug) { 483 log.start(); 484 } 485 if (!(key instanceof String )) { 486 throw new InvalidNameException ("Key must be non null and a String"); 487 } 488 if (!(contextName instanceof String )) { 489 throw new InvalidNameException ( 490 "Context name must be non null and a String"); 491 } 492 if (((String ) key).length() == 0) { 493 throw new InvalidNameException ("Key must not be an empty String"); 494 } 495 String path = "" 496 + contextName + key; 497 String fullContextName = path.substring(0, path.lastIndexOf("/") + 1); 498 if (path.lastIndexOf("/") == 0) { 499 fullContextName = (String ) contextName; 500 } 501 String lastKey = path.substring(path.lastIndexOf("/") + 1); 502 if (!server.getData().containsKey(fullContextName)) { 503 throw new NotContextException ("Context " 504 + fullContextName + " does not exists"); 505 } else { 506 if (server.getData().get(fullContextName).containsKey(lastKey)) { 507 server.getData().get(fullContextName).put(lastKey, value); 508 } else { 509 throw new NameNotFoundException (lastKey 510 + " is not bound in the context " + fullContextName); 511 } 512 } 513 if (superDebug) { 514 log.end(); 515 } 516 } 517 518 530 public void rename(Object contextName, Object oldKey, Object newKey) 531 throws NamingException { 532 if (superDebug) { 533 log.start(); 534 } 535 if (!(oldKey instanceof String )) { 536 throw new InvalidNameException ( 537 "Old key must be non null and a String"); 538 } 539 if (!(newKey instanceof String )) { 540 throw new InvalidNameException ( 541 "New key must be non null and a String"); 542 } 543 if (!(contextName instanceof String )) { 544 throw new InvalidNameException ( 545 "Context name must be non null and a String"); 546 } 547 if (((String ) oldKey).length() == 0) { 548 throw new InvalidNameException ( 549 "Old key must not be an empty String"); 550 } 551 if (((String ) newKey).length() == 0) { 552 throw new InvalidNameException ( 553 "New key must not be an empty String"); 554 } 555 Object value = lookup(contextName, oldKey); 556 unbind(contextName, oldKey); 557 bind(contextName, newKey, value); 558 if (superDebug) { 559 log.end(); 560 } 561 } 562 563 public void setLog(LoggingUtil log) { 564 this.log = log; 565 } 566 567 576 public void unbind(Object contextName, Object key) throws NamingException { 577 if (superDebug) { 578 log.start(); 579 } 580 if (!(key instanceof String )) { 581 throw new InvalidNameException ("Key must be non null and a String"); 582 } 583 if (!(contextName instanceof String )) { 584 throw new InvalidNameException ( 585 "Context name must be non null and a String"); 586 } 587 if (((String ) key).length() == 0) { 588 throw new InvalidNameException ("You should retrieve this context " 589 + contextName + " by another way"); 590 } 591 String path = "" 592 + contextName + key; 593 String fullContextName = path.substring(0, path.lastIndexOf("/") + 1); 594 if (path.lastIndexOf("/") == 0) { 595 fullContextName = (String ) contextName; 596 } 597 String lastKey = path.substring(path.lastIndexOf("/") + 1); 598 if (!server.getData().containsKey(fullContextName)) { 599 throw new NotContextException ("Context " 600 + fullContextName + " does not exists"); 601 } else { 602 if (server.getData().get(fullContextName).containsKey(lastKey)) { 603 server.getData().get(fullContextName).remove(lastKey); 604 } else { 605 throw new NameNotFoundException (lastKey 606 + " is not bound in the context " + fullContextName); 607 } 608 } 609 if (superDebug) { 610 log.end(); 611 } 612 } 613 614 public boolean isSuperDebug() { 615 return superDebug; 616 } 617 618 public void setSuperDebug(boolean superDebug) { 619 this.superDebug = superDebug; 620 } 621 622 } 623 | Popular Tags |