1 21 package net.sf.hajdbc.sql; 22 23 import java.util.HashMap ; 24 import java.util.Hashtable ; 25 import java.util.Map ; 26 27 import javax.naming.CompositeName ; 28 import javax.naming.Context ; 29 import javax.naming.Name ; 30 import javax.naming.NameNotFoundException ; 31 import javax.naming.NameParser ; 32 import javax.naming.NamingEnumeration ; 33 import javax.naming.NamingException ; 34 import javax.naming.Reference ; 35 import javax.naming.Referenceable ; 36 import javax.naming.spi.InitialContextFactory ; 37 import javax.naming.spi.ObjectFactory ; 38 39 45 public class MockInitialContextFactory implements InitialContextFactory 46 { 47 private static ThreadLocal threadLocal = new ThreadLocal () 48 { 49 52 protected Object initialValue() 53 { 54 return new MockContext(); 55 } 56 }; 57 58 61 public Context getInitialContext(Hashtable environment) throws NamingException 62 { 63 return (Context ) threadLocal.get(); 64 } 65 66 70 public static class MockContext implements Context 71 { 72 private Map referenceMap = new HashMap (); 73 74 77 public Object lookup(Name name) throws NamingException 78 { 79 return this.lookup(name.toString()); 80 } 81 82 85 public Object lookup(String name) throws NamingException 86 { 87 Reference reference = (Reference ) this.referenceMap.get(name.toString()); 88 89 if (reference == null) 90 { 91 throw new NameNotFoundException (name.toString()); 92 } 93 94 try 95 { 96 ObjectFactory factory = (ObjectFactory ) Thread.currentThread().getContextClassLoader().loadClass(reference.getFactoryClassName()).newInstance(); 97 98 return factory.getObjectInstance(reference, new CompositeName (name), this, null); 99 } 100 catch (Exception e) 101 { 102 NamingException exception = new NamingException (); 103 exception.setRootCause(e); 104 exception.initCause(e); 105 106 throw exception; 107 } 108 } 109 110 113 public void bind(Name name, Object object) throws NamingException 114 { 115 this.bind(name.toString(), object); 116 } 117 118 121 public void bind(String name, Object object) throws NamingException 122 { 123 Reference reference = null; 124 125 if (Reference .class.isInstance(object)) 126 { 127 reference = (Reference ) object; 128 } 129 else if (Referenceable .class.isInstance(object)) 130 { 131 reference = ((Referenceable ) object).getReference(); 132 } 133 else 134 { 135 throw new NamingException ("Must extend javax.naming.Reference or implement javax.naming.Referenceable"); 136 } 137 138 this.referenceMap.put(name, reference); 139 } 140 141 144 public void rebind(Name name, Object object) throws NamingException 145 { 146 this.rebind(name.toString(), object); 147 } 148 149 152 public void rebind(String name, Object object) throws NamingException 153 { 154 this.bind(name, object); 155 } 156 157 160 public void unbind(Name name) throws NamingException 161 { 162 this.unbind(name.toString()); 163 } 164 165 168 public void unbind(String name) throws NamingException 169 { 170 this.referenceMap.remove(name); 171 } 172 173 176 public void rename(Name oldName, Name newName) throws NamingException 177 { 178 this.rename(oldName.toString(), newName.toString()); 179 } 180 181 184 public void rename(String oldName, String newName) throws NamingException 185 { 186 this.referenceMap.put(newName, this.referenceMap.remove(oldName)); 187 } 188 189 192 public NamingEnumeration list(Name name) throws NamingException 193 { 194 return null; 195 } 196 197 200 public NamingEnumeration list(String name) throws NamingException 201 { 202 return null; 203 } 204 205 208 public NamingEnumeration listBindings(Name name) throws NamingException 209 { 210 return null; 211 } 212 213 216 public NamingEnumeration listBindings(String name) throws NamingException 217 { 218 return null; 219 } 220 221 224 public void destroySubcontext(Name name) throws NamingException 225 { 226 } 227 228 231 public void destroySubcontext(String name) throws NamingException 232 { 233 } 234 235 238 public Context createSubcontext(Name name) throws NamingException 239 { 240 return null; 241 } 242 243 246 public Context createSubcontext(String name) throws NamingException 247 { 248 return null; 249 } 250 251 254 public Object lookupLink(Name name) throws NamingException 255 { 256 return null; 257 } 258 259 262 public Object lookupLink(String name) throws NamingException 263 { 264 return null; 265 } 266 267 270 public NameParser getNameParser(Name name) throws NamingException 271 { 272 return null; 273 } 274 275 278 public NameParser getNameParser(String name) throws NamingException 279 { 280 return null; 281 } 282 283 286 public Name composeName(Name arg0, Name arg1) throws NamingException 287 { 288 return null; 289 } 290 291 294 public String composeName(String arg0, String arg1) throws NamingException 295 { 296 return null; 297 } 298 299 302 public Object addToEnvironment(String arg0, Object arg1) throws NamingException 303 { 304 return null; 305 } 306 307 310 public Object removeFromEnvironment(String arg0) throws NamingException 311 { 312 return null; 313 } 314 315 318 public Hashtable getEnvironment() throws NamingException 319 { 320 return null; 321 } 322 323 326 public void close() throws NamingException 327 { 328 this.referenceMap.clear(); 329 } 330 331 334 public String getNameInNamespace() throws NamingException 335 { 336 return null; 337 } 338 } 339 } 340 | Popular Tags |