1 28 package org.objectweb.carol.jndi.spi; 29 30 import java.util.HashMap ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import javax.naming.Context ; 36 import javax.naming.Name ; 37 import javax.naming.NameParser ; 38 import javax.naming.NamingEnumeration ; 39 import javax.naming.NamingException ; 40 41 import org.objectweb.carol.rmi.exception.NamingExceptionHelper; 42 import org.objectweb.carol.util.configuration.ConfigurationRepository; 43 import org.objectweb.carol.util.configuration.ProtocolConfiguration; 44 import org.objectweb.carol.util.configuration.TraceCarol; 45 46 59 public class MultiContext implements Context { 60 61 64 private Map contextsOfConfigurations = null; 65 66 69 private Context currentInitialContext = null; 70 71 72 75 private Hashtable environment = null; 76 77 82 public MultiContext(Hashtable env) throws NamingException { 83 this.environment = env; 84 if (TraceCarol.isDebugJndiCarol()) { 85 TraceCarol.debugJndiCarol("MultiContext.MultiContext(env), env = " + env); 86 } 87 88 String activeConfigName = null; 90 try { 91 activeConfigName = ConfigurationRepository.getCurrentConfiguration().getName(); 92 } catch (Error err) { 93 throw NamingExceptionHelper.create("Could not get a configuration", err); 94 } 95 96 ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations(); 98 contextsOfConfigurations = new HashMap (); 99 for (int i = 0; i < protocolConfigurations.length; i++) { 100 Context ctx = protocolConfigurations[i].getInitialContext(env); 101 if (protocolConfigurations[i].getName().equals(activeConfigName)) { 102 currentInitialContext = ctx; 103 } 104 contextsOfConfigurations.put(protocolConfigurations[i], ctx); 105 } 106 if (currentInitialContext == null) { 107 throw new NamingException ("No current initial context was set. Active environment was '" + activeConfigName + "'. Error in carol with environment = " + env); 108 } 109 } 110 111 117 public Object lookup(String name) throws NamingException { 118 return currentInitialContext.lookup(name); 119 } 120 121 127 public Object lookup(Name name) throws NamingException { 128 return currentInitialContext.lookup(name); 129 } 130 131 137 public void bind(String name, Object obj) throws NamingException { 138 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 139 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 140 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 141 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).bind(name, obj); 142 ConfigurationRepository.setCurrentConfiguration(old); 143 } 144 } 145 146 152 public void bind(Name name, Object obj) throws NamingException { 153 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 154 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 155 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 156 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).bind(name, obj); 157 ConfigurationRepository.setCurrentConfiguration(old); 158 } 159 } 160 161 167 public void rebind(String name, Object obj) throws NamingException { 168 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 169 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 170 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 171 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).rebind(name, obj); 172 ConfigurationRepository.setCurrentConfiguration(old); 173 } 174 } 175 176 184 public void rebind(Name name, Object obj) throws NamingException { 185 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 186 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 187 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 188 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).rebind(name, obj); 189 ConfigurationRepository.setCurrentConfiguration(old); 190 } 191 } 192 193 198 public void unbind(String name) throws NamingException { 199 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 200 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 201 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 202 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).unbind(name); 203 ConfigurationRepository.setCurrentConfiguration(old); 204 } 205 } 206 207 214 public void unbind(Name name) throws NamingException { 215 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 216 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 217 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 218 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).unbind(name); 219 ConfigurationRepository.setCurrentConfiguration(old); 220 } 221 } 222 223 230 public void rename(String oldName, String newName) throws NamingException { 231 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 232 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 233 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 234 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).rename(oldName, newName); 235 ConfigurationRepository.setCurrentConfiguration(old); 236 } 237 } 238 239 247 public void rename(Name oldName, Name newName) throws NamingException { 248 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 249 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 250 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 251 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).rename(oldName, newName); 252 ConfigurationRepository.setCurrentConfiguration(old); 253 } 254 } 255 256 265 public NamingEnumeration list(String name) throws NamingException { 266 return currentInitialContext.list(name); 267 } 268 269 279 public NamingEnumeration list(Name name) throws NamingException { 280 return currentInitialContext.list(name); 281 } 282 283 291 public NamingEnumeration listBindings(String name) throws NamingException { 292 return currentInitialContext.listBindings(name); 293 } 294 295 303 public NamingEnumeration listBindings(Name name) throws NamingException { 304 return currentInitialContext.listBindings(name); 305 } 306 307 312 public void destroySubcontext(String name) throws NamingException { 313 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 314 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 315 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 316 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).destroySubcontext(name); 317 ConfigurationRepository.setCurrentConfiguration(old); 318 } 319 } 320 321 328 public void destroySubcontext(Name name) throws NamingException { 329 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 330 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 331 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 332 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).destroySubcontext(name); 333 ConfigurationRepository.setCurrentConfiguration(old); 334 } 335 } 336 342 public Context createSubcontext(String name) throws NamingException { 343 return currentInitialContext.createSubcontext(name); 344 } 345 346 352 public Context createSubcontext(Name name) throws NamingException { 353 return currentInitialContext.createSubcontext(name); 354 } 355 356 364 public Object lookupLink(String name) throws NamingException { 365 return currentInitialContext.lookupLink(name); 366 } 367 368 376 public Object lookupLink(Name name) throws NamingException { 377 return currentInitialContext.lookupLink(name); 378 } 379 380 387 public NameParser getNameParser(String name) throws NamingException { 388 return currentInitialContext.getNameParser(name); 389 } 390 391 398 public NameParser getNameParser(Name name) throws NamingException { 399 return currentInitialContext.getNameParser(name); 400 } 401 402 409 public String composeName(String name, String prefix) throws NamingException { 410 return name; 411 } 412 413 420 public Name composeName(Name name, Name prefix) throws NamingException { 421 return currentInitialContext.composeName(name, prefix); 422 } 423 424 435 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 436 return environment.put(propName, propVal); 437 } 438 439 448 public Object removeFromEnvironment(String propName) throws NamingException { 449 return environment.remove(propName); 450 } 451 452 458 public Hashtable getEnvironment() throws NamingException { 459 return this.environment; 460 } 461 462 468 public void close() throws NamingException { 469 for (Iterator it = contextsOfConfigurations.keySet().iterator(); it.hasNext();) { 470 ProtocolConfiguration protocolConfiguration = (ProtocolConfiguration) it.next(); 471 ProtocolConfiguration old = ConfigurationRepository.setCurrentConfiguration(protocolConfiguration); 472 ((Context ) contextsOfConfigurations.get(protocolConfiguration)).close(); 473 ConfigurationRepository.setCurrentConfiguration(old); 474 } 475 476 } 477 478 483 public String getNameInNamespace() throws NamingException { 484 return currentInitialContext.getNameInNamespace(); 485 } 486 487 } 488 | Popular Tags |