1 28 29 package com.caucho.naming; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.config.types.InitParam; 33 import com.caucho.util.L10N; 34 35 import javax.annotation.PostConstruct; 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.NamingException ; 39 import javax.naming.spi.InitialContextFactory ; 40 import java.util.Hashtable ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class LinkProxy implements ObjectProxy, java.io.Serializable { 48 private static Logger log 49 = Logger.getLogger(LinkProxy.class.getName()); 50 private static L10N L = new L10N(LinkProxy.class); 51 52 protected InitialContextFactory _factory; 54 protected Class _factoryClass; 56 protected Hashtable <String ,String > _props; 58 protected String _name; 60 protected String _foreignName; 62 63 66 public LinkProxy() 67 throws NamingException 68 { 69 } 70 71 78 public LinkProxy(InitialContextFactory factory, 79 Hashtable <String ,String > props, 80 String name) 81 throws NamingException 82 { 83 if (factory == null) 84 throw new NullPointerException (); 85 86 _factory = factory; 87 _props = props; 88 _foreignName = name; 89 } 90 91 96 public LinkProxy(String name) 97 throws NamingException 98 { 99 _foreignName = name; 100 } 101 102 105 public void setJndiName(String name) 106 { 107 _name = name; 108 } 109 110 113 public void setName(String name) 114 { 115 setJndiName(name); 116 } 117 118 121 public void setFactory(Class factoryClass) 122 { 123 _factoryClass = factoryClass; 124 } 125 126 129 public void setJndiFactory(Class factoryClass) 130 { 131 setFactory(factoryClass); 132 } 133 134 137 public void setForeignName(String name) 138 { 139 _foreignName = name; 140 } 141 142 145 public void addInitParam(InitParam initParam) 146 { 147 if (_props == null) 148 _props = new Hashtable <String ,String >(); 149 150 _props.putAll(initParam.getParameters()); 151 } 152 153 160 public Object createObject(Hashtable env) 161 throws NamingException 162 { 163 Context context; 164 Hashtable <String ,String > mergeEnv; 165 166 if (env == null || env.size() == 0) 167 mergeEnv = _props; 168 else if (_props == null || _props.size() == 0) 169 mergeEnv = env; 170 else { 171 mergeEnv = new Hashtable <String ,String >(); 172 mergeEnv.putAll(_props); 173 mergeEnv.putAll(env); 174 } 175 176 if (_factory != null) { 177 context = _factory.getInitialContext(mergeEnv); 178 } 179 else { 180 context = new InitialContext (mergeEnv); 181 _foreignName = Jndi.getFullName(_foreignName); 182 } 183 184 if (_foreignName != null) { 185 try { 186 187 return context.lookup(_foreignName); 188 189 } catch (RuntimeException e) { 190 191 if (log.isLoggable(Level.FINE)) 192 log.log(Level.FINE, e.toString(), e); 193 194 throw e; 195 196 } catch (NamingException e) { 197 198 if (log.isLoggable(Level.FINER)) 199 log.log(Level.FINER, e.toString(), e); 200 201 throw e; 202 } 203 } 204 else 205 return context; 206 } 207 208 211 @PostConstruct 212 public void init() 213 throws Exception 214 { 215 if (_name == null) 216 throw new ConfigException(L.l("<jndi-link> configuration needs a <jndi-name>. The <jndi-name> is the JNDI name where the context will be linked.")); 217 218 Class factoryClass = _factoryClass; 219 220 if (factoryClass != null) 221 _factory = (InitialContextFactory ) factoryClass.newInstance(); 222 223 if (log.isLoggable(Level.CONFIG)) { 224 if (_foreignName != null) 225 log.config("jndi-link[jndi-name=" + _name 226 + ", foreign-name=" + _foreignName + "] configured"); 227 else if (_factoryClass != null) 228 log.config("jndi-link[jndi-name=" + _name 229 + ", factory=" + _factoryClass.getName() + "] configured"); 230 } 231 232 if (_foreignName != null 233 && Jndi.getFullName(_name).equals(Jndi.getFullName(_foreignName))) 234 return; 235 236 Jndi.bindDeep(_name, this); 239 } 240 241 public String toString() 242 { 243 if (_factoryClass != null) 244 return "LinkProxy[name=" + _name + ",factory=" + _factoryClass.getName() + "]"; 245 else 246 return "LinkProxy[name=" + _name + ",foreign=" + _foreignName + "]"; 247 } 248 } 249 | Popular Tags |