1 package org.sapia.archie.jndi; 2 3 import java.util.Hashtable ; 4 import java.util.Iterator ; 5 6 import javax.naming.Context ; 7 import javax.naming.Name ; 8 import javax.naming.NameAlreadyBoundException ; 9 import javax.naming.NameNotFoundException ; 10 import javax.naming.NameParser ; 11 import javax.naming.NamingEnumeration ; 12 import javax.naming.NamingException ; 13 14 import org.sapia.archie.Archie; 15 import org.sapia.archie.DuplicateException; 16 import org.sapia.archie.Node; 17 import org.sapia.archie.NotFoundException; 18 import org.sapia.archie.ProcessingException; 19 20 28 public class JndiContext implements Context { 29 30 private Archie _archie; 31 private Hashtable _env = new Hashtable (); 32 private NameParser _parser; 33 34 public JndiContext(Node node){ 35 _archie = new Archie(node); 36 _parser = new JndiNameParser(node.getNameParser()); 37 } 38 39 40 43 public Object addToEnvironment(String propName, Object propVal) 44 throws NamingException { 45 return _env.put(propName, propVal); 46 } 47 48 51 public synchronized void bind(Name name, Object obj) throws NamingException { 52 try{ 53 _archie.bind(getNameFrom(name), obj); 54 }catch(ProcessingException e){ 55 throw getNamingException("Could not bind under '" + name + "'", e); 56 } 57 } 58 59 62 public synchronized void bind(String name, Object obj) throws NamingException { 63 try{ 64 _archie.bind(_archie.getNameParser().parse(name), obj); 65 }catch(ProcessingException e){ 66 throw getNamingException("Could not bind under '" + name + "'", e); 67 } 68 } 69 70 73 public void close() throws NamingException { 74 } 75 76 79 public Name composeName(Name name, Name prefix) throws NamingException { 80 org.sapia.archie.Name toAdd = getNameFrom(name); 81 org.sapia.archie.Name prfx = getNameFrom(prefix); 82 return new JndiName(prfx.add(toAdd)); 83 } 84 85 88 public String composeName(String name, String prefix) 89 throws NamingException { 90 try{ 91 org.sapia.archie.Name toAdd = _archie.getNameParser().parse(name); 92 org.sapia.archie.Name prfx = _archie.getNameParser().parse(prefix); 93 return _archie.getNameParser().asString(prfx.add(toAdd)); 94 }catch(ProcessingException e){ 95 throw getNamingException("Could not process name", e); 96 } 97 } 98 99 102 public synchronized Context createSubcontext(Name name) throws NamingException { 103 try{ 104 return newChildContext(_archie.lookupNode(getNameFrom(name), true)); 105 }catch(ProcessingException e){ 106 throw getNamingException("Could not create subcontext", e); 107 }catch(NotFoundException e){ 108 throw getNamingException("Could not create subcontext", e); 109 } 110 } 111 112 115 public synchronized Context createSubcontext(String name) throws NamingException { 116 try{ 117 return newChildContext(_archie.lookupNode(_archie.getNameParser().parse(name), true)); 118 }catch(ProcessingException e){ 119 throw getNamingException("Could not create subcontext", e); 120 }catch(NotFoundException e){ 121 throw getNamingException("Could not create subcontext", e); 122 } 123 } 124 125 128 public synchronized void destroySubcontext(Name name) throws NamingException { 129 if(name.size() == 0){ 130 return; 131 } 132 try{ 133 org.sapia.archie.Name archName = getNameFrom(name); 134 Node node = _archie.lookupNode(archName, false); 135 if(node.getParent() != null){ 136 node.getParent().removeChild(archName.last()); 137 } 138 }catch(ProcessingException e){ 139 throw getNamingException("Could not destroy subcontext", e); 140 }catch(NotFoundException e){ 141 throw getNamingException("Context not found", e); 142 } 143 } 144 145 148 public synchronized void destroySubcontext(String name) throws NamingException { 149 try{ 150 org.sapia.archie.Name archName = _archie.getNameParser().parse(name); 151 if(archName.count() == 0) return; 152 Node node = _archie.lookupNode(archName, false); 153 if(node.getParent() != null){ 154 node.getParent().removeChild(archName.last()); 155 } 156 }catch(ProcessingException e){ 157 throw getNamingException("Could not destroy subcontext", e); 158 }catch(NotFoundException e){ 159 throw getNamingException("Context not found", e); 160 } 161 162 } 163 164 167 public Hashtable getEnvironment() throws NamingException { 168 return _env; 169 } 170 171 174 public synchronized String getNameInNamespace() throws NamingException { 175 return _archie.getNameParser().asString(_archie.getRoot().getAbsolutePath()); 176 } 177 178 181 public NameParser getNameParser(Name name) throws NamingException { 182 return _parser; 183 } 184 185 188 public NameParser getNameParser(String name) throws NamingException { 189 return _parser; 190 } 191 192 197 public synchronized NamingEnumeration list(Name name) throws NamingException { 198 try{ 199 Node node = _archie.lookupNode(getNameFrom(name), false); 200 return newNamingEnum(node.getEntries(), node.getChildren(), JndiNamingEnum.LIST_NAMECLASS_PAIRS); 201 }catch(ProcessingException e){ 202 throw getNamingException("Could not list objects under: " + name, e); 203 }catch(NotFoundException e){ 204 throw getNamingException("Could not list objects under: " + name, e); 205 } 206 } 207 208 213 public synchronized NamingEnumeration list(String name) throws NamingException { 214 try{ 215 Node node = _archie.lookupNode(_archie.getNameParser().parse(name), false); 216 return newNamingEnum(node.getEntries(), node.getChildren(), JndiNamingEnum.LIST_NAMECLASS_PAIRS); 217 }catch(ProcessingException e){ 218 throw getNamingException("Could not list objects under: " + name, e); 219 }catch(NotFoundException e){ 220 throw getNamingException("Could not list objects under: " + name, e); 221 } 222 } 223 224 229 public synchronized NamingEnumeration listBindings(Name name) throws NamingException { 230 try{ 231 Node node = _archie.lookupNode(getNameFrom(name), true); 232 return newNamingEnum(node.getEntries(), node.getChildren(), JndiNamingEnum.LIST_BINDINGS); 233 }catch(ProcessingException e){ 234 throw getNamingException("Could not list objects under: " + name, e); 235 }catch(NotFoundException e){ 236 throw getNamingException("Could not list objects under: " + name, e); 237 } 238 } 239 240 245 public synchronized NamingEnumeration listBindings(String name) throws NamingException { 246 try{ 247 Node node = _archie.lookupNode(_archie.getNameParser().parse(name), true); 248 return newNamingEnum(node.getEntries(), node.getChildren(), JndiNamingEnum.LIST_BINDINGS); 249 }catch(ProcessingException e){ 250 throw getNamingException("Could not list objects under: " + name, e); 251 }catch(NotFoundException e){ 252 throw getNamingException("Could not list objects under: " + name, e); 253 } 254 } 255 256 262 public synchronized NamingEnumeration listObjects(Name name) throws NamingException { 263 try{ 264 Node node = _archie.lookupNode(getNameFrom(name), false); 265 return newNamingEnum(node.getEntries(), node.getChildren(), JndiNamingEnum.LIST_OBJECTS); 266 }catch(ProcessingException e){ 267 throw getNamingException("Could not list objects under: " + name, e); 268 }catch(NotFoundException e){ 269 throw getNamingException("Could not list objects under: " + name, e); 270 } 271 } 272 273 279 public synchronized NamingEnumeration listObjects(String name) throws NamingException { 280 try{ 281 Node node = _archie.lookupNode(_archie.getNameParser().parse(name), false); 282 return newNamingEnum(node.getEntries(), node.getChildren(), JndiNamingEnum.LIST_OBJECTS); 283 }catch(ProcessingException e){ 284 throw getNamingException("Could not list objects under: " + name, e); 285 }catch(NotFoundException e){ 286 throw getNamingException("Could not list objects under: " + name, e); 287 } 288 } 289 290 293 public synchronized Object lookup(Name name) throws NamingException { 294 if(name.size() == 0){ 295 return this; 296 } 297 try{ 298 return _archie.lookup(getNameFrom(name)); 299 }catch(ProcessingException e){ 300 throw getNamingException("Could not perform lookup", e); 301 }catch(NotFoundException e){ 302 throw getNamingException("No object found for: " + name, e); 303 } 304 } 305 306 309 public synchronized Object lookup(String name) throws NamingException { 310 if(name == null || name.length() == 0){ 311 return this; 312 } 313 try{ 314 return _archie.lookup(_archie.getNameParser().parse(name)); 315 }catch(ProcessingException e){ 316 throw getNamingException("Could not perform lookup", e); 317 }catch(NotFoundException e){ 318 throw getNamingException("No object found for: " + name, e); 319 } 320 } 321 322 325 public synchronized Object lookupLink(Name name) throws NamingException { 326 return lookup(name); 327 } 328 329 332 public synchronized Object lookupLink(String name) throws NamingException { 333 return lookup(name); 334 } 335 336 339 public synchronized void rebind(Name name, Object obj) throws NamingException { 340 try{ 341 _archie.rebind(getNameFrom(name), obj); 342 }catch(ProcessingException e){ 343 throw getNamingException("Could not bind object for: " + name, e); 344 } 345 } 346 347 350 public synchronized void rebind(String name, Object obj) throws NamingException { 351 try{ 352 _archie.rebind(_archie.getNameParser().parse(name), obj); 353 }catch(ProcessingException e){ 354 throw getNamingException("Could not bind object for: " + name, e); 355 } 356 } 357 358 361 public Object removeFromEnvironment(String propName) throws NamingException { 362 return _env.remove(propName); 363 } 364 365 369 public void rename(Name oldName, Name newName) throws NamingException { 370 throw new NamingException ("Rename not supported"); 371 } 372 373 378 public void rename(String oldName, String newName) throws NamingException { 379 throw new NamingException ("Rename not supported"); 380 } 381 382 385 public synchronized void unbind(Name name) throws NamingException { 386 try{ 387 _archie.unbind(getNameFrom(name)); 388 }catch(ProcessingException e){ 389 throw getNamingException("Could not unbind: " + name, e); 390 } 391 } 392 393 396 public synchronized void unbind(String name) throws NamingException { 397 try{ 398 _archie.unbind(_archie.getNameParser().parse(name)); 399 }catch(ProcessingException e){ 400 throw getNamingException("Could not unbind: " + name, e); 401 } 402 } 403 404 protected org.sapia.archie.Name getNameFrom(Name name) throws NamingException { 405 org.sapia.archie.Name n = new org.sapia.archie.Name(); 406 for(int i = 0; i < name.size(); i++){ 407 try{ 408 n.add(_archie.getNameParser().parseNamePart((name.get(i)))); 409 }catch(ProcessingException e){ 410 throw getNamingException("Could not process name: " + name, e); 411 } 412 } 413 return n; 414 } 415 416 protected Context newChildContext(Node node){ 417 return new JndiContext(node); 418 } 419 420 429 protected NamingEnumeration newNamingEnum(Iterator entries, Iterator childNodes, int listType){ 430 return new JndiNamingEnum(entries, childNodes, listType); 431 } 432 433 private NamingException getNamingException(String msg, ProcessingException e){ 434 if(e instanceof DuplicateException){ 435 NameAlreadyBoundException nabe = new NameAlreadyBoundException (msg); 436 nabe.setRootCause(e); 437 return nabe; 438 } 439 else{ 440 NamingException ne = new NamingException (msg); 441 ne.setRootCause(e); 442 return ne; 443 } 444 } 445 private NamingException getNamingException(String msg, NotFoundException e){ 446 NamingException ne = new NameNotFoundException (msg); 447 ne.setRootCause(e); 448 ne.setResolvedName(new JndiName(e.getResolvedName())); 449 ne.setRemainingName(new JndiName(e.getRemainingName())); 450 return ne; 451 } 452 } 453 | Popular Tags |