1 10 11 package org.mule.impl.jndi; 12 13 import javax.naming.Binding ; 14 import javax.naming.CompositeName ; 15 import javax.naming.Context ; 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.OperationNotSupportedException ; 24 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.jar.Attributes ; 30 31 35 public class SimpleContext implements Context 36 { 37 38 Map bindings = new HashMap (); 39 40 41 private Hashtable environment; 42 43 public SimpleContext() 44 { 45 super(); 46 } 47 48 public Object lookupLink(Name name) throws NamingException 49 { 50 return null; 52 } 53 54 public void rename(Name oldName, Name newName) throws NamingException 55 { 56 } 58 59 public NameParser getNameParser(Name name) throws NamingException 60 { 61 return null; 63 } 64 65 public NamingEnumeration list(Name name) throws NamingException 66 { 67 return null; 69 } 70 71 public Object lookup(Name name) throws NamingException 72 { 73 return lookup(name.toString()); 74 } 75 76 public Object lookup(String name) throws NamingException 77 { 78 Object rc = bindings.get(name); 79 if (rc == null) 80 { 81 throw new NameNotFoundException (name); 82 } 83 return rc; 84 } 85 86 public void bind(Name name, Object obj) throws NamingException 87 { 88 bind(name.toString(), obj); 89 } 90 91 public void bind(String name, Object obj) throws NamingException 92 { 93 if (bindings.containsKey(name)) 94 { 95 throw new NameAlreadyBoundException (name); 96 } 97 bindings.put(name, obj); 98 } 99 100 public void rebind(Name name, Object obj) throws NamingException 101 { 102 rebind(name.toString(), obj); 103 } 104 105 public void rebind(String name, Object obj) throws NamingException 106 { 107 bindings.put(name, obj); 108 } 109 110 public void unbind(Name name) throws NamingException 111 { 112 unbind(name.toString()); 113 } 114 115 public void unbind(String name) throws NamingException 116 { 117 if (bindings.remove(name) == null) 118 { 119 throw new NameNotFoundException (name); 120 } 121 } 122 123 public void rename(Attributes.Name oldName, Attributes.Name newName) throws NamingException 124 { 125 rename(oldName.toString(), newName.toString()); 126 } 127 128 public void rename(String oldName, String newName) throws NamingException 129 { 130 if (!bindings.containsKey(oldName)) 131 { 132 throw new NameNotFoundException (oldName); 133 } 134 if (bindings.containsKey(newName)) 135 { 136 throw new NameAlreadyBoundException (newName); 137 } 138 139 bindings.put(newName, bindings.remove(oldName)); 140 } 141 142 public NamingEnumeration list(Attributes.Name name) throws NamingException 143 { 144 return list(name.toString()); 145 } 146 147 public NamingEnumeration list(String name) throws NamingException 148 { 149 if (name.length() > 0) 150 { 151 throw new OperationNotSupportedException ("subcontexts not supported"); 152 } 153 final Iterator i = bindings.entrySet().iterator(); 154 return new NamingEnumeration () 155 { 156 public Object next() 157 { 158 Map.Entry e = (Map.Entry )i.next(); 159 return new NameClassPair ((String )e.getKey(), e.getValue().getClass().getName()); 160 } 161 162 public boolean hasMore() 163 { 164 return i.hasNext(); 165 } 166 167 public void close() 168 { 169 } 171 172 public boolean hasMoreElements() 173 { 174 return hasMore(); 175 } 176 177 public Object nextElement() 178 { 179 return next(); 180 } 181 }; 182 } 183 184 public NamingEnumeration listBindings(Name name) throws NamingException 185 { 186 return listBindings(name.toString()); 187 } 188 189 public NamingEnumeration listBindings(String name) throws NamingException 190 { 191 if (name.length() > 0) 192 { 193 throw new OperationNotSupportedException ("subcontexts not supported"); 194 } 195 final Iterator i = bindings.entrySet().iterator(); 196 return new NamingEnumeration () 197 { 198 public Object next() 199 { 200 Map.Entry e = (Map.Entry )i.next(); 201 return new Binding ((String )e.getKey(), e.getValue()); 202 } 203 204 public boolean hasMore() 205 { 206 return i.hasNext(); 207 } 208 209 public void close() 210 { 211 } 213 214 public boolean hasMoreElements() 215 { 216 return hasMore(); 217 } 218 219 public Object nextElement() 220 { 221 return next(); 222 } 223 224 }; 225 } 226 227 public void destroySubcontext(Name name) throws NamingException 228 { 229 destroySubcontext(name.toString()); 230 } 231 232 public void destroySubcontext(String name) throws NamingException 233 { 234 throw new OperationNotSupportedException ("subcontexts not supported"); 235 } 236 237 public Context createSubcontext(Name name) throws NamingException 238 { 239 return createSubcontext(name.toString()); 240 } 241 242 public Context createSubcontext(String name) throws NamingException 243 { 244 throw new OperationNotSupportedException ("subcontexts not supported"); 245 } 246 247 public Object lookupLink(Attributes.Name name) throws NamingException 248 { 249 return lookupLink(name.toString()); 250 } 251 252 public Object lookupLink(String name) throws NamingException 253 { 254 return lookup(name); 255 } 256 257 public NameParser getNameParser(Attributes.Name name) 258 { 259 return getNameParser(name.toString()); 260 } 261 262 public NameParser getNameParser(String name) 263 { 264 throw new UnsupportedOperationException ("getNameParser"); 265 } 266 267 public String composeName(String name, String prefix) throws NamingException 268 { 269 return composeName(new CompositeName (name), new CompositeName (prefix)).toString(); 270 } 271 272 public Name composeName(Name name, Name prefix) throws NamingException 273 { 274 Name result = (Name )prefix.clone(); 275 result.addAll(name); 276 return result; 277 } 278 279 public Object addToEnvironment(String key, Object val) 280 { 281 if (environment == null) 282 { 283 environment = new Hashtable (); 284 } 285 return environment.put(key, val); 286 } 287 288 public Object removeFromEnvironment(String key) 289 { 290 if (environment == null) 291 { 292 environment = new Hashtable (); 293 } 294 return environment.remove(key); 295 } 296 297 public Hashtable getEnvironment() 298 { 299 if (environment == null) 300 { 301 environment = new Hashtable (); 302 } 303 return environment; 304 } 305 306 public void close() 307 { 308 } 310 311 public String getNameInNamespace() 312 { 313 return ""; 314 } 315 316 } 317 | Popular Tags |