1 6 package org.jfox.jndi.enc; 7 8 import java.lang.reflect.InvocationHandler ; 9 import java.lang.reflect.Method ; 10 import javax.naming.Context ; 11 import javax.naming.LinkRef ; 12 import javax.naming.OperationNotSupportedException ; 13 import javax.naming.Reference ; 14 15 import org.jfox.jndi.InitialContextHelper; 16 17 20 21 public class EnterpriseContextInvocationHandler implements InvocationHandler { 22 23 private static Context initCtx = InitialContextHelper.getInitialContext(); 24 25 private Context enterpriseContext = null; 26 27 public EnterpriseContextInvocationHandler(Context enterpriseContext) { 28 this.enterpriseContext = enterpriseContext; 29 } 30 31 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 33 String methodName = method.getName(); 34 if(methodName.equals("toString")) { 35 return enterpriseContext.toString(); 36 } 37 if(!methodName.equals("lookup")) { 38 throw new OperationNotSupportedException (methodName + " is not supported in EnterpriseContext, except only lookup!"); 39 } 40 41 Object obj = method.invoke(enterpriseContext, args); 42 if(obj instanceof Reference ) { 43 return lookupReference((Reference ) obj); 44 } 45 else { 46 return obj; 47 } 48 49 } 50 51 public Context getEnterpriseContext() { 52 return enterpriseContext; 53 } 54 55 public Context getInitialContext() { 56 return initCtx; 57 } 58 59 64 protected Object lookupReference(Reference ref) throws Exception { 65 if(ref instanceof LinkRef ) { 67 return getInitialContext().lookup(((LinkRef ) ref).getLinkName()); 68 } 69 else { 70 Object refAddr = ref.get(0).getContent(); 71 return getInitialContext().lookup(refAddr.toString()); 72 } 73 } 74 } 75 76 | Popular Tags |