1 22 package org.objectweb.petals.kernel.registry.jndi; 23 24 import java.io.Serializable ; 25 import java.util.Hashtable ; 26 27 import javax.naming.Binding ; 28 import javax.naming.CompositeName ; 29 import javax.naming.Context ; 30 import javax.naming.Name ; 31 import javax.naming.NameClassPair ; 32 import javax.naming.NameParser ; 33 import javax.naming.NamingEnumeration ; 34 import javax.naming.NamingException ; 35 36 import org.objectweb.petals.kernel.registry.msg.request.BindRequest; 37 import org.objectweb.petals.kernel.registry.msg.request.CreateSubcontextRequest; 38 import org.objectweb.petals.kernel.registry.msg.request.DestroySubcontextRequest; 39 import org.objectweb.petals.kernel.registry.msg.request.ListBindingsRequest; 40 import org.objectweb.petals.kernel.registry.msg.request.ListRequest; 41 import org.objectweb.petals.kernel.registry.msg.request.LookupLinkRequest; 42 import org.objectweb.petals.kernel.registry.msg.request.LookupRequest; 43 import org.objectweb.petals.kernel.registry.msg.request.RebindRequest; 44 import org.objectweb.petals.kernel.registry.msg.request.RenameRequest; 45 import org.objectweb.petals.kernel.registry.msg.request.UnbindRequest; 46 47 52 public class NamingContextImpl implements Context , Serializable { 53 54 private static final long serialVersionUID = 1L; 55 56 59 protected String contextName; 60 61 64 protected Hashtable <String , Object > env; 65 66 69 protected JNDIConnection jndiConnection; 70 71 public NamingContextImpl(Hashtable <String , Object > env, 72 JNDIConnection jndiConnection, String contextName) { 73 super(); 74 this.env = env; 75 this.jndiConnection = jndiConnection; 76 this.contextName = contextName; 77 } 78 79 82 public Object addToEnvironment(String propName, Object propVal) 83 throws NamingException { 84 Object out = env.get(propName); 85 env.put(propName, propVal); 86 return out; 87 } 88 89 92 public void bind(Name name, Object obj) throws NamingException { 93 bind(name.toString(), obj); 94 } 95 96 99 public void bind(String name, Object obj) throws NamingException { 100 jndiConnection.send(new BindRequest(contextName, name, obj, 0, 0)); 101 } 102 103 106 public void close() throws NamingException { 107 env = null; 108 jndiConnection = null; 109 contextName = null; 110 } 111 112 115 public Name composeName(Name name, Name prefix) throws NamingException { 116 if (name == null) { 117 throw new NamingException ("Name must be non null"); 118 } 119 if (prefix == null) { 120 throw new NamingException ("Prefix must be non null"); 121 } 122 return new CompositeName (prefix.toString() + name.toString()); 123 } 124 125 128 public String composeName(String name, String prefix) 129 throws NamingException { 130 if (name == null) { 131 throw new NamingException ("Name must be non null"); 132 } 133 if (prefix == null) { 134 throw new NamingException ("Prefix must be non null"); 135 } 136 return prefix + name; 137 } 138 139 142 public Context createSubcontext(Name name) throws NamingException { 143 return createSubcontext(name.toString()); 144 } 145 146 149 public Context createSubcontext(String name) throws NamingException { 150 Object newName = jndiConnection.send(new CreateSubcontextRequest( 151 contextName, name, 0, 0)); 152 NamingContextImpl contextImpl = new NamingContextImpl(env, 153 jndiConnection, (String ) newName); 154 return contextImpl; 155 } 156 157 160 public void destroySubcontext(Name name) throws NamingException { 161 destroySubcontext(name.toString()); 162 } 163 164 167 public void destroySubcontext(String name) throws NamingException { 168 jndiConnection.send(new DestroySubcontextRequest(contextName, name, 0, 169 0)); 170 } 171 172 175 public Hashtable <?, ?> getEnvironment() throws NamingException { 176 return env; 177 } 178 179 182 public String getNameInNamespace() throws NamingException { 183 return contextName; 184 } 185 186 189 public NameParser getNameParser(Name name) throws NamingException { 190 return new JNDINameParser(); 191 } 192 193 196 public NameParser getNameParser(String name) throws NamingException { 197 return new JNDINameParser(); 198 } 199 200 203 public NamingEnumeration <NameClassPair > list(Name name) 204 throws NamingException { 205 return list(name.toString()); 206 } 207 208 211 public NamingEnumeration <NameClassPair > list(String name) 212 throws NamingException { 213 NameClassPair [] names = (NameClassPair []) jndiConnection 214 .send(new ListRequest(contextName, name, 0, 0)); 215 return new NamingEnumerationImpl<NameClassPair >(names); 216 } 217 218 221 public NamingEnumeration <Binding > listBindings(Name name) 222 throws NamingException { 223 return listBindings(name.toString()); 224 } 225 226 229 public NamingEnumeration <Binding > listBindings(String name) 230 throws NamingException { 231 Binding [] bindings = (Binding []) jndiConnection 232 .send(new ListBindingsRequest(contextName, name, 0, 0)); 233 return new NamingEnumerationImpl<Binding >(bindings); 234 } 235 236 239 public Object lookup(Name name) throws NamingException { 240 return lookup(name.toString()); 241 } 242 243 246 public Object lookup(String name) throws NamingException { 247 return jndiConnection.send(new LookupRequest(contextName, name, 0, 0)); 248 } 249 250 253 public Object lookupLink(Name name) throws NamingException { 254 return lookupLink(name.toString()); 255 } 256 257 260 public Object lookupLink(String name) throws NamingException { 261 return jndiConnection.send(new LookupLinkRequest(contextName, name, 0, 262 0)); 263 } 264 265 268 public void rebind(Name name, Object obj) throws NamingException { 269 rebind(name.toString(), obj); 270 } 271 272 275 public void rebind(String name, Object obj) throws NamingException { 276 jndiConnection.send(new RebindRequest(contextName, name, obj, 0, 0)); 277 } 278 279 282 public Object removeFromEnvironment(String propName) throws NamingException { 283 Object out = env.get(propName); 284 if (env.containsKey(propName)) { 285 env.remove(propName); 286 } 287 return out; 288 } 289 290 293 public void rename(Name oldName, Name newName) throws NamingException { 294 rename(oldName.toString(), newName.toString()); 295 } 296 297 300 public void rename(String oldName, String newName) throws NamingException { 301 jndiConnection.send(new RenameRequest(contextName, oldName, newName, 0, 302 0)); 303 } 304 305 308 public void unbind(Name name) throws NamingException { 309 unbind(name.toString()); 310 } 311 312 315 public void unbind(String name) throws NamingException { 316 jndiConnection.send(new UnbindRequest(contextName, name, 0, 0)); 317 } 318 319 } 320 | Popular Tags |