1 22 package org.jboss.naming.client.java; 23 24 import java.util.Hashtable ; 25 import java.lang.reflect.InvocationHandler ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Proxy ; 28 import java.security.AccessController ; 29 import java.security.PrivilegedAction ; 30 import javax.naming.Context ; 31 import javax.naming.Name ; 32 import javax.naming.NamingException ; 33 import javax.naming.InitialContext ; 34 import javax.naming.OperationNotSupportedException ; 35 import javax.naming.NameParser ; 36 import javax.naming.spi.ObjectFactory ; 37 38 import org.jboss.corba.ORBFactory; 39 40 49 public class javaURLContextFactory 50 implements ObjectFactory 51 { 52 public static final String J2EE_CLIENT_NAME_PROP = "j2ee.clientName"; 53 54 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 56 Hashtable env) 57 throws Exception 58 { 59 String clientName = (String ) env.get(J2EE_CLIENT_NAME_PROP); 61 if (clientName == null) 62 { 63 clientName = (String ) AccessController.doPrivileged( 65 new PrivilegedAction () 66 { 67 public Object run() 68 { 69 try 70 { 71 return System.getProperty(J2EE_CLIENT_NAME_PROP); 72 } 73 catch (SecurityException e) 74 { 75 return null; 76 } 77 } 78 } 79 ); 80 if (clientName == null) 81 throw new NamingException ("Failed to find j2ee.clientName in jndi env"); 82 } 83 84 Object result = null; 85 86 if (nameCtx == null) 87 nameCtx = new InitialContext (env); 88 if (obj == null) 89 { 90 InvocationHandler handler = new EncContextProxy(nameCtx, clientName); 92 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 93 Class [] ifaces = {Context .class}; 94 result = Proxy.newProxyInstance(loader, ifaces, handler); 95 } 96 return result; 97 } 98 99 private static class EncContextProxy implements InvocationHandler 100 { 101 Context lookupCtx; 102 String clientName; 103 104 EncContextProxy(Context lookupCtx, String clientName) 105 { 106 this.lookupCtx = lookupCtx; 107 this.clientName = clientName; 108 } 109 110 112 public Object invoke(Object proxy, Method method, Object [] args) 113 throws Throwable 114 { 115 String methodName = method.getName(); 116 if (methodName.equals("toString") == true) 117 return "Client ENC(" + clientName + ")"; 118 119 if (methodName.equals("lookup") == false) 120 throw new OperationNotSupportedException ("Only lookup is supported, op=" + method); 121 NameParser parser = lookupCtx.getNameParser(""); 122 Name name = null; 123 if (args[0] instanceof String ) 124 name = parser.parse((String ) args[0]); 125 else 126 name = (Name ) args[0]; 127 128 if (name.size() < 2 || "java:comp".equals(name.get(0)) == false || "env".equals(name.get(1)) == false) 130 return getSpecialObject(name); 131 Context clientCtx = (Context ) lookupCtx.lookup(clientName); 133 Name bindingName = name.getSuffix(2); 135 Object binding = clientCtx.lookup(bindingName); 136 return binding; 137 } 138 139 public Object getSpecialObject(Name name) throws NamingException 140 { 141 if (name.size() > 0 && "java:comp".equals(name.get(0))) 142 { 143 if (name.size() == 2 && "ORB".equals(name.get(1))) 144 return ORBFactory.getORB(); 145 else if (name.size() == 2 && "HandleDelegate".equals(name.get(1))) 146 return HandleDelegateFactory.getHandleDelegateSingleton(); 147 } 148 throw new NamingException ("Name not found " + name); 149 } 150 } 151 } 152 | Popular Tags |