1 24 package fr.dyade.aaa.jndi2.impl; 25 26 import java.io.*; 27 import java.util.*; 28 import javax.naming.*; 29 30 import fr.dyade.aaa.util.*; 31 32 import org.objectweb.util.monolog.api.BasicLevel; 33 import org.objectweb.util.monolog.api.Logger; 34 35 public class ServerImpl { 36 37 40 private Object serverId; 41 42 46 private Object rootOwnerId; 47 48 52 private UpdateListener updateListener; 53 54 59 private ContextManager contextManager; 60 61 73 public ServerImpl(Transaction transaction, 74 Object serverId, 75 Object rootOwnerId) { 76 this.serverId = serverId; 77 this.rootOwnerId = rootOwnerId; 78 contextManager = new ContextManager( 79 transaction, serverId, rootOwnerId); 80 } 81 82 public void setUpdateListener(UpdateListener updateListener) { 83 this.updateListener = updateListener; 84 } 85 86 public void initialize() throws Exception { 87 contextManager.initialize(); 88 89 if (rootOwnerId.equals(serverId)) { 92 NamingContext rootNc = 93 contextManager.getRootNamingContext(); 94 if (rootNc == null) { 95 contextManager.newNamingContext( 96 serverId, 97 null, 98 new CompositeName()); 99 } 100 } 101 } 102 103 120 public void bind(CompositeName path, 121 Object obj) throws NamingException { 122 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 123 Trace.logger.log(BasicLevel.DEBUG, "ServerImpl.bind(" + 124 path + ',' + 125 obj + ',' + ')'); 126 if (path.size() == 0) throw new NameAlreadyBoundException(); 129 130 path = (CompositeName)path.clone(); 131 String lastName = (String )path.remove(path.size() - 1); 132 NamingContext nc = contextManager.getNamingContext(path); 133 134 bind(nc, lastName, obj, serverId); 135 136 if (updateListener != null) { 137 updateListener.onUpdate( 138 new BindEvent(nc.getId(), lastName, obj)); 139 } 140 } 141 142 public void bind(NamingContext nc, 143 String lastName, 144 Object obj, 145 Object ownerId) throws NamingException { 146 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 147 Trace.logger.log(BasicLevel.DEBUG, 148 "ServerImpl.bind(" + 149 nc + ',' + 150 lastName + ',' + 151 obj + ',' + 152 ownerId + ')'); 153 if (! nc.getOwnerId().equals(ownerId)) { 154 throw new NotOwnerException( 155 nc.getOwnerId()); 156 } 157 158 Record r = nc.getRecord(lastName); 159 if (r != null) throw new NameAlreadyBoundException(); 160 else { 161 nc.addRecord(new ObjectRecord(lastName, obj)); 162 contextManager.storeNamingContext(nc); 163 } 164 } 165 166 183 public void rebind(CompositeName path, 184 Object obj) throws NamingException { 185 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 186 Trace.logger.log(BasicLevel.DEBUG, "ServerImpl.rebind(" + 187 path + ',' + 188 obj + ',' + ')'); 189 190 if (path.size() == 0) throw new NamingException("Cannot rebind the root context"); 192 193 path = (CompositeName)path.clone(); 194 String lastName = (String )path.remove(path.size() - 1); 195 NamingContext nc = contextManager.getNamingContext(path); 196 197 rebind(nc, lastName, obj, serverId); 198 199 if (updateListener != null) { 200 updateListener.onUpdate( 201 new RebindEvent(nc.getId(), lastName, obj)); 202 } 203 } 204 205 public void rebind(NamingContext nc, 206 String lastName, 207 Object obj, 208 Object ownerId) throws NamingException { 209 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 210 Trace.logger.log(BasicLevel.DEBUG, 211 "ServerImpl.rebind(" + 212 nc + ',' + 213 lastName + ',' + 214 obj + ',' + 215 ownerId + ')'); 216 if (! nc.getOwnerId().equals(ownerId)) { 217 throw new NotOwnerException( 218 nc.getOwnerId()); 219 } 220 221 Record r = nc.getRecord(lastName); 222 if (r != null) { 223 if (r instanceof ContextRecord) { 224 throw new NamingException("Cannot rebind a context"); 230 } else { 231 ObjectRecord or = (ObjectRecord)r; 232 or.setObject(obj); 233 } 234 } else { 235 nc.addRecord(new ObjectRecord(lastName, obj)); 236 } 237 contextManager.storeNamingContext(nc); 238 } 239 240 256 public Record lookup(CompositeName path) throws NamingException { 257 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 258 Trace.logger.log(BasicLevel.DEBUG, "ServerImpl.lookup(" + path + ')'); 259 260 if (path.size() == 0) { 261 return null; 262 } 263 264 path = (CompositeName)path.clone(); 265 String lastName = (String )path.remove(path.size() - 1); 266 NamingContext nc = contextManager.getNamingContext(path); 267 268 Record r = nc.getRecord(lastName); 269 if (r == null) { 270 NameNotFoundException nnfe = 271 new NameNotFoundException(); 272 nnfe.setResolvedName(path); 273 throw new MissingRecordException( 274 nc.getId(), nc.getOwnerId(), nnfe); 275 } else if (r instanceof ObjectRecord) { 276 return r; 277 } else { 278 return null; 279 } 280 } 281 282 299 public void unbind(CompositeName path) throws NamingException { 300 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 301 Trace.logger.log(BasicLevel.DEBUG, "ServerImpl.unbind(" + 302 path + ')'); 303 304 if (path.size() == 0) throw new NamingException("Cannot unbind the root context"); 306 307 path = (CompositeName)path.clone(); 308 String lastName = (String )path.remove(path.size() - 1); 309 NamingContext nc = contextManager.getNamingContext(path); 310 311 if (unbind(nc, lastName, serverId)) { 312 if (updateListener != null) { 313 updateListener.onUpdate( 314 new UnbindEvent(nc.getId(), lastName)); 315 } 316 } 317 } 318 319 public boolean unbind(NamingContext nc, 320 String lastName, 321 Object ownerId) throws NamingException { 322 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 323 Trace.logger.log(BasicLevel.DEBUG, 324 "ServerImpl.unbind(" + 325 nc + ',' + 326 lastName + ',' + 327 ownerId + ')'); 328 if (! nc.getOwnerId().equals(ownerId)) { 329 throw new NotOwnerException( 330 nc.getOwnerId()); 331 } 332 Record r = nc.getRecord(lastName); 333 if (r != null) { 334 if (r instanceof ContextRecord) { 335 throw new NamingException("Cannot unbind a context"); 336 } else { 337 nc.removeRecord(lastName); 338 contextManager.storeNamingContext(nc); 339 return true; 340 } 341 } else { 342 return false; 344 } 345 } 346 347 public NameClassPair[] list(CompositeName path) throws NamingException { 348 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 349 Trace.logger.log(BasicLevel.DEBUG, "ServerImpl.list(" + path + ')'); 350 NamingContext nc = contextManager.getNamingContext(path); 351 return nc.getNameClassPairs(); 352 } 353 354 public Binding[] listBindings(CompositeName path) throws NamingException { 355 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 356 Trace.logger.log(BasicLevel.DEBUG, "ServerImpl.listBindings(" + path + ')'); 357 NamingContext nc = contextManager.getNamingContext(path); 358 return nc.getBindings(); 359 } 360 361 public void createSubcontext(CompositeName path) 362 throws NamingException { 363 createSubcontext(path, serverId); 364 } 365 366 385 public void createSubcontext(CompositeName path, 386 Object subcontextOwnerId) 387 throws NamingException { 388 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 389 Trace.logger.log( 390 BasicLevel.DEBUG, 391 "ServerImpl.createSubcontext(" + 392 path + ',' + subcontextOwnerId + ')'); 393 394 if (path.size() == 0) throw new NameAlreadyBoundException(); 396 397 CompositeName parentPath = (CompositeName)path.clone(); 398 String lastName = 399 (String )parentPath.remove(parentPath.size() - 1); 400 NamingContext parentNc = 401 contextManager.getNamingContext(parentPath); 402 403 NamingContextId ncid = createSubcontext( 404 parentNc, lastName, path, null, 405 subcontextOwnerId, serverId); 406 407 if (updateListener != null) { 408 updateListener.onUpdate( 409 new CreateSubcontextEvent( 410 parentNc.getId(), lastName, path, ncid, 411 subcontextOwnerId)); 412 } 413 } 414 415 public NamingContextId createSubcontext( 416 NamingContext parentNc, 417 String lastName, 418 CompositeName path, 419 NamingContextId ncid, 420 Object subcontextOwnerId, 421 Object ownerId) 422 throws NamingException { 423 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 424 Trace.logger.log(BasicLevel.DEBUG, 425 "ServerImpl.createSubcontext(" + 426 parentNc + ',' + 427 lastName + ',' + 428 path + ',' + 429 ncid + ',' + 430 subcontextOwnerId + ',' + 431 ownerId + ')'); 432 if (! parentNc.getOwnerId().equals(ownerId)) { 433 throw new NotOwnerException( 434 parentNc.getOwnerId()); 435 } 436 437 if (parentNc.getRecord(lastName) != null) 438 throw new NameAlreadyBoundException(); 439 440 NamingContext nc = 441 contextManager.newNamingContext( 442 subcontextOwnerId, ncid, path); 443 parentNc.addRecord(new ContextRecord( 444 lastName, nc.getId())); 445 contextManager.storeNamingContext(parentNc); 446 return nc.getId(); 447 } 448 449 469 public void destroySubcontext(CompositeName path) throws NamingException { 470 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 471 Trace.logger.log(BasicLevel.DEBUG, 472 "ServerImpl.destroySubcontext(" + 473 path + ')'); 474 475 if (path.size() == 0) 476 throw new NamingException("Cannot delete root context."); 477 478 CompositeName parentPath = (CompositeName)path.clone(); 479 String lastName = (String )parentPath.remove(parentPath.size() - 1); 480 NamingContext parentNc = contextManager.getNamingContext(parentPath); 481 482 try { 483 NamingContext nc = contextManager.getNamingContext(path); 484 if (nc.size() > 0) { 485 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 486 Trace.logger.log(BasicLevel.DEBUG, 487 " -> not empty: nc = " + nc); 488 throw new ContextNotEmptyException(); 489 } 490 } catch (MissingRecordException exc) { 491 return; 493 } 494 495 if (destroySubcontext(parentNc, lastName, path, serverId)) { 496 if (updateListener != null) { 497 updateListener.onUpdate( 498 new DestroySubcontextEvent( 499 parentNc.getId(), lastName, path)); 500 } 501 } 502 } 503 504 public boolean destroySubcontext(NamingContext parentNc, 505 String lastName, 506 CompositeName path, 507 Object ownerId) 508 throws NamingException { 509 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 510 Trace.logger.log(BasicLevel.DEBUG, 511 "ServerImpl.destroySubcontext(" + 512 parentNc + ',' + 513 lastName + ',' + 514 path + ',' + 515 ownerId + ')'); 516 if (! parentNc.getOwnerId().equals(ownerId)) { 517 throw new NotOwnerException( 518 parentNc.getOwnerId()); 519 } 520 521 Record r = parentNc.getRecord(lastName); 522 if (r != null) { 523 if (r instanceof ContextRecord) { 524 ContextRecord cr = (ContextRecord)r; 525 NamingContextId ctxId = cr.getId(); 526 contextManager.delete(ctxId, path); 527 528 parentNc.removeRecord(lastName); 530 contextManager.storeNamingContext(parentNc); 531 return true; 532 } else { 533 throw new NotContextException(); 534 } 535 } else { 536 return false; 538 } 539 } 540 541 548 public NamingContextInfo[] copyNamingContexts(Object serverId) 549 throws NamingException { 550 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 551 Trace.logger.log(BasicLevel.DEBUG, 552 "ServerImpl.copyNamingContexts(" + serverId + ')'); 553 return contextManager.copyNamingContexts(serverId); 554 } 555 556 public NamingContext getNamingContext(NamingContextId ncid) 557 throws NamingException { 558 return contextManager.getNamingContext(ncid); 559 } 560 561 public void addNamingContext(NamingContextInfo ncInfo) 562 throws NamingException { 563 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 564 Trace.logger.log(BasicLevel.DEBUG, 565 "ServerImpl.addNamingContext(" + 566 ncInfo + ')'); 567 contextManager.addNamingContext(ncInfo); 568 } 569 570 public void changeOwner(Object formerOwnerId) 571 throws NamingException { 572 NamingContextInfo[] contexts = 573 contextManager.changeOwner( 574 formerOwnerId, serverId); 575 if (updateListener != null) { 576 updateListener.onUpdate( 577 new ChangeOwnerEvent( 578 formerOwnerId, 579 contexts)); 580 } 581 } 582 583 public void resetNamingContext(NamingContext context) 584 throws NamingException { 585 contextManager.resetNamingContext(context); 586 } 587 588 public void writeBag(ObjectOutputStream out) 589 throws IOException { 590 contextManager.writeBag(out); 591 } 592 593 public void readBag(ObjectInputStream in) 594 throws IOException, ClassNotFoundException { 595 contextManager.readBag(in); 596 } 597 } 598 599 600 | Popular Tags |