1 23 24 package com.sun.enterprise.resource; 25 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.HashMap ; 29 import javax.naming.Context ; 30 import javax.naming.Name ; 31 import javax.naming.NamingException ; 32 import javax.naming.NameNotFoundException ; 33 import javax.naming.RefAddr ; 34 import javax.naming.Reference ; 35 import javax.naming.spi.ObjectFactory ; 36 import javax.naming.spi.InitialContextFactory ; 37 38 39 42 public class JndiProxyObjectFactory implements ObjectFactory { 43 44 private static Hashtable contextMap = new Hashtable (); 47 48 protected static Context removeInitialContext(String jndiName) { 49 return (Context ) contextMap.remove(jndiName); 50 } 51 52 55 private Context loadInitialContext(String factoryClass, Hashtable env) { 56 Object factory = ResourceInstaller.loadObject(factoryClass); 57 if (factory == null) { 58 System.err.println("Cannot load external-jndi-resource " + 59 "factory-class '" + factoryClass + "'"); 60 return null; 61 } else if (! (factory instanceof 62 javax.naming.spi.InitialContextFactory )) { 63 64 System.err.println("external-jndi-resource factory-class '" 65 + factoryClass + "' must be of type " 66 + "javax.naming.spi.InitialContextFactory"); 67 return null; 68 } 69 70 Context context = null; 71 try { 72 context = ((InitialContextFactory )factory).getInitialContext(env); 73 } catch (NamingException ne) { 74 System.err.println("Exception thrown creating initial context " + 75 "for external JNDI factory '" + 76 factoryClass + "' " + ne.getMessage()); 77 } 78 79 return context; 80 } 81 82 85 public Object getObjectInstance(Object obj, 86 Name name, Context nameCtx, Hashtable environment) 87 throws NamingException { 88 89 String jndiLookupName = ""; 91 String jndiFactoryClass = null; 92 String bindName = null; 93 94 Reference ref = (Reference ) obj; 96 Enumeration addrs = ref.getAll(); 97 while (addrs.hasMoreElements()) { 98 RefAddr addr = (RefAddr ) addrs.nextElement(); 99 100 String prop = addr.getType(); 101 if (prop.equals("jndiName")) { 102 bindName = (String )addr.getContent(); 103 } 104 else if (prop.equals("jndiLookupName")) { 105 jndiLookupName = (String ) addr.getContent(); 106 } 107 else if (prop.equals("jndiFactoryClass")) { 108 jndiFactoryClass = (String ) addr.getContent(); 109 } 110 } 111 112 if (bindName == null) { 113 throw new NamingException ("JndiProxyObjectFactory: no bindName context info"); 114 } 115 116 ProxyRefAddr contextAddr = (ProxyRefAddr)ref.get(bindName); 117 Hashtable env = null; 118 if (contextAddr == null || 119 jndiFactoryClass == null || 120 (env = (Hashtable )(contextAddr.getContent())) == null) { 121 throw new NamingException ("JndiProxyObjectFactory: no info in the reference about the target context; contextAddr = " + contextAddr + " env = " + env + " factoryClass = " + jndiFactoryClass); 122 } 123 124 Context context = (Context )contextMap.get(bindName); 126 if (context == null) { 127 synchronized (contextMap) { 128 context = (Context )contextMap.get(bindName); 129 if (context == null) { 130 context = loadInitialContext(jndiFactoryClass, env); 131 contextMap.put(bindName, context); 132 } 133 } 134 } 135 136 if (context == null) 137 throw new NamingException ("JndiProxyObjectFactory no InitialContext" + jndiFactoryClass); 138 139 try { 141 return context.lookup(jndiLookupName); 142 } catch (NameNotFoundException e) { 143 throw new ExternalNameNotFoundException(e); 144 } 145 } 146 } 147 | Popular Tags |