1 22 package org.jboss.test.security.ejb.project.support; 23 24 import java.io.Serializable ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import javax.naming.Binding ; 30 import javax.naming.CompositeName ; 31 import javax.naming.Context ; 32 import javax.naming.InvalidNameException ; 33 import javax.naming.Name ; 34 import javax.naming.NameAlreadyBoundException ; 35 import javax.naming.NamingEnumeration ; 36 import javax.naming.NamingException ; 37 import javax.naming.NameNotFoundException ; 38 import javax.naming.NameParser ; 39 import javax.naming.NotContextException ; 40 import javax.naming.OperationNotSupportedException ; 41 import javax.naming.directory.Attribute ; 42 import javax.naming.directory.Attributes ; 43 import javax.naming.directory.BasicAttributes ; 44 import javax.naming.directory.DirContext ; 45 import javax.naming.directory.ModificationItem ; 46 import javax.naming.directory.SearchControls ; 47 48 54 public class HeirMemoryMap extends DirContextStringImpl implements DirContext , Serializable 55 { 56 private static NameParser nameParser = DefaultName.getNameParser(); 57 private HashMap bindings = new HashMap (); 58 private HashMap bindingAttrs = new HashMap (); 59 private HeirMemoryMap parent; 60 private String contextName; 61 private Hashtable env; 62 63 64 public HeirMemoryMap() 65 { 66 this.contextName = ""; 67 } 68 public HeirMemoryMap(String contextName, HeirMemoryMap parent, Attributes attributes) throws NamingException 69 { 70 this(contextName, parent, attributes, null); 71 } 72 public HeirMemoryMap(String contextName, HeirMemoryMap parent, Attributes attributes, Hashtable env) throws NamingException 73 { 74 this.contextName = contextName == null ? "" : contextName; 75 this.parent = parent; 76 bindingAttrs.put("", attributes.clone()); 77 if( parent != null ) 78 parent.bind(contextName, this); 79 this.env = env; 80 } 81 82 public String toString() 83 { 84 Name name = null; 85 try 86 { 87 name = getFullName(); 88 } 89 catch(NamingException e) 90 { 91 } 92 return name.toString(); 93 } 94 95 String getName() 96 { 97 return contextName; 98 } 99 void setName(String contextName) 100 { 101 this.contextName = contextName; 102 } 103 Name getFullName() throws NamingException 104 { 105 CompositeName name = new CompositeName (getName()); 106 HeirMemoryMap context = parent; 107 if( context == null ) 108 return name; 109 110 try 111 { 112 while( context.parent != null ) 113 { 114 name.add(0, context.getName()); 115 context = context.parent; 116 } 117 } 118 catch(NamingException e) 119 { 120 } 121 return name; 122 } 123 124 public Object addToEnvironment(String p1,Object p2) throws NamingException 126 { 127 return null; 128 } 129 public Object removeFromEnvironment(String p1) throws NamingException 130 { 131 return null; 132 } 133 134 public void bind(Name name, Object value) throws NamingException 135 { 136 bind(name, value, null); 137 } 138 139 public void bind(Name name, Object value, Attributes attributes) throws NamingException 140 { 141 if( name.isEmpty() ) 142 { 143 throw new InvalidNameException ("Cannot bind empty name"); 144 } 145 146 internalBind(name, value, attributes, true); 147 } 148 149 public void close() throws NamingException 150 { 151 } 152 153 public Name composeName(Name p1,Name p2) throws NamingException { 154 return null; 155 } 156 157 public Context createSubcontext(Name name) throws NamingException 158 { 159 return createSubcontext(name, null); 160 } 161 162 public DirContext createSubcontext(Name name, Attributes attributes) throws NamingException 163 { 164 if( name.isEmpty() ) 165 { 166 throw new InvalidNameException ("Cannot createSubcontext with empty name"); 167 } 168 169 DirContext subctx = null; 170 String atom = name.get(0); 171 if( name.size() == 1 ) 172 { 173 subctx = new HeirMemoryMap(atom, this, attributes, env); 174 } 175 else 176 { 177 DirContext context = (DirContext ) bindings.get(atom); 178 subctx = context.createSubcontext(name.getSuffix(1), attributes); 179 } 180 181 return subctx; 182 } 183 184 public void destroySubcontext(Name name) throws NamingException 185 { 186 unbind(name); 187 } 188 189 public Attributes getAttributes(Name name) throws NamingException 190 { 191 return getAttributes(name, null); 192 } 193 194 public Attributes getAttributes(Name name, String [] attrIDs) throws NamingException 195 { 196 Attributes nameAttributes = null; 197 String atom = name.get(0); 198 if( name.isEmpty() == true ) 199 { 200 nameAttributes = (Attributes ) bindingAttrs.get(""); 201 } 202 else if( name.size() == 1 ) 203 { 204 Object binding = bindings.get(atom); 205 if( binding != null ) 206 { 207 if( binding instanceof DirContext ) 208 { 209 DirContext dirCtx = (DirContext ) binding; 210 return dirCtx.getAttributes(name.getSuffix(1), attrIDs); 211 } 212 } 213 nameAttributes = (Attributes ) bindingAttrs.get(atom); 214 } 215 else 216 { 217 DirContext context = (DirContext ) bindings.get(atom); 218 nameAttributes = context.getAttributes(name.getSuffix(1), attrIDs); 219 } 220 221 if( nameAttributes != null && attrIDs != null ) 222 { 223 BasicAttributes matches = new BasicAttributes (nameAttributes.isCaseIgnored()); 224 for(int a = 0; a < attrIDs.length; a ++) 225 { 226 Attribute attr = nameAttributes.get(attrIDs[a]); 227 if( attr != null ) 228 matches.put(attr); 229 } 230 nameAttributes = matches; 231 } 232 return nameAttributes; 233 } 234 235 public java.util.Hashtable getEnvironment() throws NamingException 236 { 237 return env; 238 } 239 240 public String getNameInNamespace() throws NamingException 241 { 242 return toString(); 243 } 244 245 public NameParser getNameParser(Name p1) throws NamingException 246 { 247 return nameParser; 248 } 249 250 public DirContext getSchema(Name p1) throws NamingException 251 { 252 throw new OperationNotSupportedException ("Not implemented yet"); 253 } 254 255 public DirContext getSchemaClassDefinition(Name p1) throws NamingException 256 { 257 throw new OperationNotSupportedException ("Not implemented yet"); 258 } 259 260 public NamingEnumeration list(Name p1) throws NamingException 261 { 262 return null; 263 } 264 265 public NamingEnumeration listBindings(Name name) throws NamingException 266 { 267 NamingEnumeration iter = null; 268 269 if( name.isEmpty() == true ) 270 { 271 Iterator keys = bindings.keySet().iterator(); 272 ArrayList tmp = new ArrayList (); 273 while( keys.hasNext() ) 274 { 275 String key = (String ) keys.next(); 276 Object value = bindings.get(key); 277 Attributes attributes = (Attributes ) bindingAttrs.get(key); 278 DirBinding tuple = new DirBinding(key, value, attributes); 279 tmp.add(tuple); 280 } 281 iter = new NameBindingIterator(tmp.iterator(), this); 282 } 283 else 284 { 285 String atom = name.get(0); 286 Context context = (Context ) bindings.get(atom); 287 iter = context.listBindings(name.getSuffix(1)); 288 } 289 290 return iter; 291 } 292 293 public Object lookup(Name name) throws NamingException 294 { 295 if( name.isEmpty() == true ) 296 return this; 297 298 String atom = name.get(0); 299 Object binding = bindings.get(atom); 300 if( name.size() == 1 ) 301 { 304 if( binding == null && bindings.containsKey(atom) == false ) 305 { 306 NameNotFoundException e = new NameNotFoundException ("Failed to find: "+atom); 307 e.setRemainingName(name); 308 e.setResolvedObj(this); 309 throw e; 310 } 311 } 312 else if( (binding instanceof Context ) ) 313 { 314 Context context = (Context ) binding; 315 binding = context.lookup(name.getSuffix(1)); 316 } 317 else 318 { 319 NotContextException e = new NotContextException (atom + " does not name a directory context that supports attributes"); 320 e.setRemainingName(name); 321 e.setResolvedObj(binding); 322 throw e; 323 } 324 return binding; 325 } 326 327 public Object lookupLink(Name p1) throws NamingException 328 { 329 throw new OperationNotSupportedException ("Not implemented yet"); 330 } 331 332 public void modifyAttributes(Name p1,ModificationItem [] p2) throws NamingException 333 { 334 throw new OperationNotSupportedException ("Not implemented yet"); 335 } 336 337 public void modifyAttributes(Name p1,int p2,Attributes p3) throws NamingException 338 { 339 throw new OperationNotSupportedException ("Not implemented yet"); 340 } 341 342 public void rebind(Name name, Object value) throws NamingException 343 { 344 rebind(name, value, null); 345 } 346 347 public void rebind(Name name, Object value, Attributes attributes) throws NamingException 348 { 349 if( name.isEmpty() ) 350 { 351 throw new InvalidNameException ("Cannot bind empty name"); 352 } 353 354 internalBind(name, value, attributes, false); 355 } 356 357 public void rename(Name p1,Name p2) throws NamingException 358 { 359 throw new OperationNotSupportedException ("Not implemented yet"); 360 } 361 362 public NamingEnumeration search(Name p1,Attributes p2) throws NamingException 363 { 364 throw new OperationNotSupportedException ("Not implemented yet"); 365 } 366 367 public NamingEnumeration search(Name p1,String p2,SearchControls p3) throws NamingException 368 { 369 throw new OperationNotSupportedException ("Not implemented yet"); 370 } 371 372 public NamingEnumeration search(Name p1,Attributes p2,String [] p3) throws NamingException 373 { 374 throw new OperationNotSupportedException ("Not implemented yet"); 375 } 376 377 public NamingEnumeration search(Name p1,String p2,Object [] p3,SearchControls p4) throws NamingException 378 { 379 throw new OperationNotSupportedException ("Not implemented yet"); 380 } 381 382 public void unbind(Name name) throws NamingException 383 { 384 if( name.isEmpty() ) 385 { 386 throw new InvalidNameException ("Cannot unbind empty name"); 387 } 388 389 String atom = name.get(0); 390 Object binding = bindings.get(atom); 391 if( name.size() == 1 ) 392 { 395 if( binding == null && bindings.containsKey(atom) == false ) 396 { 397 NameNotFoundException e = new NameNotFoundException ("Failed to find: "+atom); 398 e.setRemainingName(name); 399 e.setResolvedObj(this); 400 throw e; 401 } 402 bindings.remove(atom); 403 bindingAttrs.remove(atom); 404 } 405 else if( (binding instanceof Context ) ) 406 { 407 Context context = (Context ) binding; 408 context.unbind(name.getSuffix(1)); 409 } 410 else 411 { 412 NotContextException e = new NotContextException (atom + " does not name a directory context that supports attributes"); 413 e.setRemainingName(name); 414 e.setResolvedObj(binding); 415 throw e; 416 } 417 } 418 420 private void internalBind(Name name, Object value, Attributes attributes, boolean isBind) throws NamingException 421 { 422 String atom = name.get(0); 423 Object binding = bindings.get(atom); 424 425 if( name.size() == 1 ) 426 { 427 if( binding != null && isBind == false ) 428 { 429 throw new NameAlreadyBoundException ("Use rebind to override"); 430 } 431 432 bindings.put(atom, value); 434 435 if( attributes != null ) 437 { 438 bindingAttrs.put(atom, attributes); 439 } 440 } 441 else 442 { 443 if( (binding instanceof Context ) == false ) 445 { 446 NotContextException e = new NotContextException (atom + " does not name a context"); 447 e.setRemainingName(name); 448 e.setResolvedObj(binding); 449 throw e; 450 } 451 452 if( attributes == null ) 453 { 454 Context context = (Context ) binding; 455 if( isBind == true ) 456 context.bind(name.getSuffix(1), value); 457 else 458 context.rebind(name.getSuffix(1), value); 459 } 460 else if( (binding instanceof DirContext ) == false ) 461 { 462 NotContextException e = new NotContextException (atom + " does not name a directory context that supports attributes"); 463 e.setRemainingName(name); 464 e.setResolvedObj(binding); 465 throw e; 466 } 467 else 468 { 469 DirContext context = (DirContext ) binding; 470 if( isBind == true ) 471 context.bind(name.getSuffix(1), value, attributes); 472 else 473 context.rebind(name.getSuffix(1), value, attributes); 474 } 475 } 476 } 477 } 478 | Popular Tags |