Making a local hash based Context
jndi.properties
1 2 java.naming.factory.initial=com.admin.model.naming.ContextFactory 3 java.naming.provider.url=iiop://localhost:1050
ContextFactory.java
1 2 package com.admin.model.naming; 3 import java.util.Hashtable; 4 import javax.naming.Context; 5 import javax.naming.NamingException; 6 import javax.naming.spi.InitialContextFactory; 7 public class ContextFactory implements InitialContextFactory { 8 public ContextFactory() { 9 } 10 public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException { 11 ContextImpl ctx = (ContextImpl)ContextImpl.getInstance(); 12 ctx.setEnvironment(environment); 13 return ctx; 14 } 15 }
ContextImpl.java
1 2 package com.admin.model.naming; 3 import java.util.Hashtable; 4 import java.util.logging.Logger; 5 import javax.naming.Binding; 6 import javax.naming.CompositeName; 7 import javax.naming.Context; 8 import javax.naming.Name; 9 import javax.naming.NameClassPair; 10 import javax.naming.NameParser; 11 import javax.naming.NamingEnumeration; 12 import javax.naming.NamingException; 13 public class ContextImpl implements Context { 14 private static Logger logger = Logger.getLogger(ContextImpl.class.getName()); 15 private static ContextImpl instance = new ContextImpl(); 16 private Hashtable environment; 17 private Hashtable<Name, Object> directory = new Hashtable(); 18 19 private ContextImpl() { 20 } 21 22 static Context getInstance() { 23 return instance; 24 } 25 26 public Object lookup(Name name) throws NamingException { 27 logger.entering(getClass().getName(), "lookup", name); 28 Object result = null; 29 if(!directory.containsKey(name)) { 30 throw new NamingException("Naming directory does not contain entry for: " + name); 31 } 32 result = directory.get(name); 33 logger.exiting(getClass().getName(), "lookup", result); 34 return result; 35 } 36 37 public Object lookup(String name) throws NamingException { 38 logger.entering(getClass().getName(), "lookup", name); 39 Object result = null; 40 Name properName = new CompositeName(name); 41 if(!directory.containsKey(properName)) { 42 throw new NamingException("Naming directory does not contain entry for: " + properName); 43 } 44 result = directory.get(properName); 45 logger.exiting(getClass().getName(), "lookup", result); 46 return result; 47 } 48 49 public void bind(Name name, Object obj) throws NamingException { 50 logger.entering(getClass().getName(), "bind", new Object[]{name, obj}); 51 if(directory.containsKey(name)) { 52 throw new NamingException("Naming directory already contains entry for: " + name); 53 } 54 directory.put(name, obj); 55 } 56 57 public void bind(String name, Object obj) throws NamingException { 58 logger.entering(getClass().getName(), "bind", new Object[]{name, obj}); 59 Name properName = new CompositeName(name); 60 if(directory.containsKey(properName)) { 61 throw new NamingException("Naming directory already contains entry for: " + properName); 62 } 63 directory.put(properName, obj); 64 } 65 66 public void rebind(Name name, Object obj) throws NamingException { 67 logger.entering(getClass().getName(), "rebind", new Object[]{name, obj}); 68 if(!directory.containsKey(name)) { 69 throw new NamingException("Naming directory does not contain entry for: " + name); 70 } 71 directory.put(name, obj); 72 } 73 74 public void rebind(String name, Object obj) throws NamingException { 75 logger.entering(getClass().getName(), "rebind", new Object[]{name, obj}); 76 Name properName = new CompositeName(name); 77 if(!directory.containsKey(properName)) { 78 throw new NamingException("Naming directory does not contain entry for: " + properName); 79 } 80 directory.put(properName, obj); 81 } 82 83 public void unbind(Name name) throws NamingException { 84 logger.entering(getClass().getName(), "unbind", name); 85 if(!directory.containsKey(name)) { 86 throw new NamingException("Naming directory does not contain entry for: " + name); 87 } 88 directory.remove(name); 89 } 90 91 public void unbind(String name) throws NamingException { 92 logger.entering(getClass().getName(), "unbind", name); 93 Name properName = new CompositeName(name); 94 if(!directory.containsKey(properName)) { 95 throw new NamingException("Naming directory does not contain entry for: " + properName); 96 } 97 directory.remove(properName); 98 } 99 100 public void rename(Name oldName, Name newName) throws NamingException { 101 logger.entering(getClass().getName(), "rename", new Object[]{oldName, newName}); 102 Object obj = lookup(oldName); 103 unbind(oldName); 104 bind(newName, obj); 105 } 106 107 public void rename(String oldName, String newName) throws NamingException { 108 logger.entering(getClass().getName(), "rename", new Object[]{oldName, newName}); 109 Object obj = lookup(oldName); 110 unbind(oldName); 111 bind(newName, obj); 112 } 113 114 public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { 115 logger.entering(getClass().getName(), "list", name); 116 return null; 117 } 118 119 public NamingEnumeration<NameClassPair> list(String name) throws NamingException { 120 logger.entering(getClass().getName(), "list", name); 121 return null; 122 } 123 124 public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { 125 logger.entering(getClass().getName(), "listBindings", name); 126 return null; 127 } 128 129 public NamingEnumeration<Binding> listBindings(String name) throws NamingException { 130 logger.entering(getClass().getName(), "listBindings", name); 131 return null; 132 } 133 134 public void destroySubcontext(Name name) throws NamingException { 135 logger.entering(getClass().getName(), "destroySubcontext", name); 136 } 137 138 public void destroySubcontext(String name) throws NamingException { 139 logger.entering(getClass().getName(), "destroySubcontext", name); 140 } 141 142 public Context createSubcontext(Name name) throws NamingException { 143 logger.entering(getClass().getName(), "createSubcontext", name); 144 return null; 145 } 146 147 public Context createSubcontext(String name) throws NamingException { 148 logger.entering(getClass().getName(), "createSubcontext", name); 149 return null; 150 } 151 152 public Object lookupLink(Name name) throws NamingException { 153 logger.entering(getClass().getName(), "lookupLink", name); 154 return null; 155 } 156 157 public Object lookupLink(String name) throws NamingException { 158 logger.entering(getClass().getName(), "lookupLink", name); 159 return null; 160 } 161 162 public NameParser getNameParser(Name name) throws NamingException { 163 logger.entering(getClass().getName(), "getNameParser", name); 164 return null; 165 } 166 167 public NameParser getNameParser(String name) throws NamingException { 168 logger.entering(getClass().getName(), "getNameParser", name); 169 return null; 170 } 171 172 public Name composeName(Name name, Name prefix) throws NamingException { 173 logger.entering(getClass().getName(), "composeName", new Object[]{name, prefix}); 174 return null; 175 } 176 177 public String composeName(String name, String prefix) throws NamingException { 178 logger.entering(getClass().getName(), "composeName", new Object[]{name, prefix}); 179 return null; 180 } 181 182 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 183 logger.entering(getClass().getName(), "addToEnvironment", new Object[]{propName, propVal}); 184 environment.put(propName, propVal); 185 return null; 186 } 187 188 public Object removeFromEnvironment(String propName) throws NamingException { 189 logger.entering(getClass().getName(), "removeFromEnvironment", propName); 190 Object result = environment.get(propName); 191 environment.remove(propName); 192 return result; 193 } 194 195 public Hashtable getEnvironment() throws NamingException { 196 logger.entering(getClass().getName(), "getEnvironment"); 197 return environment; 198 } 199 200 void setEnvironment(Hashtable env) { 201 environment = env; 202 } 203 204 public void close() throws NamingException { 205 logger.entering(getClass().getName(), "close"); 206 } 207 208 public String getNameInNamespace() throws NamingException { 209 logger.entering(getClass().getName(), "getNameInNamespace"); 210 return null; 211 } 212 }
usage
1 2 InitialContext ctx = new InitialContext(); 3 ctx.bind("DataSource", ds);