1 19 51 package org.objectweb.carol.cmi.jndi; 52 53 import java.rmi.AccessException ; 54 import java.rmi.RemoteException ; 55 import java.util.Hashtable ; 56 57 import javax.naming.CompositeName ; 58 import javax.naming.Context ; 59 import javax.naming.InvalidNameException ; 60 import javax.naming.Name ; 61 import javax.naming.NameAlreadyBoundException ; 62 import javax.naming.NameClassPair ; 63 import javax.naming.NameNotFoundException ; 64 import javax.naming.NameParser ; 65 import javax.naming.NamingEnumeration ; 66 import javax.naming.NamingException ; 67 import javax.naming.OperationNotSupportedException ; 68 import javax.naming.Reference ; 69 import javax.naming.Referenceable ; 70 import javax.naming.spi.NamingManager ; 71 72 import org.objectweb.carol.cmi.ClusterRegistry; 73 import org.objectweb.carol.util.configuration.TraceCarol; 74 75 78 79 class FlatCtx implements Context { 80 private Hashtable myEnv = null; 81 private String provider = null; 82 private static NameParser myParser = new FlatNameParser(); 83 private ClusterRegistry reg; 84 85 FlatCtx(Hashtable environment) throws NamingException { 86 if (environment != null) { 87 myEnv = (Hashtable ) (environment.clone()); 88 provider = (String ) myEnv.get(Context.PROVIDER_URL); 89 } 90 if (provider == null) { 91 provider = "cmi:"; 92 } 93 org.objectweb.carol.cmi.NamingContext nc; 94 try { 95 nc = new org.objectweb.carol.cmi.NamingContext(provider); 96 } catch (java.net.MalformedURLException e) { 97 throw new NamingException (e.toString()); 98 } 99 try { 100 reg = org.objectweb.carol.cmi.Naming.getRegistry(nc.hp); 101 } catch (java.rmi.RemoteException e) { 102 throw new NamingException (e.toString()); 103 } 104 } 105 106 public Object lookup(String name) throws NamingException { 107 if (TraceCarol.isDebugJndiCarol()) 108 TraceCarol.debugJndiCarol("lookup(" + name + ")"); 109 if (name.equals("")) { 110 return (new FlatCtx(myEnv)); 113 } 114 try { 115 Object obj = reg.lookup(name); 116 if (obj instanceof RemoteReference) { 117 return NamingManager.getObjectInstance( 118 ((RemoteReference) obj).getReference(), 119 null, 120 this, 121 this.myEnv); 122 } 123 if (TraceCarol.isDebugJndiCarol()) 124 TraceCarol.debugJndiCarol("lookup(" + name + ") returned"); 125 return obj; 126 } catch (java.rmi.NotBoundException e) { 127 throw new NameNotFoundException (e.toString()); 128 } catch (Exception e) { 129 throw new NamingException (e.toString()); 130 } 131 } 132 133 public Object lookup(Name name) throws NamingException { 134 return lookup(name.toString()); 136 } 137 138 public void bind(String name, Object obj) throws NamingException { 139 if (TraceCarol.isDebugJndiCarol()) 140 TraceCarol.debugJndiCarol("bind(" + name + ")"); 141 if (name.equals("")) { 142 throw new InvalidNameException ("Cannot bind empty name"); 143 } 144 Object o = NamingManager.getStateToBind(obj, null, this, myEnv); 145 try { 146 if (o instanceof java.rmi.Remote ) { 147 reg.bind(name, (java.rmi.Remote ) o); 148 } else if (o instanceof Reference ) { 149 reg.bind(name, new ReferenceImpl((Reference ) o)); 150 } else if (o instanceof Referenceable ) { 151 reg.bind( 152 name, 153 new ReferenceImpl(((Referenceable ) o).getReference())); 154 } else 155 throw new NamingException ( 156 "object to bind must be Remote : " 157 + obj.getClass().getName()); 158 } catch (java.rmi.RemoteException e) { 159 throw new NamingException (e.toString()); 160 } catch (java.rmi.AlreadyBoundException e) { 161 throw new NameAlreadyBoundException (e.toString()); 162 } 163 } 164 165 public void bind(Name name, Object obj) throws NamingException { 166 bind(name.toString(), obj); 168 } 169 170 public void rebind(String name, Object obj) throws NamingException { 171 if (TraceCarol.isDebugJndiCarol()) 172 TraceCarol.debugJndiCarol("rebind(" + name + ")"); 173 if (name.equals("")) { 174 throw new InvalidNameException ("Cannot bind empty name"); 175 } 176 Object o = NamingManager.getStateToBind(obj, null, this, myEnv); 177 try { 178 if (o instanceof java.rmi.Remote ) { 179 reg.rebind(name, (java.rmi.Remote ) o); 180 } else if (o instanceof Reference ) { 181 reg.rebind(name, new ReferenceImpl((Reference ) o)); 182 } else if (o instanceof Referenceable ) { 183 reg.rebind( 184 name, 185 new ReferenceImpl(((Referenceable ) o).getReference())); 186 } else 187 throw new NamingException ( 188 "object to bind must be Remote : " 189 + obj.getClass().getName()); 190 } catch (java.rmi.RemoteException e) { 191 throw new NamingException (e.toString()); 192 } 193 } 194 195 public void rebind(Name name, Object obj) throws NamingException { 196 rebind(name.toString(), obj); 198 } 199 200 public void unbind(String name) throws NamingException { 201 if (TraceCarol.isDebugJndiCarol()) 202 TraceCarol.debugJndiCarol("unbind(" + name + ")"); 203 if (name.equals("")) { 204 throw new InvalidNameException ("Cannot unbind empty name"); 205 } 206 try { 207 reg.unbind(name); 208 } catch (java.rmi.RemoteException e) { 209 throw new NamingException (e.toString()); 210 } catch (java.rmi.NotBoundException e) { 211 throw new NameNotFoundException (e.toString()); 212 } 213 } 214 215 public void unbind(Name name) throws NamingException { 216 unbind(name.toString()); 218 } 219 220 public void rename(String oldname, String newname) throws NamingException { 221 throw new NamingException ("not supported"); 222 } 223 224 public void rename(Name oldname, Name newname) throws NamingException { 225 rename(oldname.toString(), newname.toString()); 227 } 228 229 public NamingEnumeration list(String name) throws NamingException { 230 try { 231 return new CmiNames(reg.list()); 232 } catch (AccessException e) { 233 throw new NamingException (e.toString()); 234 } catch (RemoteException e) { 235 throw new NamingException (e.toString()); 236 } 237 } 238 239 public NamingEnumeration list(Name name) throws NamingException { 240 return list(name.toString()); 242 } 243 244 public NamingEnumeration listBindings(String name) throws NamingException { 245 throw new NamingException ("not supported"); 246 } 247 248 public NamingEnumeration listBindings(Name name) throws NamingException { 249 return listBindings(name.toString()); 251 } 252 253 public void destroySubcontext(String name) throws NamingException { 254 throw new OperationNotSupportedException ("FlatCtx does not support subcontexts"); 255 } 256 257 public void destroySubcontext(Name name) throws NamingException { 258 destroySubcontext(name.toString()); 260 } 261 262 public Context createSubcontext(String name) throws NamingException { 263 throw new OperationNotSupportedException ("FlatCtx does not support subcontexts"); 264 } 265 266 public Context createSubcontext(Name name) throws NamingException { 267 return createSubcontext(name.toString()); 269 } 270 271 public Object lookupLink(String name) throws NamingException { 272 return lookup(name); 274 } 275 276 public Object lookupLink(Name name) throws NamingException { 277 return lookupLink(name.toString()); 279 } 280 281 public NameParser getNameParser(String name) throws NamingException { 282 return myParser; 283 } 284 285 public NameParser getNameParser(Name name) throws NamingException { 286 return getNameParser(name.toString()); 288 } 289 290 public String composeName(String name, String prefix) 291 throws NamingException { 292 Name result = 293 composeName(new CompositeName (name), new CompositeName (prefix)); 294 return result.toString(); 295 } 296 297 public Name composeName(Name name, Name prefix) throws NamingException { 298 Name result = (Name ) (prefix.clone()); 299 result.addAll(name); 300 return result; 301 } 302 303 public Object addToEnvironment(String propName, Object propVal) 304 throws NamingException { 305 if (myEnv == null) { 306 myEnv = new Hashtable (5, 0.75f); 307 } 308 return myEnv.put(propName, propVal); 309 } 310 311 public Object removeFromEnvironment(String propName) 312 throws NamingException { 313 if (myEnv == null) 314 return null; 315 316 return myEnv.remove(propName); 317 } 318 319 public Hashtable getEnvironment() throws NamingException { 320 if (myEnv == null) { 321 return new Hashtable (3, 0.75f); 323 } else { 324 return (Hashtable ) myEnv.clone(); 325 } 326 } 327 328 public String getNameInNamespace() throws NamingException { 329 return ""; 330 } 331 332 public void close() throws NamingException { 333 myEnv = null; 334 reg = null; 335 } 336 337 class CmiNames implements NamingEnumeration { 339 String [] names; 340 int index = 0; 341 342 CmiNames (String [] names) { 343 this.names = names; 344 } 345 346 public boolean hasMoreElements() { 347 return index < names.length; 348 } 349 350 public boolean hasMore() throws NamingException { 351 return hasMoreElements(); 352 } 353 354 public Object nextElement() { 355 String name = names[index++]; 356 String className = "java.lang.Object"; 357 return new NameClassPair (name, className); 358 } 359 360 public Object next() throws NamingException { 361 return nextElement(); 362 } 363 364 public void close() throws NamingException { 365 names=null; 366 } 367 } 368 } 369 | Popular Tags |