1 22 package org.jnp.server; 23 24 import java.util.Collection ; 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.Vector ; 29 import javax.naming.Binding ; 30 import javax.naming.CannotProceedException ; 31 import javax.naming.Context ; 32 import javax.naming.InvalidNameException ; 33 import javax.naming.Name ; 34 import javax.naming.NameAlreadyBoundException ; 35 import javax.naming.NameClassPair ; 36 import javax.naming.NameNotFoundException ; 37 import javax.naming.NamingException ; 38 import javax.naming.NotContextException ; 39 import javax.naming.Reference ; 40 import javax.naming.spi.ResolveResult ; 41 42 import org.jnp.interfaces.Naming; 43 import org.jnp.interfaces.NamingContext; 44 import org.jnp.interfaces.NamingParser; 45 import org.jboss.logging.Logger; 46 47 55 public class NamingServer 56 implements Naming, java.io.Serializable 57 { 58 private static Logger log = Logger.getLogger(NamingServer.class); 59 60 61 private static final long serialVersionUID = 4183855539507934373L; 62 64 66 protected Hashtable table = new Hashtable (); 67 protected Name prefix; 68 protected NamingParser parser = new NamingParser(); 69 protected NamingServer parent; 70 71 73 public NamingServer() 75 throws NamingException 76 { 77 this(null, null); 78 } 79 80 public NamingServer(Name prefix, NamingServer parent) 81 throws NamingException 82 { 83 if (prefix == null) prefix = parser.parse(""); 84 this.prefix = prefix; 85 86 this.parent = parent; 87 } 88 89 91 public synchronized void bind(Name name, Object obj, String className) 93 throws NamingException 94 { 95 if (name.isEmpty()) 96 { 97 throw new InvalidNameException (); 99 } else if (name.size() > 1) 100 { 101 104 Object ctx = getObject(name); 105 if (ctx != null) 106 { 107 if (ctx instanceof NamingServer) 108 { 109 ((NamingServer)ctx).bind(name.getSuffix(1),obj, className); 110 } else if (ctx instanceof Reference ) 111 { 112 if (((Reference )ctx).get("nns") != null) 114 { 115 CannotProceedException cpe = new CannotProceedException (); 116 cpe.setResolvedObj(ctx); 117 cpe.setRemainingName(name.getSuffix(1)); 118 throw cpe; 119 } else 120 { 121 throw new NotContextException (); 122 } 123 } else 124 { 125 throw new NotContextException (); 126 } 127 } else 128 { 129 throw new NameNotFoundException (); 130 } 131 } else 132 { 133 if (name.get(0).equals("")) 135 { 136 throw new InvalidNameException (); 137 } else 138 { 139 try 141 { 142 getBinding(name); 143 throw new NameAlreadyBoundException (); 145 } catch (NameNotFoundException e) 146 { 147 setBinding(name,obj,className); 148 } 149 } 150 } 151 } 152 153 public synchronized void rebind(Name name, Object obj, String className) 154 throws NamingException 155 { 156 if (name.isEmpty()) 157 { 158 throw new InvalidNameException (); 160 } else if (name.size() > 1) 161 { 162 165 Object ctx = getObject(name); 166 if (ctx instanceof NamingServer) 167 { 168 ((NamingServer)ctx).rebind(name.getSuffix(1),obj, className); 169 } else if (ctx instanceof Reference ) 170 { 171 if (((Reference )ctx).get("nns") != null) 173 { 174 CannotProceedException cpe = new CannotProceedException (); 175 cpe.setResolvedObj(ctx); 176 cpe.setRemainingName(name.getSuffix(1)); 177 throw cpe; 178 } else 179 { 180 throw new NotContextException (); 181 } 182 } else 183 { 184 throw new NotContextException (); 185 } 186 } else 187 { 188 if (name.get(0).equals("")) 190 { 191 throw new InvalidNameException (); 192 } else 193 { 194 setBinding(name,obj,className); 196 } 197 } 198 } 199 200 public synchronized void unbind(Name name) 201 throws NamingException 202 { 203 if (name.isEmpty()) 204 { 205 throw new InvalidNameException (); 207 } else if (name.size() > 1) 208 { 209 212 Object ctx = getObject(name); 213 if (ctx instanceof NamingServer) 214 { 215 ((NamingServer)ctx).unbind(name.getSuffix(1)); 216 } else if (ctx instanceof Reference ) 217 { 218 if (((Reference )ctx).get("nns") != null) 220 { 221 CannotProceedException cpe = new CannotProceedException (); 222 cpe.setResolvedObj(ctx); 223 cpe.setRemainingName(name.getSuffix(1)); 224 throw cpe; 225 } else 226 { 227 throw new NotContextException (); 228 } 229 } else 230 { 231 throw new NotContextException (); 232 } 233 } else 234 { 235 if (name.get(0).equals("")) 237 { 238 throw new InvalidNameException (); 239 } else 240 { 241 if (getBinding(name) != null) 243 { 244 removeBinding(name); 245 } else 246 { 247 throw new NameNotFoundException (); 248 } 249 } 250 } 251 } 252 253 public Object lookup(Name name) 255 throws NamingException 256 { 257 Object result; 258 if (name.isEmpty()) 259 { 260 result = new NamingContext(null, (Name )(prefix.clone()), getRoot()); 262 } else if (name.size() > 1) 263 { 264 267 Object ctx = getObject(name); 268 if (ctx instanceof NamingServer) 269 { 270 result = ((NamingServer)ctx).lookup(name.getSuffix(1)); 271 } else if (ctx instanceof Reference ) 272 { 273 if (((Reference )ctx).get("nns") != null) 275 { 276 CannotProceedException cpe = new CannotProceedException (); 277 cpe.setResolvedObj(ctx); 278 cpe.setRemainingName(name.getSuffix(1)); 279 throw cpe; 280 } 281 282 result = new ResolveResult (ctx, name.getSuffix(1)); 283 } else 284 { 285 throw new NotContextException (); 286 } 287 } else 288 { 289 if (name.get(0).equals("")) 291 { 292 result = new NamingContext(null, prefix, getRoot()); 293 } else 294 { 295 Object res = getObject(name); 297 298 if (res instanceof NamingServer) 299 { 300 Name fullName = (Name )(prefix.clone()); 301 fullName.addAll(name); 302 result = new NamingContext(null, fullName, getRoot()); 303 } 304 else 305 result = res; 306 } 307 } 308 309 return result; 310 } 311 312 public Collection list(Name name) 313 throws NamingException 314 { 315 if (name.isEmpty()) 317 { 318 320 Vector list = new Vector (); 321 Enumeration keys = table.keys(); 322 while (keys.hasMoreElements()) 323 { 324 String key = (String )keys.nextElement(); 325 Binding b = getBinding(key); 326 327 list.addElement(new NameClassPair (b.getName(),b.getClassName(),true)); 328 } 329 return list; 330 } else 331 { 332 334 Object ctx = getObject(name); 335 if (ctx instanceof NamingServer) 336 { 337 return ((NamingServer)ctx).list(name.getSuffix(1)); 338 } else if (ctx instanceof Reference ) 339 { 340 if (((Reference )ctx).get("nns") != null) 342 { 343 CannotProceedException cpe = new CannotProceedException (); 344 cpe.setResolvedObj(ctx); 345 cpe.setRemainingName(name.getSuffix(1)); 346 throw cpe; 347 } else 348 { 349 throw new NotContextException (); 350 } 351 } else 352 { 353 throw new NotContextException (); 354 } 355 } 356 } 357 358 public Collection listBindings(Name name) 359 throws NamingException 360 { 361 if (name.isEmpty()) 362 { 363 Collection bindings = table.values(); 364 Collection newBindings = new Vector (bindings.size()); 365 Iterator iter = bindings.iterator(); 366 while (iter.hasNext()) 367 { 368 Binding b = (Binding )iter.next(); 369 if (b.getObject() instanceof NamingServer) 370 { 371 Name n = (Name )prefix.clone(); 372 n.add(b.getName()); 373 newBindings.add(new Binding (b.getName(), 374 b.getClassName(), 375 new NamingContext(null, n, getRoot()))); 376 } else 377 { 378 newBindings.add(b); 379 } 380 } 381 382 return newBindings; 383 } else 384 { 385 Object ctx = getObject(name); 386 if (ctx instanceof NamingServer) 387 { 388 return ((NamingServer)ctx).listBindings(name.getSuffix(1)); 389 } else if (ctx instanceof Reference ) 390 { 391 if (((Reference )ctx).get("nns") != null) 393 { 394 CannotProceedException cpe = new CannotProceedException (); 395 cpe.setResolvedObj(ctx); 396 cpe.setRemainingName(name.getSuffix(1)); 397 throw cpe; 398 } else 399 { 400 throw new NotContextException (); 401 } 402 } else 403 { 404 throw new NotContextException (); 405 } 406 } 407 } 408 409 public Context createSubcontext(Name name) 410 throws NamingException 411 { 412 if( name.size() == 0 ) 413 throw new InvalidNameException ("Cannot pass an empty name to createSubcontext"); 414 415 NamingException ex = null; 416 Context subCtx = null; 417 if (name.size() > 1) 418 { 419 Object ctx = getObject(name); 420 if (ctx != null) 421 { 422 Name subCtxName = name.getSuffix(1); 423 if (ctx instanceof NamingServer) 424 { 425 subCtx = ((NamingServer)ctx).createSubcontext(subCtxName); 426 } 427 else if (ctx instanceof Reference ) 428 { 429 if (((Reference )ctx).get("nns") != null) 431 { 432 CannotProceedException cpe = new CannotProceedException (); 433 cpe.setResolvedObj(ctx); 434 cpe.setRemainingName(subCtxName); 435 throw cpe; 436 } 437 else 438 { 439 ex = new NotContextException (); 440 ex.setResolvedName(name.getPrefix(0)); 441 ex.setRemainingName(subCtxName); 442 throw ex; 443 } 444 } 445 else 446 { 447 ex = new NotContextException (); 448 ex.setResolvedName(name.getPrefix(0)); 449 ex.setRemainingName(subCtxName); 450 throw ex; 451 } 452 } 453 else 454 { 455 ex = new NameNotFoundException (); 456 ex.setRemainingName(name); 457 throw ex; 458 } 459 } 460 else 461 { 462 Object binding = table.get(name.get(0)); 463 if( binding != null ) 464 { 465 ex = new NameAlreadyBoundException (); 466 ex.setResolvedName(prefix); 467 ex.setRemainingName(name); 468 throw ex; 469 } 470 else 471 { 472 Name fullName = (Name ) prefix.clone(); 473 fullName.addAll(name); 474 NamingServer subContext = new NamingServer(fullName, this); 475 setBinding(name, subContext, NamingContext.class.getName()); 476 subCtx = new NamingContext(null, fullName, getRoot()); 477 } 478 } 479 return subCtx; 480 } 481 482 public Naming getRoot() 483 { 484 if (parent == null) 485 return this; 486 else 487 return parent.getRoot(); 488 } 489 490 492 494 496 private void setBinding(Name name, Object obj, String className) 498 { 499 String n = name.toString(); 500 table.put(n, new Binding (n, className, obj, true)); 501 } 502 503 private Binding getBinding(String key) 504 throws NameNotFoundException 505 { 506 Binding b = (Binding )table.get(key); 507 if (b == null) 508 { 509 if( log.isTraceEnabled() ) 510 { 511 StringBuffer tmp = new StringBuffer ("No binding for: "+key); 512 tmp.append(" in context "); 513 tmp.append(this.prefix); 514 tmp.append(", bindings:\n"); 515 Iterator bindings = table.values().iterator(); 516 while( bindings.hasNext() ) 517 { 518 Binding value = (Binding ) bindings.next(); 519 tmp.append(value.getName()); 520 tmp.append('='); 521 if( value.getObject() != null ) 522 tmp.append(value.getObject().toString()); 523 else 524 tmp.append("null"); 525 tmp.append('\n'); 526 } 527 log.trace(tmp.toString()); 528 } 529 throw new NameNotFoundException (key + " not bound"); 530 } 531 return b; 532 } 533 534 private Binding getBinding(Name key) 535 throws NameNotFoundException 536 { 537 return getBinding(key.get(0)); 538 } 539 540 private Object getObject(Name key) 541 throws NameNotFoundException 542 { 543 return getBinding(key).getObject(); 544 } 545 546 private void removeBinding(Name name) 547 { 548 table.remove(name.get(0)); 549 } 550 551 } 552 | Popular Tags |