1 22 package org.jboss.naming; 23 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Proxy ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.Hashtable ; 29 import javax.naming.Context ; 30 import javax.naming.NameNotFoundException ; 31 import javax.naming.NamingException ; 32 33 import org.jnp.interfaces.NamingContextFactory; 34 35 49 public class BridgeNamingContextFactory extends NamingContextFactory 50 { 51 public Context getInitialContext(Hashtable env) 53 throws NamingException 54 { 55 Context primaryCtx = super.getInitialContext(env); 56 Context bridgeCtx = primaryCtx; 57 Object providerURL2 = env.get("org.jboss.naming.provider.url2"); 58 if( providerURL2 != null ) 59 { 60 Hashtable env2 = (Hashtable ) env.clone(); 62 env2.put(Context.PROVIDER_URL, providerURL2); 63 Context secondaryCtx = super.getInitialContext(env2); 64 InvocationHandler h = new BridgeContext(primaryCtx, secondaryCtx); 65 Class [] interfaces = {Context .class}; 66 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 67 bridgeCtx = (Context ) Proxy.newProxyInstance(loader, interfaces, h); 68 } 69 return bridgeCtx; 70 } 71 72 76 static class BridgeContext implements InvocationHandler 77 { 78 private Context primaryCtx; 79 private Context secondaryCtx; 80 81 BridgeContext(Context primaryCtx, Context secondaryCtx) 82 { 83 this.primaryCtx = primaryCtx; 84 this.secondaryCtx = secondaryCtx; 85 } 86 87 public Object invoke(Object proxy, Method method, Object [] args) 88 throws Throwable 89 { 90 Object value = null; 91 try 93 { 94 value = method.invoke(primaryCtx, args); 95 } 96 catch(InvocationTargetException e) 97 { 98 Throwable t = e.getTargetException(); 99 if( t instanceof NameNotFoundException && method.getName().equals("lookup") ) 101 { 102 try 103 { 104 value = method.invoke(secondaryCtx, args); 105 } 106 catch (InvocationTargetException e1) 107 { 108 throw e1.getTargetException(); 109 } 110 } 111 else 112 { 113 throw t; 114 } 115 } 116 return value; 117 } 118 } 119 } 120 | Popular Tags |