1 6 package org.jfox.jndi.enc; 7 8 import java.util.Enumeration ; 9 import java.util.Hashtable ; 10 import java.util.Properties ; 11 import javax.naming.Binding ; 12 import javax.naming.CompositeName ; 13 import javax.naming.CompoundName ; 14 import javax.naming.Context ; 15 import javax.naming.InvalidNameException ; 16 import javax.naming.Name ; 17 import javax.naming.NameAlreadyBoundException ; 18 import javax.naming.NameClassPair ; 19 import javax.naming.NameNotFoundException ; 20 import javax.naming.NameParser ; 21 import javax.naming.NamingEnumeration ; 22 import javax.naming.NamingException ; 23 import javax.naming.NotContextException ; 24 import javax.naming.OperationNotSupportedException ; 25 26 31 32 public class EnterpriseContext implements Context { 33 private Hashtable myEnv; 34 private Hashtable bindings = new Hashtable (); 35 private static NameParser myParser = new FlatNameParser(); 36 37 public Object lookup(String name) throws NamingException { 38 if(name.equals("")) { 39 return (new EnterpriseContext()); 42 } 43 Object answer = bindings.get(name); 44 if(answer == null) { 45 throw new NameNotFoundException (name + " not found"); 46 } 47 return answer; 48 } 49 50 public Object lookup(Name name) throws NamingException { 51 return lookup(name.toString()); 53 } 54 55 public void bind(String name, Object obj) throws NamingException { 56 if(name.equals("")) { 57 throw new InvalidNameException ("Cannot bind empty name"); 58 } 59 if(bindings.get(name) != null) { 60 throw new NameAlreadyBoundException ("Use rebind to override"); 61 } 62 bindings.put(name, obj); 63 } 64 65 public void bind(Name name, Object obj) throws NamingException { 66 bind(name.toString(), obj); 68 } 69 70 public void rebind(String name, Object obj) throws NamingException { 71 if(name.equals("")) { 72 throw new InvalidNameException ("Cannot bind empty name"); 73 } 74 bindings.put(name, obj); 75 } 76 77 public void rebind(Name name, Object obj) throws NamingException { 78 rebind(name.toString(), obj); 80 } 81 82 public void unbind(String name) throws NamingException { 83 if(name.equals("")) { 84 throw new InvalidNameException ("Cannot unbind empty name"); 85 } 86 bindings.remove(name); 87 } 88 89 public void unbind(Name name) throws NamingException { 90 unbind(name.toString()); 92 } 93 94 public void rename(String oldname, String newname) 95 throws NamingException { 96 if(oldname.equals("") || newname.equals("")) { 97 throw new InvalidNameException ("Cannot rename empty name"); 98 } 99 if(bindings.get(newname) != null) { 101 throw new NameAlreadyBoundException (newname + 102 " is already bound"); 103 } 104 Object oldBinding = bindings.remove(oldname); 106 if(oldBinding == null) { 107 throw new NameNotFoundException (oldname + " not bound"); 108 } 109 bindings.put(newname, oldBinding); 110 } 111 112 public void rename(Name oldname, Name newname) 113 throws NamingException { 114 rename(oldname.toString(), newname.toString()); 116 } 117 118 public NamingEnumeration list(String name) 119 throws NamingException { 120 if(name.equals("")) { 121 return new FlatNames(bindings.keys()); 123 } 124 Object target = lookup(name); 126 if(target instanceof Context ) { 127 return ((Context ) target).list(""); 128 } 129 throw new NotContextException (name + " cannot be listed"); 130 } 131 132 public NamingEnumeration list(Name name) 133 throws NamingException { 134 return list(name.toString()); 136 } 137 138 public NamingEnumeration listBindings(String name) 139 throws NamingException { 140 if(name.equals("")) { 141 return new FlatBindings(bindings.keys()); 143 } 144 Object target = lookup(name); 146 if(target instanceof Context ) { 147 return ((Context ) target).listBindings(""); 148 } 149 throw new NotContextException (name + " cannot be listed"); 150 } 151 152 public NamingEnumeration listBindings(Name name) 153 throws NamingException { 154 return listBindings(name.toString()); 156 } 157 158 public void destroySubcontext(String name) throws NamingException { 159 throw new OperationNotSupportedException ("FlatCtx does not support subcontexts"); 160 } 161 162 public void destroySubcontext(Name name) throws NamingException { 163 destroySubcontext(name.toString()); 165 } 166 167 public Context createSubcontext(String name) 168 throws NamingException { 169 throw new OperationNotSupportedException ("FlatCtx does not support subcontexts"); 170 } 171 172 public Context createSubcontext(Name name) throws NamingException { 173 return createSubcontext(name.toString()); 175 } 176 177 public Object lookupLink(String name) throws NamingException { 178 return lookup(name); 180 } 181 182 public Object lookupLink(Name name) throws NamingException { 183 return lookupLink(name.toString()); 185 } 186 187 public NameParser getNameParser(String name) 188 189 throws NamingException { 190 return myParser; 191 } 192 193 public NameParser getNameParser(Name name) throws NamingException { 194 return getNameParser(name.toString()); 196 } 197 198 public String composeName(String name, String prefix) 199 throws NamingException { 200 Name result = composeName(new CompositeName (name), 201 new CompositeName (prefix)); 202 return result.toString(); 203 } 204 205 public Name composeName(Name name, Name prefix) 206 throws NamingException { 207 Name result = (Name ) (prefix.clone()); 208 result.addAll(name); 209 return result; 210 } 211 212 public Object addToEnvironment(String propName, Object propVal) 213 throws NamingException { 214 if(myEnv == null) { 215 myEnv = new Hashtable (5, 0.75f); 216 } 217 return myEnv.put(propName, propVal); 218 } 219 220 public Object removeFromEnvironment(String propName) 221 throws NamingException { 222 if(myEnv == null) 223 return null; 224 return myEnv.remove(propName); 225 } 226 227 public Hashtable getEnvironment() throws NamingException { 228 if(myEnv == null) { 229 return new Hashtable (3, 0.75f); 231 } 232 else { 233 return (Hashtable ) myEnv.clone(); 234 } 235 } 236 237 public String getNameInNamespace() throws NamingException { 238 return ""; 239 } 240 241 public void close() throws NamingException { 242 myEnv = null; 243 bindings = null; 244 } 245 class FlatNames implements NamingEnumeration { 247 Enumeration names; 248 249 FlatNames(Enumeration names) { 250 this.names = names; 251 } 252 253 public boolean hasMoreElements() { 254 return names.hasMoreElements(); 255 } 256 257 public boolean hasMore() throws NamingException { 258 return hasMoreElements(); 259 } 260 261 public Object nextElement() { 262 String name = (String ) names.nextElement(); 263 String className = bindings.get(name).getClass().getName(); 264 return new NameClassPair (name, className); 265 } 266 267 public Object next() throws NamingException { 268 return nextElement(); 269 } 270 271 public void close() { 272 } 273 } 274 275 class FlatBindings implements NamingEnumeration { 277 Enumeration names; 278 279 FlatBindings(Enumeration names) { 280 this.names = names; 281 } 282 283 public boolean hasMoreElements() { 284 return names.hasMoreElements(); 285 } 286 287 public boolean hasMore() throws NamingException { 288 return hasMoreElements(); 289 } 290 291 public Object nextElement() { 292 String name = (String ) names.nextElement(); 293 return new Binding (name, bindings.get(name)); 294 } 295 296 public Object next() throws NamingException { 297 return nextElement(); 298 } 299 300 public void close() { 301 } 302 } 303 304 static class FlatNameParser implements NameParser { 305 static Properties syntax = new Properties (); 306 307 static { 308 syntax.put("jndi.syntax.direction", "flat"); 309 syntax.put("jndi.syntax.ignorecase", "false"); 310 } 311 312 public Name parse(String name) throws NamingException { 313 return new CompoundName (name, syntax); 314 } 315 } 316 } | Popular Tags |